首页 > 学院 > 开发设计 > 正文

Windbg调试托管代码

2019-11-17 02:34:41
字体:
来源:转载
供稿:网友
Windbg调试托管代码
  1. Windbg调试.net托管代码需要借助于SOS.dll,.Net 4.0的32位sos.dll的路径在C:/Windows/Microsoft.NET/Framework/v4.0.30319,64为的路径在C:/Windows/Microsoft.NET/Framework64/v4.0.30319,加载哪个版本的sos.dll要与调试的应用程序的位数一致。关于SOS调试扩展的详细介绍请参考:https://msdn.microsoft.com/zh-cn/library/bb190764(v=vs.100).aspx
  2. Windbg的可以通过Open Executable直接在windbg中启动程序调试,也可以通过Attach to a PRocess attach到一个已经启动的进程进行调试,本例使用Open Executable方式调试程序
  3. 示例代码
    static void Main(string[] args)        {                   System.IO.FileSystemInfo fileSystemPath = GetFileSystemPath();            if (!fileSystemPath.Exists)            {                throw new System.IO.FileNotFoundException(fileSystemPath.FullName);            }                     Console.ReadLine();        } private static FileSystemInfo GetFileSystemPath()        {            FileInfo fileInfo = new FileInfo(@"x:/Dev/TempLibrary.mdb");                 return fileInfo;        }
  4. 在Windbg中选择Open Executable打开应用程序,image
  5. 如何在某个函数中设置断点:
    • Open Executable的调试方法首先要执行sxe ld:clrjit命令,该命令的作用是表示当程序加载完clrjit.dll后中断到调试器,即在调试器中中断运行
    • 执行g命令,运行程序,当程序加载完clrjit.dll时就会中断
    • 执行.loadby sos clr命令加载扩展信息
    • 执行lm命令查看加载的所有模块,lmvm可以查看某个模块的详细信息,本例中执行lmvm Console1image
    • 执行!Name2EE <模块名> <类型名或方法名> 命令获取模块中指定类型或方法的MethodTable结构和EEClass结构,本例中获取Program类的信息image
    • 调用DumpMT –md <MethodTable地址>获取类中方法信息,本例中methodtable地址由name2ee命令获取image
    • 执行!bpmd <模块名> <方法名>设置断点,本例中执行!bpmd Console1 Console1.Program.GetFileSystemPathimage
    • 执行命令g,运行到断点就会停止
  6. 如何查看变量信息:
    • !clrstack -a命令显示托管函数的参数和局部变量信息image
    • !dumpheap –type 类型:显示堆中某类型变量的地址信息,然后使用!dumpobj address显示变量信息
  7. 如何分析内存泄露:
    • 执行!dumpheap命令可以打印出当前所有类型所占内存空间的大小image

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表