[译]使用BlackBox捕获错误
By robot-v1.0
本文链接 https://www.kyfws.com/applications/trapping-bugs-with-blackbox-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3922 个词 阅读量 0[译]使用BlackBox捕获错误
原文地址:https://www.codeproject.com/Articles/2679/Trapping-Bugs-with-BlackBox
原文作者:Jim Crafton
译文由本站 robot-v1.0 翻译
前言
A brief article explaining how to use BlackBox in your programs to trap errors
简短的文章解释了如何在程序中使用BlackBox来捕获错误
介绍(Introduction)
您是否曾经在客户的计算机上使应用程序崩溃,并且只有"它崩溃并显示了一个有趣的消息框"才能继续尝试找出错误所在?您是否曾经希望您可以在客户计算机上放置某种"看门狗"来监视您的应用程序,然后在该应用程序崩溃时自动了解崩溃发生的位置,并允许客户或将这些信息邮寄给您?如果是这样,那么BlackBox可能正是您想要的!(Have you ever had an application crash on a customer’s machine and have only, “it just crashes and displays a funny message box”, to go on in attempting to track down what the error was? Ever wish you could put some kind of “watchdog” that would monitor your application on the customers computer and then if it crashed, automatically scoop up where the crash happened, and allow the customer or mail this information to you? If so then BlackBox may be just what you’re looking for!)
BlackBox(此后我发现它是Linux窗口管理器的名称,因此,如果有人对更好的名称有任何想法,请随时告诉我:))是一个dll,您只需加载它,然后就可以.加载库后,它将设置一个崩溃处理程序(在(BlackBox (which I have since discovered is the name of a window manager for linux, so if anyone has any ideas about a better name, please feel free to let me know :) ) is a dll that you simply load up and then forget about. It will set a crash handler as soon as the library is loaded (in) DLLMain()
),然后耐心等待应用程序崩溃.如果和/或当您的应用程序崩溃时,它会显示一个类似于Netscape的TalkBack的信息,或者显示新的错误报告对话框,而不仅仅是显示诸如"错误地写入内存地址0XDEADBEEF"之类的错误消息.随新版本的Microsoft Internet Explorer弹出.该对话框允许用户复制或保存崩溃数据,然后通过电子邮件发送或发送到相应的网站,他们可以在该网站上提交信息.然后,程序员可以使用此信息来更好地了解问题发生的确切位置以及有关崩溃的计算机的信息.(), and then just waits patiently for your application to crash. If and/or when your application crashes, instead of just displaying an error message like “Error writing to memory address 0XDEADBEEF”, a nice little dialog comes up that displays the information in a fashion similar to Netscape’s TalkBack, or the new error reporting dialog that pops up with the newer version of Microsoft’s Internet Explorer. This dialog allows the user to then copy or save the crash data, and then email or be taken to an appropriate website where they can submit the information. This information can then be used by a programmer to have a much better idea of exactly where the problem occurred, as well as information about the machine that it crashed on.)
作为仅32 Kb大小的DLL,它的重量非常轻,因此,如果您决定使用它,则不必担心膨胀!(It is extremely lighweight as a DLL at only 32 Kb in size, so there is no worry about bloat if you decide to use this!)
客制化(Customization)
您可以通过打开BlackBox.cpp并编辑以下代码来自定义主标签中显示的文本:(You can customize the text that is displayed in the main label, by opening BlackBox.cpp and editing the following code:)
const char* g_errorLable =
"This is my version of BlackBox - An error has occured!
You are screwed";
通过更改(By changing the) g_errorLable
变量,这将更改对话框顶部标签中显示的内容.(variable, this will change what is displayed in the label in the top area of the dialog.)
此外,您可以自定义电子邮件地址,以在单击"邮件至…“按钮时使用,以及单击"提交错误…“按钮时使用的URL.再次打开BlackBox.cpp并编辑以下代码:(In addition, you can customize the email address to use when the “Mail to…” button is clicked, and the URL that is used when the “Submit bug…” button is clicked. Again open BlackBox.cpp and editing the following code:)
const char* g_mailToAddress=
//change this to something useful
"mailto:me@wherever.com"
const char* g_submitBugURL=
//change this to something useful
"http://www.mybugtrackingpage.html"
用法(Usage)
BlackBox显示各种信息:(BlackBox displays a variety of information:)
- 例外原因(The Exception Reason)
- 寄存器(The Registers)
- 堆栈跟踪,发生错误的完整堆栈跟踪(The Stack Trace, a complete stack trace from where the error occured)
- 机器信息,例如CPU,OS版本,物理内存等.单击"信息…“按钮将在对话框中显示该信息.(The machine information, such as CPU, OS version, physical memory, etc. Clicking on the “Information…” button will display this in a dialog.)
- 计算机状态-崩溃时正在运行的所有进程的列表,以及这些进程加载的所有DLL.单击"状态…“按钮将在对话框中显示.(The machine state - a list of ALL the processes running at the time of the crash, and any DLLs loaded by the processes. Clicking on the “State…” button will display this in a dialog.) 作为用户,您可以通过单击"复制"按钮将内容复制到剪贴板.单击"保存…“按钮将提示您将内容保存到文件,默认名称的格式为:” errorlog- <GMT时间,dd-mm-yyyy>(<时间,hh:mm: ss>).log”.(As a user, you can copy the contents to the clipboard by clicking on the “Copy” button. Clicking the “Save…” button will prompt you to save the contents to a file, with the default name being of the form: “errorlog-<GMT time, dd-mm-yyyy>(<time, hh:mm:ss>).log”.)
单击"邮件发送到…“按钮,将尝试在系统上执行mailto:命令.假设正确安装了电子邮件程序,它将显示适当的电子邮件地址.有关配置这两个功能的信息,请参见上文.(Click the “Mail To…” button and an attempt will be made to execute the mailto: command on your system. Assuming a properly installed email program, it will be brought up with the appropriate email address. See above for configuring these two features.)
约翰罗宾斯(John Robbins)的优秀著作"窃取了"代码的基础(*The basics of the code is "stolen" from John Robbins excellent book*) [调试应用程序(*Debugging Applications*)](http://www.microsoft.com/MSPress/books/4023.asp) 展示了如何进行各种恶意的底层操作,其中包括执行堆栈跟踪.为了获得堆栈跟踪以提供有用的信息,您可能必须对发布设置的构建设置进行一些修改.堆栈跟踪依赖于某些系统调用以及可执行文件本身中的信息.如果可执行文件是发行版本,则几乎没有调试信息,因此,堆栈跟踪将几乎不显示任何内容,至少对我来说,这是我编写此工具的关键原因之一.为了使符号引擎成功显示信息,您需要在链接器设置中启用调试信息(/debug)的生成(您无需在代码生成或C ++设置中执行此操作),并且需要告知链接器以生成一个映射文件(/map [" filename"]).然后,在分发您的应用程序时,请确保也将(*that show how to do all sorts of gory low level things, among them performing a stack trace. To get the stack trace to provide something useful, however, you may have to make some modifications to your build settings for release builds. The stack tracing relies on certain system calls, as well as information in the executable itself. If the executable is a release build, then there will be almost no debug information, and as a result, your stack trace will show almost nothing, which for me, at least, is one of the critical reasons why I wrote this tool. To allow the symbol engine to successfully display information, you need to enable the generation of debug information (/debug) in the linker settings (you do NOT need to do this in your code generation or C++ settings), and you need to tell the linker to generate a mapfile (/map ["filename"] ). Then when you distribute your application make sure to also put the*)
BlackBox.dll和映射文件(通常,该映射文件与可执行文件同名,但带有"(*and the map file(typically the map file is the same name as the executable, but with a "*)
.map` 扩展名)与exe放在同一目录中.显然,这确实意味着您正在向人们(即您的客户)“公开"更多信息,但是要权衡的是可读的堆栈跟踪信息,这对于确定崩溃发生的位置非常有用. .BlackBox仍可与不提供映射文件或链接器调试信息的可执行文件一起使用,只是不会显示太多信息.(” extension) in the same directory as your exe. Obviously this does mean you are “exposing” a bit more information to people (i.e. your customers), but the tradeoff is readable stack trace information that can be invaluable in locating where a crash happened. BlackBox will still work with an executable that does not supply a map file or linker debug information, it will just not display as much information.)
要在您的应用程序中实际使用它,您只需执行以下操作:(To actually use this in your application, you only need to do the following:)
HINSTANCE hLib = LoadLibrary( "BlackBox.dll" );
而已!为了成为一名优秀的Win32公民,您应该在应用退出时卸载该库:(That’s it! To be a good Win32 citizen, you should unload the library when your app exits:)
if ( NULL != hLib ) {
FreeLibrary( hLib );
}
其他所有内容都在BlackBox库中内部处理.(Everything else is handled internally in the BlackBox library.)
最后一点:要构建此代码,您将需要最新的Platform SDK(至少从去年开始).另外,当您将它放到野外时,可以这么说,您需要分发(One final note: To build this code you will need the latest Platform SDK (at least from the last year or so). In addition, when you put this out in the wild, so to speak, you will need to distribute the) psapi.dll
和(and the) dbghelp.dll
dll可以正常工作,它们是允许符号引擎正常工作的一部分.(dll’s for this to all work, they are part of what allows the symbol engine to work.)
去做:(To Do:)
在不久的将来,我想增加对数据加密的支持,现在它只是纯文本,在某些情况下可能不合适.我还想编写一个在计算机上运行的服务器程序,BlackBox客户端程序可以通过套接字简单地将加密数据发送到该服务器程序.(In the near future I would like to add support for encrypting the data, right now it is just plain text, and this may not be appropriate in certain cases. I would also like to write a server program that would run on a machine, and to which the BlackBox client program could simply send the encrypted data via sockets.)
进一步定制:(Further Customization:)
如果您希望进行更多的根本性定制,则核心UI代码都包含在BlackBoxUI.cpp中.这就是分配所有内存的步骤,使用(Should you wish to do more radical customizations, the core UI code is all contained in the BlackBoxUI.cpp. This is what allocates all the memory, which is done using) GlobalXXX
函数,因为我们可能无法使用本地堆.所有这些的起点是(functions, as we may not be able to use the local heap. The starting point for all of this is the) ShowBlackBoxUI()
功能,设置所有信息,然后显示主对话框.(function, which sets up all the information and then displays the main dialog.)
学分:(Credits:)
约翰`罗宾斯(John Robbins),调试应用程序,Microsoft Press;国际标准书号(ISBN):0735608865,2000(, Debugging Applications, Microsoft Press; ISBN: 0735608865, 2000)
看到(See) 这个(this) 有关Wintellect的John Robbins的更多信息.(for more information about John Robbins at Wintellect.)
各个过程的显示代码基于Emmanuel Kartmann在他的文章中所做的工作(The display code for the various processes was based on work that Emmanuel Kartmann did in his article) 显示加载的模块(Display Loaded Modules)
更新:(Updates:)
BlackBox dll是为以下工具创建的工具集的一部分(The BlackBox dll is part of the toolset created for the) 视觉组件框架(Visual Component Framework) .有关进一步的更新,请参见VCF项目页面,并检查CVS状态或查看(. For further updates please see the VCF project page and either check the CVS status or look at the) 文件部分(files section) 最新的BlackBox文件版本.(for the newest BlackBox file release.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C++ VC6 WinXP Windows Win2K Visual-Studio Dev 新闻 翻译