//============================================================================ // getLineForAddress //============================================================================ public void getLineForAddress(uint pAddr, ref string filePath, ref int lineNum) { char[] ByteArray = new char[10000]; DbgHelp._IMAGEHLP_LINE64 lineinfo = new DbgHelp._IMAGEHLP_LINE64(); uint displacement = 0; bool result = true; unsafe { fixed(char *p = ByteArray) { IntPtr MyIntPtr = (IntPtr)p; lineinfo.SizeOfStruct = (uint)DbgHelp._IMAGEHLP_LINE64.getSize(); lineinfo.FileName = MyIntPtr; result = DbgHelp.SymGetLineFromAddr64(hProcess, (UInt64)pAddr, ref displacement, ref lineinfo); } } if (result) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(ByteArray); filePath = sb.ToString(); lineNum = (int)lineinfo.LineNumber; } else { Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); filePath = "[unknown]"; lineNum = 0; } }
//============================================================================ // close //============================================================================ public void close() { DbgHelp.SymUnloadModule64(hProcess, pvModuleBase); DbgHelp.SymCleanup(hProcess); }
//============================================================================ // load //============================================================================ public bool load(string filename) { DbgHelp.SymSetOptions(DbgHelp.SymOpt.UNDNAME); hProcess = new IntPtr(0); if (!DbgHelp.SymInitialize(hProcess, "", false)) { Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); return(false); } IntPtr pZero = new IntPtr(0); DbgHelp.MODLOAD_DATA modLoadDat = new DbgHelp.MODLOAD_DATA(); pvModuleBase = (UInt64)DbgHelp.SymLoadModuleEx(hProcess, pZero, filename, null, 0, 0, modLoadDat, 0); if (pvModuleBase == 0) { Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); return(false); } pvModuleBase = (UInt64)((UInt32)(pvModuleBase)); //enumerate symbols DbgHelp.SymEnumSymbolsProc enumSymbolsCB = new DbgHelp.SymEnumSymbolsProc(this.enumSymbolsCallback); System.Runtime.InteropServices.GCHandle gch = System.Runtime.InteropServices.GCHandle.Alloc(enumSymbolsCB); IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(enumSymbolsCB); DbgHelp.SymEnumSymbolsProc cb = null; bool OK = true; unsafe { // cb = (ip.ToPointer() as DbgHelp.SymEnumSymbolsProc); // OK = DbgHelp.SymEnumSymbols(hProcess, pvModuleBase, null, cb, pZero);//pvModuleBase); } if (!OK) { Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); return(false); } while (true) { } ; //enumerate lines //OK = DbgHelp.SymEnumLines(hProcess, pvModuleBase, null, null, enumLinesCallback, hProcess); //if (!OK) //{ // Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); // return false; //} return(true); }
//============================================================================ // load //============================================================================ public bool init(int procHandle, string filename) { hProcess = (uint)procHandle; bool dointrusive = false; if (mDoEnumerate) { dointrusive = false; DbgHelp.SymSetOptions(DbgHelp.SymOpt.UNDNAME); } else { dointrusive = false;// true; DbgHelp.SymSetOptions(DbgHelp.SymOpt.LOAD_LINES); } string pathToPDB = Path.GetDirectoryName(filename); if (!DbgHelp.SymInitialize(hProcess, pathToPDB, dointrusive)) { Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); return(false); } IntPtr pZero = new IntPtr(0); DbgHelp.MODLOAD_DATA modLoadDat = new DbgHelp.MODLOAD_DATA(); /////////////////////////////////////////////// //load module pvModuleBase = (UInt64)DbgHelp.SymLoadModuleEx(hProcess, pZero, filename, null, 0, 0, modLoadDat, 0); if (pvModuleBase == 0) { Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); return(false); } pvModuleBase = (UInt64)((UInt32)(pvModuleBase)); /////////////////ENUMERATION if (mDoEnumerate) { /////////////////////////////////////////////// //enumerate lines bool OK = DbgHelp.SymEnumLines(hProcess, pvModuleBase, null, null, enumLinesCallback, pZero); if (!OK) { Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); return(false); } mAddressHash.sortList(); /////////////////////////////////////////////// //enumerate symbols //OK = DbgHelp.SymEnumSymbols(hProcess, pvModuleBase, null, enumSymbolsCallback, pZero);//pvModuleBase); //if (!OK) //{ // Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); // return false; //} } return(true); }
public bool lookupImmediate(UInt32 address, ref LookupInfo info) { info.mAddress = address; info.mLine = -1; info.mFilename = "?"; info.mSymbol = "?"; info.mFoundLine = false; info.mFoundSymbol = false; /////////////////////////////////////////////////// //lookup filename byte[] fName = new byte[1000]; uint displacement = 0; DbgHelp._IMAGEHLP_LINE64 lineinfo = new DbgHelp._IMAGEHLP_LINE64(); lineinfo.SizeOfStruct = (uint)DbgHelp._IMAGEHLP_LINE64.getSize(); unsafe { fixed(byte *pCst = fName) { //IntPtr ptr = new IntPtr() lineinfo.FileName = new IntPtr(pCst); info.mFoundLine = DbgHelp.SymGetLineFromAddr64(hProcess, (UInt64)address, ref displacement, ref lineinfo); } } if (!info.mFoundLine) { Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); return(false); } else { info.mLine = (int)lineinfo.LineNumber; info.mFilename = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(lineinfo.FileName); } /////////////////////////////////////////////////// //lookup symbol ulong pdwDisplacment = 0; DbgHelp.SYMBOL_INFO symbInfo = new DbgHelp.SYMBOL_INFO(); symbInfo.SizeOfStruct = (uint)DbgHelp.SYMBOL_INFO.getSize(); symbInfo.MaxNameLen = (int)DbgHelp.SYMBOL_INFO.getmaxCharSize(); info.mFoundSymbol = DbgHelp.SymFromAddr(hProcess, address, ref pdwDisplacment, ref symbInfo); if (!info.mFoundSymbol) { Int32 errCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); return(false); } else { info.mSymbol = symbInfo.Name; info.mFoundSymbol = true; } return(true); }
//============================================================================ // close //============================================================================ public void deinit() { DbgHelp.SymUnloadModule64(hProcess, pvModuleBase); DbgHelp.SymCleanup(hProcess); mAddressHash = null; }