addrtranslate-win.C

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 #include <stdio.h>
00032 #define WIN32_LEAN_AND_MEAN
00033 #include <windows.h>
00034 
00035 #include <vector>
00036 #include <string>
00037 
00038 #include "common/h/addrtranslate.h"
00039 #include "Psapi.h"
00040 
00041 using namespace std;
00042 
00043 namespace Dyninst {
00044 
00045 class AddressTranslateWin : public AddressTranslate
00046 {
00047 private:
00048    bool no_proc;
00049 public:
00050    virtual bool init();
00051    virtual bool refresh();
00052    AddressTranslateWin(PID pid, PROC_HANDLE phandle);
00053    void setNoProc(bool b);
00054    virtual Address getLibraryTrapAddrSysV();
00055 
00056 };
00057 
00058 }
00059 
00060 using namespace Dyninst;
00061 
00062 void AddressTranslateWin::setNoProc(bool b)
00063 {
00064    no_proc = b;
00065 }
00066 
00067 AddressTranslate *AddressTranslate::createAddressTranslator(PID pid_, ProcessReader *, SymbolReaderFactory *, PROC_HANDLE phandle_, std::string, Address)
00068 {
00069     AddressTranslateWin *new_translate = new AddressTranslateWin(pid_, phandle_);
00070     if (!new_translate)
00071         return NULL;
00072     if (new_translate->creation_error)
00073         return NULL;
00074     return new_translate;
00075 }
00076 
00077 AddressTranslate *AddressTranslate::createAddressTranslator(ProcessReader *, SymbolReaderFactory *, std::string, Address)
00078 {
00079     //return createAddressTranslator(GetCurrentProcess(), NULL);
00080     return createAddressTranslator(GetCurrentProcessId(), NULL, NULL, GetCurrentProcess());
00081 }
00082 
00083 bool AddressTranslateWin::init()
00084 {
00085     bool result = refresh();
00086     if (!result)
00087         creation_error = true;
00088     return true;
00089 }
00090 
00091 void printSysError(unsigned errNo) {
00092     char buf[1000];
00093     DWORD result = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errNo, 
00094           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00095           buf, 1000, NULL);
00096     if (!result) {
00097         fprintf(stderr, "Couldn't print error message\n");
00098         printSysError(GetLastError());
00099     }
00100     fprintf(stderr, "*** System error [%d]: %s\n", errNo, buf);
00101     fflush(stderr);
00102 }
00103 
00104 bool AddressTranslateWin::refresh()
00105 {
00106     HANDLE currentProcess = phandle;
00107     int result;
00108 
00109    if (no_proc)
00110       return true;
00111 
00112    for (unsigned i=0; i<libs.size(); i++)
00113       if (libs[i]) delete libs[i];
00114     libs.clear();
00115    
00116     //Calculate the number of modules
00117     DWORD num_modules_needed, total_space;
00118    result = EnumProcessModules(currentProcess,
00119                                NULL,
00120                                0,
00121                                &total_space);
00122     if (!result) {
00123         return false;
00124     }
00125     num_modules_needed = total_space / sizeof(HMODULE);
00126     
00127     //Get modules
00128     HMODULE* loadedModules = new HMODULE[num_modules_needed];
00129    result = EnumProcessModules(currentProcess,
00130                                loadedModules,
00131                                sizeof(HMODULE) * num_modules_needed,
00132                                &total_space);
00133     if (!result) {
00134       printSysError(GetLastError());
00135         return false;
00136     }
00137    
00138     //Add modules to libs
00139     for (HMODULE *i = loadedModules; i < loadedModules + num_modules_needed; i++)
00140     {
00141         MODULEINFO info;
00142         TCHAR filename[MAX_PATH];
00143         
00144         result = GetModuleInformation(currentProcess, *i, &info, sizeof(info));
00145         if (!result)
00146             continue;
00147       result = GetModuleFileNameEx(currentProcess, *i, filename, MAX_PATH);
00148       if (!result)
00149          continue;
00150       
00151       LoadedLib *ll = new LoadedLib(std::string(filename), (Address) info.lpBaseOfDll);
00152       ll->add_mapped_region((Address) info.lpBaseOfDll, info.SizeOfImage);
00153       libs.push_back(ll);
00154     }
00155     
00156     delete [] loadedModules;
00157     return true;
00158 }
00159 
00160 vector< pair<Address, unsigned long> > *LoadedLib::getMappedRegions()
00161 {
00162    return &mapped_regions;
00163 }
00164 
00165 AddressTranslateWin::AddressTranslateWin(PID pid, PROC_HANDLE phandle_) :
00166     AddressTranslate(pid, phandle_),
00167    no_proc(false)
00168 {
00169     init();
00170 }
00171 
00172 Address AddressTranslateWin::getLibraryTrapAddrSysV()
00173 {
00174    return 0x0;
00175 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1