|
[分享]Windows用户态程序高效排错
目录是: === 第一部分,思考问题 1.0 热身运动 1.1 灰常奇怪的问题 1.2 稀疏平常的Session Lost 1.3 绝望的性能 1.4 本可以做得更好 第二部分,知识和工具 2.1 汇编 2.2 异常和通知 2.3 内存 2.4 区分层次 2.5 调试器和Windbg 2.6 同步和锁 2.7 调试和设计 暂告一个段落 === 前言和简介 本文介绍什么? 这是一篇介绍Windows系统上User Mode程序的排错 (troubleshooting) 方法和技巧的文章。 无论是开发,测试还是支持,都会遇上程序运行结果跟预期效果不一致的情况。找到问题的根源和解决的过程,就是排错。同时,如果问题发生的情况很特殊,比如特别难于重现,或者没有源代码可以参考,在这样的情况下解决问题,非常有挑战性! 后面的章节会通过例子来跟大家分享排错过程中的经验和技巧。 下面这些问题截取于本文后面要讨论的一些例子: l ASP.NET的程序在测试环境中一切正常,部署到生产环境中后,在压力比较大的时候,发生Session丢失现象。 (ASP.NET Session lost) l VC开发的程序运行一段时间后,偶尔发生内存访问错误,然后崩溃。 l 程序消耗的handle数量持续增长,内存使用也持续增长,最后性能下降非常厉害。 l VC程序中,使用ShellExecute打开一个本地的TXT文件。TXT格式默认打开方式关联到UltraEdit。发现在UltraEdit中除了打开这个TXT外,另外还打开了一个GIF文件。 问题可以表现得非常简单,或者非常复杂。可能涉及不同的开发工具和技术。如何分析解决,正是后面要讨论的。 本文的组织结构: 后面分三部分来解释 l 第一部分介绍最重要的,通用的思考方法。正确的思维方法能找出问题的核心,制定排错步骤和决定采用何种技术和工具进行研究。 l 第二部分介绍对排错非常有帮助的知识点和工具。包括调试器 (debugger),异常 (exception),内存工具,同步等等。选择恰当的工具,在恰当的时间,可以获取关于问题的关键信息。结合对应的知识就可以分析出问题的根源。 l 第三部分结合前两部分的内容,针对常见的几大类问题进行了总结。包括资源泄漏 (resource leak), 性能问题 (performance), 崩溃 (crash), CLR调试技巧和COM+调试技巧。 前两部分已经完成,正在整理。第三部分还没开始写。先把写好的给出来,听听大家的意见。 本文的使用方法和说明: 本文着重介绍思路,经验和工具。对于具体的知识点和工具,给出重点和方法,但是不会做详细的介绍,原因是MSDN的解释更加丰富和权威。具体信息可以参考文章中给出的链接。 在第一章阅读过程中,可能会发现某些术语,或者知识点并不熟悉。但是,这些并不会阻碍理解整体的思路。鼓励自主学习。通过Internet,都可以找到这些知识点的具体说明。同时,很多知识点会在第二章来说明。 内容组织上尽量先给现实的情况和问题,然后提供线索,然后分析,最后是结论和思考。目的是鼓励边阅读边思考。在拿到问题后,建议先自主分析如何去获取线索;拿到线索后,建议自主分析如何利用线索。 后面的案例都是这两年来亲手处理过的案例,但是处理的过程是在整个团队(微软大中华区开发语言和工具支持部)的帮助下完成的。没有整个团队的帮助,这些案例和这篇文章都无法完成。 我在处理这些问题的时候感觉很有趣,所以希望跟大家分享这种乐趣。在完成这篇文章的时候并不是严肃的,所以大家也别用严肃的眼光来看。希望大家当成一种趣味来阅读。 下面是文章中一些案例问题的节选: === 有一天,一个电话打进来,客户非常气愤地抱怨, 调用ShellExecute这个API,传入本地的一个文本文件的路径,在相同的机器上,有的时候会同时打开除了这个TXT以外的另一个不相干的文件!客户非常明确地告诉我,所有的参数肯定没有传错,而且ShellExecute的返回值也正确。 === 一段加密解密程序,首先由用户输入16个字节的原文,然后程序用固定的密钥加密生成密文,接着再对密文解密得到原来的原文,并且打印。问题的现象是,无论用户输入怎么样的原文,经过加密解密后,打印出来的原文的二进制都是一连串的0xcdcdcdcd === Windows SharePoint Portal 是运行在.NET Framework上的一个web应用程序。管理员可以设定使用英文界面或者中文界面。某一天一个客户抱怨SharePoint无法显示出中文界面。所有的页面都用英文显示。 === 客户开发一个性能很敏感的程序,想知道VC编译器对下面这段代码的优化做得怎么样: int hgt=4; int wid=7; for (i=0; i<hgt; i++) for (j=0; j<wid; j++) A[i*wid+j] = exp(-(i*i+j*j)); === 客户声称用VC开发的程序偶尔会崩溃。为了获取详细信息,客户激活了Dr. Watson,以便程序崩溃的时候可以自动获取dump文件进行分析。但是问题再次发生后,Dr. Watson并没有记录下来dump文件。 === 买了中文版的魔兽争霸,但家里的Windows却是英文版。中文的魔兽争霸必须要运行到中文的操作系统上,否则就报告操作系统语言不匹配,然后退出。怎么办呢?重装系统?去网上找破解?其实windbg就可以解决问题。 |
|
|
|
A Sample .NET DeProtector - whole assembly protection[转帖]
fusion hooking, delegation hooking In CLR world, fusion refers to the assembly loading phrase. Comparing with win32 world, fusion can be considers as DLL loader. The fusion manager is responsible for assembly probing (from GAC or Bin folder ect), loading into correct context, and then passing to jit phrase. I think fusion hooking means hooking the core functions to monitor assembly laoding. Delegation usually means callback mechalims in CLR world. I am not sure how delegation hooking helps depretector here. |
操作理由
RANk
{{ user_info.golds == '' ? 0 : user_info.golds }}
雪币
{{ experience }}
课程经验
{{ score }}
学习收益
{{study_duration_fmt}}
学习时长
基本信息
荣誉称号:
{{ honorary_title }}
能力排名:
No.{{ rank_num }}
等 级:
LV{{ rank_lv-100 }}
活跃值:
在线值:
浏览人数:{{ visits }}
最近活跃:{{ last_active_time }}
注册时间:{{ user_info.create_date_jsonfmt }}
勋章
兑换勋章
证书
证书查询 >
能力值