*** empty log message ***
[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),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     shared_object(const shared_object &s_obj){
28         name = s_obj.name;
29         base_addr = s_obj.base_addr;
30         processed = s_obj.processed;
31         mapped = s_obj.mapped;
32         include_funcs = s_obj.include_funcs;
33         objs_image = s_obj.objs_image;
34         some_funcs = s_obj.some_funcs;
35     }
36     ~shared_object(){ objs_image = 0;}
37
38     const string &getName(){ return(name); }
39     u_int getBaseAddress() { return(base_addr); }
40     bool  isProcessed() { return(processed); }
41     bool  isMapped() { return(mapped); }
42     const image  *getImage() { return(objs_image); }
43     u_int getImageId(){ return((u_int)objs_image); }
44     bool includeFunctions(){ return(include_funcs); }
45     void changeIncludeFuncs(bool flag){ include_funcs = flag; } 
46
47     void  unMapped(){ mapped = false; }
48     void  setBaseAddress(u_int new_ba){ base_addr = new_ba; }
49
50     bool  getSymbolInfo(const string &n,Symbol &info) {
51         if(objs_image) {
52             return (objs_image->symbol_info(n,info));
53         }
54         return false;
55     }
56
57     vector<pd_Function *> *getAllFunctions(){
58         if(objs_image) {
59             // previously objs_image->mdlNormal....
60             return (&(objs_image->getAllFunctions()));
61         }
62         return 0;
63     }
64
65     // from a string that is a complete path name to a function in a module
66     // (ie. "/usr/lib/libc.so.1/write") return a string with the function
67     // part removed.  return 0 on error
68     char *getModulePart(string &full_path_name) ;
69
70 #ifndef BPATCH_LIBRARY
71     // get only the functions not excluded by the mdl options exclude_lib
72     // or exclude_funcs
73     vector<pd_Function *> *getSomeFunctions();
74 #endif
75
76     // Get list of ALL modules, not just included ones.
77     vector<pdmodule *> *getModules() {
78         if(objs_image) {
79             return (&(objs_image->getAllModules()));
80         }
81         return 0;
82     }
83
84     bool  addImage(image *i){ 
85         if(!processed && (objs_image == 0)) {
86             objs_image = i;
87             processed = true;
88             return true;
89         }
90         else {
91             return false;
92         }
93     }
94     bool removeImage(){ return true;}
95
96     pd_Function *findOneFunction(string f_name,bool check_excluded){
97         if (f_name.string_of() == 0) return 0;
98         if(objs_image) {
99 #ifndef BPATCH_LIBRARY
100             if(check_excluded){
101                 // only search the some_funcs list
102                 if(!some_funcs) getSomeFunctions();
103                 if(some_funcs) {
104                     for(u_int i=0; i < some_funcs->size(); i++){
105                         if(((*some_funcs)[i])->prettyName() == f_name){
106                             return (*some_funcs)[i];
107                         }
108                     }
109                     return 0;
110                 }
111             }
112 #endif
113             return (objs_image->findOneFunction(f_name));
114         }
115         return 0;
116     }
117
118     pd_Function *findOneFunctionFromAll(string f_name,bool check_excluded){
119         if (f_name.string_of() == 0) return 0;
120         if(objs_image) {
121 #ifndef BPATCH_LIBRARY
122             if(check_excluded){
123                 // only search the some_funcs list
124                 if(!some_funcs) getSomeFunctions();
125                 if(some_funcs) {
126                     for(u_int i=0; i < some_funcs->size(); i++){
127                         if(((*some_funcs)[i])->prettyName() == f_name){
128                             return (*some_funcs)[i];
129                         }
130                     }
131                     return 0;
132                 }
133             }
134 #endif
135             return (objs_image->findOneFunctionFromAll(f_name));
136         }
137         return 0;
138     }
139
140
141     pdmodule *findModule(string m_name,bool check_excluded){
142         if(objs_image) {
143             if(check_excluded && !include_funcs){
144                 return 0;
145             }
146             return (objs_image->findModule(m_name));
147         }
148         return 0;
149     }
150     pd_Function *findFunctionIn(Address adr,const process *p){
151         if((adr >= base_addr) && objs_image){
152             Address new_adr = adr - base_addr;
153             return(objs_image->findFunctionIn(new_adr,p));
154         }
155         return (0);
156     }
157     pd_Function *findFunctionInInstAndUnInst(Address adr,const process *p){
158         if((adr >= base_addr) && objs_image){
159             Address new_adr = adr - base_addr;
160             return(objs_image->findFunctionInInstAndUnInst(new_adr,p));
161         }
162         return (0);
163     }
164
165
166     //
167     //     PRIVATE DATA MEMBERS
168     //                          
169 private:
170     string  name;       // full file name of the shared object
171     u_int   base_addr;  // base address of where the shared object is mapped
172     bool    processed;  // if true, daemon has processed the shared obj. file
173     bool    mapped;     // if true, the application has the shared obj. mapped
174                         // shared objects can be unmapped as the appl. runs
175     bool include_funcs; // if true include the the functions from this shared
176                         // object in the set of all instrumentable functions
177                         // (this is for foci not refined on the Code heirarchy)
178                         // - Conceptually assumes that shared object has 1
179                         // and only 1 module.
180     image  *objs_image; // pointer to image if processed is true 
181     vector<pd_Function *> *some_funcs; // all functions not excluded by 
182                                        // exclude_func option
183 };
184
185 #endif