patch to fix exclude whole module
[dyninst.git] / dyninstAPI / src / sharedobject.h
1 #if !defined(_shared_object_h)
2 #define _shared_object_h
3
4 #include "util/h/String.h"
5 #include "dyninstAPI/src/symtab.h"
6 #include "dyninstAPI/src/symtab.h"
7 #ifndef BPATCH_LIBRARY
8 #include "paradynd/src/mdld.h"
9 #endif
10
11 /*
12  * A class for link map information about a shared object that is mmapped 
13  * by the dynamic linker into the applicaitons address space at runtime. 
14  */
15 #define         SHAREDOBJECT_NOCHANGE   0
16 #define         SHAREDOBJECT_ADDED      1
17 #define         SHAREDOBJECT_REMOVED    2
18
19 class shared_object {
20
21 public:
22     shared_object():name(0),short_name(0),base_addr(0),processed(false),
23          mapped(false),include_funcs(true), objs_image(0),some_funcs(0){}
24     shared_object(string &n,u_int b, bool p,bool m, bool i, image *d):
25         name(n), base_addr(b),processed(p),mapped(m),
26         include_funcs(i), objs_image(d),some_funcs(0){ 
27         set_short_name();
28     }
29     shared_object(const shared_object &s_obj){
30         name = s_obj.name;
31         short_name = s_obj.short_name;
32         base_addr = s_obj.base_addr;
33         processed = s_obj.processed;
34         mapped = s_obj.mapped;
35         include_funcs = s_obj.include_funcs;
36         objs_image = s_obj.objs_image;
37         some_funcs = s_obj.some_funcs;
38     }
39     ~shared_object(){ objs_image = 0;}
40
41     const string &getName(){ return(name); }
42     const string &getShortName() { return short_name; }
43     u_int getBaseAddress() { return(base_addr); }
44     bool  isProcessed() { return(processed); }
45     bool  isMapped() { return(mapped); }
46     const image  *getImage() { return(objs_image); }
47     u_int getImageId(){ return((u_int)objs_image); }
48     bool includeFunctions(){ return(include_funcs); }
49     void changeIncludeFuncs(bool flag){ include_funcs = flag; } 
50
51     void  unMapped(){ mapped = false; }
52     void  setBaseAddress(u_int new_ba){ base_addr = new_ba; }
53
54     bool  getSymbolInfo(const string &n,Symbol &info) {
55         if(objs_image) {
56             return (objs_image->symbol_info(n,info));
57         }
58         return false;
59     }
60
61     const vector<pd_Function *> *getAllFunctions(){
62         if(objs_image) {
63             // previously objs_image->mdlNormal....
64             return (&(objs_image->getAllFunctions()));
65         }
66         return 0;
67     }
68
69     // from a string that is a complete path name to a function in a module
70     // (ie. "/usr/lib/libc.so.1/write") return a string with the function
71     // part removed.  return 0 on error
72     char *getModulePart(string &full_path_name) ;
73
74 #ifndef BPATCH_LIBRARY
75     // get only the functions not excluded by the mdl options exclude_lib
76     // or exclude_funcs
77     vector<pd_Function *> *getSomeFunctions();
78 #endif
79
80     // Get list of ALL modules, not just included ones.
81     const vector<pdmodule *> *getModules() {
82         if(objs_image) {
83             return (&(objs_image->getAllModules()));
84         }
85         return 0;
86     }
87
88     bool  addImage(image *i){ 
89         if(!processed && (objs_image == 0)) {
90             objs_image = i;
91             processed = true;
92             return true;
93         }
94         else {
95             return false;
96         }
97     }
98     bool removeImage(){ return true;}
99
100     pd_Function *findOneFunction(string f_name,bool check_excluded){
101         if (f_name.string_of() == 0) return 0;
102         if(objs_image) {
103 #ifndef BPATCH_LIBRARY
104             if(check_excluded){
105                 // only search the some_funcs list
106                 if(!some_funcs) getSomeFunctions();
107                 if(some_funcs) {
108                     for(u_int i=0; i < some_funcs->size(); i++){
109                         if(((*some_funcs)[i])->prettyName() == f_name){
110                             return (*some_funcs)[i];
111                         }
112                     }
113                     return 0;
114                 }
115             }
116 #endif
117             return (objs_image->findOneFunction(f_name));
118         }
119         return 0;
120     }
121
122     pd_Function *findOneFunctionFromAll(string f_name,bool check_excluded){
123         if (f_name.string_of() == 0) return 0;
124         if(objs_image) {
125 #ifndef BPATCH_LIBRARY
126             if(check_excluded){
127                 // only search the some_funcs list
128                 if(!some_funcs) getSomeFunctions();
129                 if(some_funcs) {
130                     for(u_int i=0; i < some_funcs->size(); i++){
131                         if(((*some_funcs)[i])->prettyName() == f_name){
132                             return (*some_funcs)[i];
133                         }
134                     }
135                     return 0;
136                 }
137             }
138 #endif
139             return (objs_image->findOneFunctionFromAll(f_name));
140         }
141         return 0;
142     }
143
144
145     pdmodule *findModule(string m_name,bool check_excluded){
146         if(objs_image) {
147             if(check_excluded && !include_funcs){
148                 return 0;
149             }
150             return (objs_image->findModule(m_name));
151         }
152         return 0;
153     }
154     pd_Function *findFunctionIn(Address adr,const process *p){
155         if((adr >= base_addr) && objs_image){
156             Address new_adr = adr - base_addr;
157             return(objs_image->findFunctionIn(new_adr,p));
158         }
159         return (0);
160     }
161     pd_Function *findFunctionInInstAndUnInst(Address adr,const process *p){
162         if((adr >= base_addr) && objs_image){
163             Address new_adr = adr - base_addr;
164             return(objs_image->findFunctionInInstAndUnInst(new_adr,p));
165         }
166         return (0);
167     }
168
169
170     //
171     //     PRIVATE DATA MEMBERS
172     //                          
173 private:
174     string  name;       // full file name of the shared object
175     string  short_name; // name of shared object as it should be identified
176                         //  in mdl, e.g. as used for "exclude"....
177     u_int   base_addr;  // base address of where the shared object is mapped
178     bool    processed;  // if true, daemon has processed the shared obj. file
179     bool    mapped;     // if true, the application has the shared obj. mapped
180                         // shared objects can be unmapped as the appl. runs
181     bool include_funcs; // if true include the the functions from this shared
182                         // object in the set of all instrumentable functions
183                         // (this is for foci not refined on the Code heirarchy)
184                         // - Conceptually assumes that shared object has 1
185                         // and only 1 module.
186     image  *objs_image; // pointer to image if processed is true 
187     vector<pd_Function *> *some_funcs; // all functions not excluded by 
188                                        // exclude_func option
189
190     void set_short_name();
191 };
192
193 #endif