Merge branch 'master' of ssh://git.dyninst.org/pub/dyninst into Defensive
[dyninst.git] / dyninstAPI / src / BPatch_addressSpace.C
1 /*
2  * Copyright (c) 1996-2011 Barton P. Miller
3  *
4  * We provide the Paradyn Parallel Performance Tools (below
5  * described as "Paradyn") on an AS IS basis, and do not warrant its
6  * validity or performance.  We reserve the right to update, modify,
7  * or discontinue this software at any time.  We shall have no
8  * obligation to supply such updates or modifications or any other
9  * form of support to you.
10  *
11  * By your use of Paradyn, you understand and agree that we (or any
12  * other person or entity with proprietary rights in Paradyn) are
13  * under no obligation to provide either maintenance services,
14  * update services, notices of latent defects, or correction of
15  * defects for Paradyn.
16  *
17  * This library is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public
19  * License as published by the Free Software Foundation; either
20  * version 2.1 of the License, or (at your option) any later version.
21  *
22  * This library is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  * Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30  */
31
32 #define BPATCH_FILE
33
34 #include "process.h"
35 #include "binaryEdit.h"
36 #include "EventHandler.h"
37 #include "mailbox.h"
38 #include "signalgenerator.h"
39 #include "inst.h"
40 #include "instP.h"
41 #include "instPoint.h"
42 #include "function.h" // func_instance
43 #include "codeRange.h"
44 #include "dyn_thread.h"
45 #include "miniTramp.h"
46 #include "addressSpace.h"
47
48 #include "mapped_module.h"
49
50 #include "BPatch_libInfo.h"
51 #include "BPatch_asyncEventHandler.h"
52 #include "BPatch.h"
53 #include "BPatch_thread.h"
54 #include "BPatch_function.h"
55 #include "BPatch_point.h"
56 #include "callbacks.h"
57
58 #include "BPatch_private.h"
59
60 #include "ast.h"
61
62 #include "BPatch_addressSpace.h"
63
64 #include "BPatch_instruction.h"
65
66 #include "mapped_object.h"
67
68 #include <sstream>
69 #include "Parsing.h"
70
71 #include "Command.h"
72 #include "Relocation/DynInstrumenter.h"
73
74 using Dyninst::PatchAPI::Patcher;
75 using Dyninst::PatchAPI::PatcherPtr;
76 using Dyninst::PatchAPI::DynInsertSnipCommand;
77 using Dyninst::PatchAPI::DynReplaceFuncCommand;
78 using Dyninst::PatchAPI::DynModifyCallCommand;
79 using Dyninst::PatchAPI::DynRemoveCallCommand;
80
81 BPatch_addressSpace::BPatch_addressSpace() :
82    image(NULL)
83 {
84 }
85
86 BPatch_addressSpace::~BPatch_addressSpace()
87 {}
88
89
90 BPatch_function *BPatch_addressSpace::findOrCreateBPFunc(func_instance* ifunc,
91                                                          BPatch_module *bpmod)
92 {
93    if (!bpmod)
94       bpmod = image->findOrCreateModule(ifunc->mod());
95    assert(bpmod);
96    if (bpmod->func_map.count(ifunc)) {
97       BPatch_function *bpf = bpmod->func_map[ifunc];
98       assert(bpf);
99       assert(bpf->func == ifunc);
100       return bpf;
101    }
102
103    // Find the module that contains the function
104    if (bpmod == NULL && ifunc->mod() != NULL) {
105       bpmod = getImage()->findModule(ifunc->mod()->fileName().c_str());
106    }
107
108    // findModule has a tendency to make new function objects... so
109    // check the map again
110    if (bpmod->func_map.count(ifunc)) {
111       BPatch_function *bpf = bpmod->func_map[ifunc];
112       assert(bpf);
113       assert(bpf->func == ifunc);
114       return bpf;
115    }
116
117    BPatch_function *ret = new BPatch_function(this, ifunc, bpmod);
118    assert( ret != NULL );
119    assert(ret->func == ifunc);
120    return ret;
121 }
122
123
124
125
126 BPatch_point *BPatch_addressSpace::findOrCreateBPPoint(BPatch_function *bpfunc,
127                                                        instPoint *ip,
128                                                        BPatch_procedureLocation pointType)
129 {
130    assert(ip);
131
132    BPatch_module *mod = image->findOrCreateModule(ip->func()->mod());
133    assert(mod);
134
135    if (mod->instp_map.count(ip))
136       return mod->instp_map[ip];
137
138    if (pointType == BPatch_locUnknownLocation) {
139       cerr << "Error: point type not specified!" << endl;
140       assert(0);
141       return NULL;
142    }
143
144    AddressSpace *lladdrSpace = ip->func()->proc();
145    if (!bpfunc)
146       bpfunc = findOrCreateBPFunc(ip->func(), mod);
147
148    assert(bpfunc->func == ip->func());
149    std::pair<instPoint *, instPoint *> pointsToUse = instPoint::getInstpointPair(ip);
150
151    BPatch_point *pt = new BPatch_point(this, bpfunc,
152                                        pointsToUse.first, pointsToUse.second,
153                                        pointType, lladdrSpace);
154    mod->instp_map[ip] = pt;
155
156    return pt;
157 }
158
159 BPatch_variableExpr *BPatch_addressSpace::findOrCreateVariable(int_variable *v,
160                                                                BPatch_type *type)
161 {
162    BPatch_module *mod = image->findOrCreateModule(v->mod());
163    assert(mod);
164    if (mod->var_map.count(v))
165       return mod->var_map[v];
166
167    if (!type) {
168       SymtabAPI::Type *stype = v->ivar()->svar()->getType();
169
170       if (stype){
171          type = BPatch_type::findOrCreateType(stype);
172       }else{
173          type = BPatch::bpatch->type_Untyped;
174       }
175    }
176
177    BPatch_variableExpr *var = BPatch_variableExpr::makeVariableExpr(this, v, type);
178    mod->var_map[v] = var;
179    return var;
180 }
181
182
183
184 BPatch_function *BPatch_addressSpace::createBPFuncCB(AddressSpace *a, func_instance *f)
185 {
186    BPatch_addressSpace *aS = (BPatch_addressSpace *)a->up_ptr();
187    assert(aS);
188    return aS->findOrCreateBPFunc(f, NULL);
189 }
190
191 BPatch_point *BPatch_addressSpace::createBPPointCB(AddressSpace *a,
192                                                    func_instance *f,
193                                                    instPoint *ip, int type)
194 {
195    BPatch_addressSpace *aS = (BPatch_addressSpace *)a->up_ptr();
196    assert(aS);
197
198    BPatch_module *bpmod = aS->getImageInt()->findOrCreateModule(f->mod());
199    assert(bpmod);
200
201    BPatch_function *func = aS->findOrCreateBPFunc(f, bpmod);
202    assert(func);
203
204    return aS->findOrCreateBPPoint(func, ip, (BPatch_procedureLocation) type);
205 }
206
207
208
209 /***************************************************************************
210  * Bpatch_snippetHandle
211  ***************************************************************************/
212
213 /*
214  * BPatchSnippetHandle::BPatchSnippetHandle
215  *
216  * Constructor for BPatchSnippetHandle.  Delete the snippet instance(s)
217  * associated with the BPatchSnippetHandle.
218  */
219 BPatchSnippetHandle::BPatchSnippetHandle(BPatch_addressSpace * addSpace) :
220    addSpace_(addSpace)
221 {
222 }
223
224 /*
225  * BPatchSnippetHandle::~BPatchSnippetHandle
226  *
227  * Destructor for BPatchSnippetHandle.  Delete the snippet instance(s)
228  * associated with the BPatchSnippetHandle.
229  */
230 void BPatchSnippetHandle::BPatchSnippetHandle_dtor()
231 {
232    // don't delete inst instances since they are might have been copied
233 }
234
235 BPatch_addressSpace *BPatchSnippetHandle::getAddressSpaceInt()
236 {
237    return addSpace_;
238 }
239
240 BPatch_process *BPatchSnippetHandle::getProcessInt()
241 {
242    return dynamic_cast<BPatch_process *>(addSpace_);
243 }
244
245 BPatch_Vector<BPatch_thread *> &BPatchSnippetHandle::getCatchupThreadsInt()
246 {
247    return catchup_threads;
248 }
249
250 BPatch_function * BPatchSnippetHandle::getFuncInt()
251 {
252     if (!mtHandles_.empty()) {
253         func_instance *func = mtHandles_.back()->instP()->func();
254         BPatch_function *bpfunc = addSpace_->findOrCreateBPFunc(func,NULL);
255         return bpfunc;
256     }
257     return NULL;
258 }
259
260
261 BPatch_image * BPatch_addressSpace::getImageInt()
262 {
263    return image;
264 }
265
266
267 /*
268  * BPatch_addressSpace::deleteSnippet
269  *
270  * Deletes an instance of a snippet.
271  *
272  * handle       The handle returned by insertSnippet when the instance to
273  *              deleted was created.
274  */
275
276 bool BPatch_addressSpace::deleteSnippetInt(BPatchSnippetHandle *handle)
277    mal_printf("deleting snippet handle %p\n",handle);
278    if (getTerminated()) return true;
279
280    if (handle == NULL) {
281        bperr("Request to delete NULL snippet handle, returning false\n");
282        return false;
283    }
284
285    if (handle->addSpace_ != this) {
286      bperr("Error: wrong address space in deleteSnippet\n");
287      return false;
288    }
289
290    // if this is a process, check to see if the instrumentation is
291    // executing on the call stack
292    if ( handle->getProcess() && handle->mtHandles_.size() > 0 &&
293        BPatch_normalMode !=
294         handle->mtHandles_[0]->instP()->func()->obj()->hybridMode())
295    {
296        if (handle->mtHandles_.size() > 1) {
297            mal_printf("ERROR: Removing snippet that is installed in "
298                           "multiple miniTramps %s[%d]\n",FILE__,__LINE__);
299      }
300    }
301
302    // uninstrument and remove snippet handle from point datastructures
303    for (unsigned int i=0; i < handle->mtHandles_.size(); i++)
304      {
305        instPoint *iPoint = handle->mtHandles_[i]->instP();
306        handle->mtHandles_[i]->uninstrument();
307        BPatch_point *bPoint = findOrCreateBPPoint(NULL, iPoint,
308                                                   BPatch_point::convertInstPointType_t(iPoint->type()));
309        assert(bPoint);
310        bPoint->removeSnippet(handle);
311      }
312
313    //delete handle; //KEVINTODO: fix this, add instrumentation-removal callback
314
315    handle->mtHandles_.clear();
316
317    if (pendingInsertions == NULL) {
318      // Trigger it now
319      bool tmp;
320      finalizeInsertionSet(false, &tmp);
321    }
322
323    //delete handle;
324    return true;
325 }
326
327 /*
328  * BPatch_addressSpace::replaceCode
329  *
330  * Replace a given instruction with a BPatch_snippet.
331  *
332  * point       Represents the instruction to be replaced
333  * snippet     The replacing snippet
334  */
335
336 bool BPatch_addressSpace::replaceCodeInt(BPatch_point *point,
337       BPatch_snippet *snippet)
338 {
339    // Need to reevaluate how this code works. I don't think it should be
340    // point-based, though.
341
342    assert(0);
343    return false;
344 }
345
346 /*
347  * BPatch_addressSpace::replaceFunctionCall
348  *
349  * Replace a function call with a call to a different function.  Returns true
350  * upon success, false upon failure.
351  *
352  * point        The call site that is to be changed.
353  * newFunc      The function that the call site will now call.
354  */
355 bool BPatch_addressSpace::replaceFunctionCallInt(BPatch_point &point,
356       BPatch_function &newFunc)
357 {
358    char name[1024];
359    newFunc.getName(name, 1024);
360
361    // Can't make changes to code when mutations are not active.
362    if (!getMutationsActive())
363       return false;
364
365    assert(point.point && newFunc.lowlevel_func());
366
367   /* PatchAPI stuffs */
368    AddressSpace* addr_space = point.getAS();
369    DynModifyCallCommand::Ptr rep_call = DynModifyCallCommand::create(addr_space,
370       point.point->block(), newFunc.lowlevel_func(), point.point->func());
371   addr_space->patcher()->add(rep_call);
372   /* End of PatchAPI */
373   /*
374    point.getAS()->modifyCall(point.point->block(),
375                              newFunc.lowlevel_func(),
376                              point.point->func());
377   */
378    if (pendingInsertions == NULL) {
379      // Trigger it now
380      bool tmp;
381      finalizeInsertionSet(false, &tmp);
382    }
383    return true;
384 }
385
386 /*
387  * BPatch_addressSpace::removeFunctionCall
388  *
389  * Replace a function call with a NOOP.  Returns true upon success, false upon
390  * failure.
391  *
392  * point        The call site that is to be NOOPed out.
393  */
394 bool BPatch_addressSpace::removeFunctionCallInt(BPatch_point &point)
395 {
396    // Can't make changes to code when mutations are not active.
397    if (!getMutationsActive())
398       return false;
399
400    assert(point.point);
401
402   /* PatchAPI stuffs */
403    AddressSpace* addr_space = point.getAS();
404    DynRemoveCallCommand::Ptr remove_call = DynRemoveCallCommand::create(addr_space,
405       point.point->block(), point.point->func());
406   addr_space->patcher()->add(remove_call);
407   /* End of PatchAPI */
408    // point.getAS()->removeCall(point.point->block(), point.point->func());
409
410    if (pendingInsertions == NULL) {
411      // Trigger it now
412      bool tmp;
413      finalizeInsertionSet(false, &tmp);
414    }
415
416    return true;
417 }
418
419
420 /*
421  * BPatch_addressSpace::replaceFunction
422  *
423  * Replace all calls to function OLDFUNC with calls to NEWFUNC.
424  * Returns true upon success, false upon failure.
425  *
426  * oldFunc      The function to replace
427  * newFunc      The replacement function
428  */
429 bool BPatch_addressSpace::replaceFunctionInt(BPatch_function &oldFunc,
430       BPatch_function &newFunc)
431 {
432   assert(oldFunc.lowlevel_func() && newFunc.lowlevel_func());
433   if (!getMutationsActive())
434     return false;
435
436   // Self replacement is a nop
437   // We should just test direct equivalence here...
438   if (oldFunc.lowlevel_func() == newFunc.lowlevel_func()) {
439     return true;
440   }
441
442   /* PatchAPI stuffs */
443   AddressSpace* addr_space = oldFunc.lowlevel_func()->proc();
444   DynReplaceFuncCommand::Ptr rep_func = DynReplaceFuncCommand::create(addr_space,
445      oldFunc.lowlevel_func(), newFunc.lowlevel_func());
446   addr_space->patcher()->add(rep_func);
447   /* End of PatchAPI */
448
449   //oldFunc.lowlevel_func()->proc()->replaceFunction(oldFunc.lowlevel_func(), newFunc.lowlevel_func());
450
451   if (pendingInsertions == NULL) {
452     // Trigger it now
453     bool tmp;
454     finalizeInsertionSet(false, &tmp);
455   }
456   return true;
457 }
458
459 bool BPatch_addressSpace::wrapFunctionInt(BPatch_function &oldFunc,
460                                           BPatch_function &newFunc)
461 {
462    assert(oldFunc.lowlevel_func() && newFunc.lowlevel_func());
463    if (!getMutationsActive())
464       return false;
465
466    // Self replacement is a nop
467    // We should just test direct equivalence here...
468    if (oldFunc.lowlevel_func() == newFunc.lowlevel_func()) {
469       return true;
470    }
471
472    if (!oldFunc.lowlevel_func()->proc()->wrapFunction(oldFunc.lowlevel_func(), newFunc.lowlevel_func()))
473       return false;
474
475    if (pendingInsertions == NULL) {
476       // Trigger it now
477       bool tmp;
478       finalizeInsertionSet(false, &tmp);
479    }
480    return true;
481 }
482
483
484 bool BPatch_addressSpace::getAddressRangesInt( const char * fileName,
485       unsigned int lineNo,
486       std::vector< std::pair< unsigned long, unsigned long > > & ranges )
487 {
488    unsigned int originalSize = ranges.size();
489    BPatch_Vector< BPatch_module * > * modules = image->getModules();
490
491    /* Iteratate over the modules, looking for addr in each. */
492    for ( unsigned int i = 0; i < modules->size(); i++ ) {
493       BPatch_module *m = (*modules)[i];
494       m->getAddressRanges(fileName, lineNo, ranges);
495    }
496
497    if ( ranges.size() != originalSize ) { return true; }
498
499    return false;
500 } /* end getAddressRangesInt() */
501
502 bool BPatch_addressSpace::getSourceLinesInt( unsigned long addr,
503       BPatch_Vector< BPatch_statement > & lines )
504 {
505    return image->getSourceLinesInt(addr, lines);
506 } /* end getLineAndFile() */
507
508
509 /*
510  * BPatch_process::malloc
511  *
512  * Allocate memory in the thread's address space.
513  *
514  * n    The number of bytes to allocate.
515  *
516  * Returns:
517  *      A pointer to a BPatch_variableExpr representing the memory.
518  *
519  * If otherwise unspecified when binary rewriting, then the allocation
520  * happens in the original object.
521  */
522
523 BPatch_variableExpr *BPatch_addressSpace::mallocInt(int n, std::string name)
524 {
525    std::vector<AddressSpace *> as;
526    assert(BPatch::bpatch != NULL);
527    getAS(as);
528    assert(as.size());
529    void *ptr = (void *) as[0]->inferiorMalloc(n, dataHeap);
530    if (!ptr) return NULL;
531    if(name.empty()){
532       std::stringstream namestr;
533       namestr << "dyn_malloc_0x" << std::hex << ptr << "_" << n << "_bytes";
534       name = namestr.str();
535    }
536    BPatch_type *type = BPatch::bpatch->createScalar(name.c_str(), n);
537
538    return BPatch_variableExpr::makeVariableExpr(this, as[0], name, ptr,
539                                                 type);
540 }
541
542
543 /*
544  * BPatch_process::malloc
545  *
546  * Allocate memory in the thread's address space for a variable of the given
547  * type.
548  *
549  * type         The type of variable for which to allocate space.
550  *
551  * Returns:
552  *      A pointer to a BPatch_variableExpr representing the memory.
553  *
554  * XXX Should return NULL on failure, but the function which it calls,
555  *     inferiorMalloc, calls exit rather than returning an error, so this
556  *     is not currently possible.
557  */
558
559 BPatch_variableExpr *BPatch_addressSpace::mallocByType(const BPatch_type &type, std::string name)
560 {
561    std::vector<AddressSpace *> as;
562    assert(BPatch::bpatch != NULL);
563    getAS(as);
564    assert(as.size());
565    BPatch_type &t = const_cast<BPatch_type &>(type);
566    void *mem = (void *) as[0]->inferiorMalloc(t.getSize(), dataHeap);
567    if (!mem) return NULL;
568    if(name.empty()){
569       std::stringstream namestr;
570       namestr << "dyn_malloc_0x" << std::hex << mem << "_" << type.getName();
571       name = namestr.str();
572    }
573    BPatch_variableExpr *varExpr = BPatch_variableExpr::makeVariableExpr(this, as[0], name, mem, &t);
574    return varExpr;
575 }
576
577
578 /*
579  * BPatch_process::free
580  *
581  * Free memory that was allocated with BPatch_process::malloc.
582  *
583  * ptr          A BPatch_variableExpr representing the memory to free.
584  */
585
586 bool BPatch_addressSpace::freeInt(BPatch_variableExpr &ptr)
587 {
588   if(ptr.intvar)
589   {
590     // kill the symbols
591
592   }
593
594    ptr.getAS()->inferiorFree((Address)ptr.getBaseAddr());
595    return true;
596 }
597
598 BPatch_variableExpr *BPatch_addressSpace::createVariableInt(std::string name,
599                                                             Dyninst::Address addr,
600                                                             BPatch_type *type) {
601     assert(BPatch::bpatch != NULL);
602     std::vector<AddressSpace *> as;
603     getAS(as);
604     assert(as.size());
605
606 //dynC added feature
607     if(strstr(name.c_str(), "dynC") == name.c_str()){
608        void *mem = (void *) as[0]->inferiorMalloc(type->getSize(), dataHeap);
609        if (!mem) return NULL;
610        BPatch_variableExpr *varExpr = BPatch_variableExpr::makeVariableExpr(this, as[0], name, mem, type);
611        BPatch_module *mod = image->findOrCreateModule(varExpr->intvar->mod());
612        assert(mod);
613        mod->var_map[varExpr->intvar] = varExpr;
614        return varExpr;
615     }
616
617     BPatch_variableExpr *varExpr = BPatch_variableExpr::makeVariableExpr(this,
618                                                  as[0],
619                                                  name,
620                                                  (void *)addr,
621                                                  type);
622
623     return varExpr;
624 }
625
626 /*
627  * BPatch_addressSpace::findFunctionByAddr
628  *
629  * Returns the function that contains the specified address, or NULL if the
630  * address is not within a function.
631  *
632  * addr         The address to use for the lookup.
633  */
634 BPatch_function *BPatch_addressSpace::findFunctionByAddrInt(void *addr)
635 {
636    std::vector<AddressSpace *> as;
637
638    getAS(as);
639    assert(as.size());
640    std::set<func_instance *> funcs;
641    if (!as[0]->findFuncsByAddr((Address) addr, funcs)) {
642       // if it's a mapped_object that has yet to be analyzed,
643       // trigger analysis and re-invoke this function
644        mapped_object *obj = as[0]->findObject((Address) addr);
645        if (obj &&
646            !obj->isAnalyzed()) {
647          obj->analyze();
648          return findFunctionByAddrInt(addr);
649       }
650       return NULL;
651    }
652    if (funcs.empty()) return NULL;
653
654    if (funcs.size() > 1) {
655        bpwarn("Warning: deprecated function findFunctionByAddr found "
656               "multiple functions sharing address 0x%lx, picking one at "
657               "random.  Use findFunctionByEntry or findFunctionsByAddr\n",
658               addr);
659    }
660
661    return findOrCreateBPFunc((*(funcs.begin())), NULL);
662 }
663
664 /*
665  *  BPatch_addressSpace::findFunctionByEntry
666  *
667  *  Returns the function starting at the given address, or NULL if the
668  *  address is not within a function.
669  *
670  *  entry       The address to use for the lookup.
671  */
672 BPatch_function *BPatch_addressSpace::findFunctionByEntryInt(Address entry)
673 {
674     vector<BPatch_function*> funcs;
675     findFunctionsByAddr(entry, funcs);
676     vector<BPatch_function*>::iterator fit;
677     for (fit = funcs.begin(); fit != funcs.end(); fit++) {
678         if (entry == (Address)(*fit)->getBaseAddrInt()) {
679             return *fit;
680         }
681     }
682     return NULL;
683 }
684
685 bool BPatch_addressSpace::findFuncsByRange(Address startAddr,
686                                            Address endAddr,
687                                            std::set<BPatch_function*> &bpFuncs)
688 {
689     std::vector<AddressSpace *> as;
690     getAS(as);
691     assert(as.size());
692
693     // find the first code range in the region
694     mapped_object* mobj = as[0]->findObject(startAddr);
695     assert(mobj);
696     set<func_instance*> intFuncs;
697     mobj->findFuncsByRange(startAddr,endAddr,intFuncs);
698     set<func_instance*>::iterator fIter = intFuncs.begin();
699     for (; fIter != intFuncs.end(); fIter++) {
700         BPatch_function * bpfunc = findOrCreateBPFunc(*fIter,NULL);
701         bpFuncs.insert(bpfunc);
702     }
703     return 0 != bpFuncs.size();
704 }
705
706
707 /*
708  * BPatch_addressSpace::findFunctionsByAddr
709  *
710  * Returns the functions that contain the specified address, or NULL if the
711  * address is not within a function. (there could be multiple functions
712  * because of the possibility of shared code)
713  *
714  * addr         The address to use for the lookup.
715  * returns false if there were no functions that matched the address
716  */
717 bool BPatch_addressSpace::findFunctionsByAddrInt
718     (Address addr, std::vector<BPatch_function*> &funcs)
719 {
720     std::vector<AddressSpace *> as;
721     getAS(as);
722     assert(as.size());
723
724     // grab the funcs, return false if there aren't any
725     std::set<func_instance*> intfuncs;
726     if (!as[0]->findFuncsByAddr( addr, intfuncs )) {
727         return false;
728     }
729     // convert to BPatch_functions
730     for (std::set<func_instance*>::iterator fiter=intfuncs.begin();
731          fiter != intfuncs.end(); fiter++)
732     {
733         funcs.push_back(findOrCreateBPFunc(*fiter, NULL));
734     }
735     return 0 < funcs.size();
736 }
737
738
739 /*
740  * BPatch_addressSpace::findModuleByAddr
741  *
742  * Returns the module that contains the specified address, or NULL if the
743  * address is not within a module.  Does NOT trigger parsing
744  *
745  * addr         The address to use for the lookup.
746  */
747 BPatch_module *BPatch_addressSpace::findModuleByAddr(Address addr)
748 {
749    std::vector<AddressSpace *> as;
750    getAS(as);
751    assert(as.size());
752
753    mapped_object *obj = as[0]->findObject(addr);
754    if ( ! obj )
755        return NULL;
756
757    const pdvector<mapped_module*> mods = obj->getModules();
758    if (mods.size()) {
759        return getImage()->findOrCreateModule(mods[0]);
760    }
761    return NULL;
762 }
763
764
765 /*
766  * BPatch_addressSpace::insertSnippet
767  *
768  * Insert a code snippet at a given instrumentation point.  Upon success,
769  * returns a handle to the created instance of the snippet, which can be used
770  * to delete it.  Otherwise returns NULL.
771  *
772  * expr         The snippet to insert.
773  * point        The point at which to insert it.
774  */
775
776 BPatchSnippetHandle *BPatch_addressSpace::insertSnippetInt(const BPatch_snippet &expr,
777       BPatch_point &point,
778       BPatch_snippetOrder order)
779 {
780    BPatch_callWhen when;
781    if (point.getPointType() == BPatch_exit)
782       when = BPatch_callAfter;
783    else
784       when = BPatch_callBefore;
785
786    return insertSnippetWhen(expr, point, when, order);
787 }
788
789 /*
790  * BPatch_addressSpace::insertSnippet
791  *
792  * Insert a code snippet at a given instrumentation point.  Upon succes,
793  * returns a handle to the created instance of the snippet, which can be used
794  * to delete it.  Otherwise returns NULL.
795  *
796  * expr         The snippet to insert.
797  * point        The point at which to insert it.
798  */
799
800 // This handles conversion without requiring inst.h in a header file...
801 extern bool BPatchToInternalArgs(BPatch_point *point,
802       BPatch_callWhen when,
803       BPatch_snippetOrder order,
804       callWhen &ipWhen,
805       callOrder &ipOrder);
806
807
808 BPatchSnippetHandle *BPatch_addressSpace::insertSnippetWhen(const BPatch_snippet &expr,
809       BPatch_point &point,
810       BPatch_callWhen when,
811       BPatch_snippetOrder order)
812 {
813    BPatch_Vector<BPatch_point *> points;
814    points.push_back(&point);
815    return insertSnippetAtPointsWhen(expr,
816          points,
817          when,
818          order);
819 }
820
821 extern int dyn_debug_ast;
822
823 /*
824  * BPatch_addressSpace::insertSnippet
825  *
826  * Insert a code snippet at each of a list of instrumentation points.  Upon
827  * success, Returns a handle to the created instances of the snippet, which
828  * can be used to delete them (as a unit).  Otherwise returns NULL.
829  *
830  * expr         The snippet to insert.
831  * points       The list of points at which to insert it.
832  */
833
834 // A lot duplicated from the single-point version. This is unfortunate.
835 BPatchSnippetHandle *BPatch_addressSpace::insertSnippetAtPointsWhen(const BPatch_snippet &expr,
836                                                                     const BPatch_Vector<BPatch_point *> &points,
837                                                                     BPatch_callWhen when,
838                                                                     BPatch_snippetOrder order)
839 {
840   BPatchSnippetHandle *retHandle = new BPatchSnippetHandle(this);
841
842   if (dyn_debug_inst) {
843       BPatch_function *f;
844       for (unsigned i=0; i<points.size(); i++) {
845          f = points[i]->getFunction();
846          const char *sname = f->func->prettyName().c_str();
847          inst_printf("[%s:%u] - %d. Insert instrumentation at function %s, "
848                "address %p, when %d, order %d\n",
849                FILE__, __LINE__, i,
850                sname, points[i]->getAddress(), (int) when, (int) order);
851
852       }
853   }
854
855   if (BPatch::bpatch->isTypeChecked()) {
856       if (expr.ast_wrapper->checkType() == BPatch::bpatch->type_Error) {
857         fprintf(stderr, "[%s:%u] - Type error inserting instrumentation\n",
858                 FILE__, __LINE__);
859          expr.ast_wrapper->debugPrint();
860          return false;
861       }
862    }
863
864    if (!points.size()) {
865      fprintf(stderr, "%s[%d]:  request to insert snippet at zero points!\n", FILE__, __LINE__);
866       return false;
867    }
868
869    for (unsigned i = 0; i < points.size(); i++) {
870       BPatch_point *bppoint = points[i];
871
872       if (bppoint->addSpace == NULL) {
873          fprintf(stderr, "Error: attempt to use point with no process info\n");
874          continue;
875       }
876
877       if (dynamic_cast<BPatch_addressSpace *>(bppoint->addSpace) != this) {
878          fprintf(stderr, "Error: attempt to use point specific to a different process\n");
879          continue;
880       }
881
882       callWhen ipWhen;
883       callOrder ipOrder;
884
885       if (!BPatchToInternalArgs(bppoint, when, order, ipWhen, ipOrder)) {
886         fprintf(stderr, "[%s:%u] - BPatchToInternalArgs failed for point %d\n",
887                FILE__, __LINE__, i);
888          return retHandle;
889       }
890
891       /* PatchAPI stuffs */
892       instPoint* ipoint = bppoint->getPoint(when);
893       AddressSpace* ias = ipoint->proc();
894       PatcherPtr patcher = ias->patcher();
895       DynInsertSnipCommand::Ptr ins_snip = DynInsertSnipCommand::create(ipoint,
896               ipOrder, expr.ast_wrapper, BPatch::bpatch->isTrampRecursive());
897       miniTramp* mini = ins_snip->mini();
898       patcher->add(ins_snip);
899       /* End of PatchAPI stuffs */
900
901       if (mini) {
902         retHandle->mtHandles_.push_back(mini);
903         bppoint->recordSnippet(when, order, retHandle);
904       }
905    }
906    if (pendingInsertions == NULL) {
907      // There's no insertion set, instrument now
908      bool tmp;
909      finalizeInsertionSet(false, &tmp); //KEVINTODO: do we really want this?
910    }   
911
912    return retHandle;
913 }
914
915
916 /*
917  * BPatch_addressSpace::insertSnippet
918  *
919  * Insert a code snippet at each of a list of instrumentation points.  Upon
920  * success, Returns a handle to the created instances of the snippet, which
921  * can be used to delete them (as a unit).  Otherwise returns NULL.
922  *
923  * expr         The snippet to insert.
924  * points       The list of points at which to insert it.
925  */
926
927 BPatchSnippetHandle *BPatch_addressSpace::insertSnippetAtPoints(
928       const BPatch_snippet &expr,
929       const BPatch_Vector<BPatch_point *> &points,
930       BPatch_snippetOrder order)
931 {
932    return insertSnippetAtPointsWhen(expr,
933          points,
934          BPatch_callUnset,
935          order);
936 }
937
938 /*
939  * BPatch_addressSpace::isStaticExecutable
940  *
941  * Returns true if the underlying image represents a statically-linked executable, false otherwise.
942  */
943 bool BPatch_addressSpace::isStaticExecutableInt() {
944    std::vector<AddressSpace *> as;
945    getAS(as);
946
947    if( !as.size() ) return false;
948
949    AddressSpace *aout = as[0];
950    return aout->getAOut()->isStaticExec();
951 }
952
953 #include "registerSpace.h"
954
955 #if defined(cap_registers)
956 bool BPatch_addressSpace::getRegistersInt(std::vector<BPatch_register> &regs) {
957    if (registers_.size()) {
958        regs = registers_;
959        return true;
960    }
961
962    std::vector<AddressSpace *> as;
963
964    getAS(as);
965    assert(as.size());
966
967    registerSpace *rs = registerSpace::getRegisterSpace(as[0]);
968
969    for (unsigned i = 0; i < rs->realRegs().size(); i++) {
970        // Let's do just GPRs for now
971        registerSlot *regslot = rs->realRegs()[i];
972        registers_.push_back(BPatch_register(regslot->name, regslot->number));
973    }
974    regs = registers_;
975    return true;
976 }
977 #else
978 bool BPatch_addressSpace::getRegistersInt(std::vector<BPatch_register> &) {
979     // Empty vector since we're not supporting register objects on
980     // these platforms (yet)
981    return false;
982 }
983 #endif
984
985 #if defined(cap_registers)
986 bool BPatch_addressSpace::createRegister_NPInt(std::string regName,
987                                                BPatch_register &reg) {
988     // Build the register list.
989     std::vector<BPatch_register> dontcare;
990     getRegisters(dontcare);
991
992     for (unsigned i = 0; i < registers_.size(); i++) {
993         if (registers_[i].name() == regName) {
994             reg = registers_[i];
995             return true;
996         }
997     }
998     return false;
999 }
1000 #else
1001 bool BPatch_addressSpace::createRegister_NPInt(std::string,
1002                                                BPatch_register &)
1003 {
1004    return false;
1005 }
1006 #endif
1007
1008 bool BPatch_addressSpace::loadLibraryInt(const char * /*libname*/, bool /*reload*/)
1009 {
1010         return false;
1011 }
1012
1013 void BPatch_addressSpace::allowTrapsInt(bool allowtraps)
1014 {
1015    std::vector<AddressSpace *> as;
1016    getAS(as);
1017
1018    for (std::vector<AddressSpace *>::iterator i = as.begin(); i != as.end(); i++)
1019    {
1020       (*i)->setUseTraps(allowtraps);
1021    }
1022 }
1023
1024 BPatch_variableExpr *BPatch_addressSpace::createVariableInt(
1025                         Dyninst::Address at_addr,
1026                         BPatch_type *type, std::string var_name,
1027                         BPatch_module *in_module)
1028 {
1029    BPatch_binaryEdit *binEdit = dynamic_cast<BPatch_binaryEdit *>(this);
1030    if (binEdit && !in_module) {
1031       //Address alone isn't unique when binary rewriting
1032       return NULL;
1033    }
1034    if (!type) {
1035       //Required for size information.
1036       return NULL;
1037    }
1038    AddressSpace *ll_addressSpace = NULL;
1039
1040    std::vector<AddressSpace *> as;
1041    getAS(as);
1042    if (binEdit) {
1043       std::vector<AddressSpace *>::iterator as_i;
1044       for (as_i = as.begin(); as_i != as.end(); as_i++)
1045       {
1046          BinaryEdit *b = dynamic_cast<BinaryEdit *>(*as_i);
1047          assert(b);
1048          if (in_module->mod->obj() == b->getMappedObject()) {
1049             ll_addressSpace = *as_i;
1050             break;
1051          }
1052       }
1053    }
1054    else {
1055       assert(as.size() == 1);
1056       ll_addressSpace = as[0];
1057    }
1058
1059    if (!ll_addressSpace) {
1060       //in_module doesn't belong to 'this'
1061       return NULL;
1062    }
1063
1064    if (!var_name.size()) {
1065       std::stringstream namestream;
1066       namestream << "dyninst_var_" << std::hex << at_addr;
1067       var_name = namestream.str();
1068    }
1069
1070    return BPatch_variableExpr::makeVariableExpr(this, ll_addressSpace, var_name,
1071                                                 (void *) at_addr, type);
1072 }
1073