Find the source code error line only through the crash address

Source: Internet
Author: User
Tags command line

What are some of the things that we are most worried about seeing as programmers? is a memory leak? Is the interface not good to see? ...... Wrong! I believe my view is that there will be no objections--that is, the program has crashed!

"The program has performed an illegal operation and is about to close. Please contact your software vendor. ", hehe, this sentence m$" famous words ", I am afraid the programmer is most worried about seeing things. Sometimes, your own program runs well on your own machine, but when it comes to someone else's machine, it breaks down, and sometimes I get the illegal operation in the process of writing and testing, but I can't determine which line in the source code is causing it ... Is it painful? It doesn't matter, this article can help you out of this predicament, even after you can proudly ask users to tell you the crash address, and then you can accurately locate the source code error in the line. (Is it amazing?) Oh. )

First of all, I must stress that this method can be used on any compiler currently available on the market. But I'm only familiar with m$ VC and MASM, so the next section only describes how to implement them in these two compilers, and ask readers to master the methods used on other compilers.

That's it, let's get started! :)

You must first generate the program's MAP file. What is a MAP file? Simply put, the MAP file is the only textual representation of the program's global symbols, source files, and code line number information, which can be used anywhere, anytime, without additional program support. And that's the only way to find out where the program crashed.

Well, now that the MAP file is so magical, how do we build it? In VC, we can press ALT+F7, open the Project Settings Options page, select the C + + tab, and enter the following project options:/zd, and then select the Link tab, at the bottom of project Options inside Input:/mapinfo:lines and/map:project_name.map. Finally press F7 to compile the build EXE executable and MAP file.

In MASM, we want to set compilation and connection parameters, which I usually do:

RC%1.rc

ML/C/coff/zd%1.asm

Link/subsystem:windows/mapinfo:exports/mapinfo:lines/map:%1.map%1.obj%1.res

Save it as a makem.bat, you can enter Makem filename at the command line to compile the build EXE executable and MAP file.

Let me first explain the meaning of the parameters added:

/ZD indicates that line information is generated at compile time

/map[:filename] represents the path and filename of the build map file

/mapinfo:lines represents adding line information when generating a MAP file

/mapinfo:exports indicates that when the MAP file is generated, add exported functions (if the DLL file is generated, this option will be added)

OK, we've got the MAP file through the steps above, so how do we use it?

Let's start with a simple example, please open your VC, create a new file like this:

01 //****************************************************************
02 //程序 名称:演示如何通过崩溃地址找出源代码的出错行
03 //作者:罗聪
04 //日期 :2003-2-7
05 //出处:http://www.luocong.com(老罗的缤纷天地)
06 //本 程序会产生“除0错误”,以至于会弹出“非法操作”对话框。
07 //“除0错误”只会在 Debug 版本下产生,本程序为了演示而尽量简化。
08 //注意事项:如欲转载,请保持本程序的完整,并注明:
09 //转载自 “老罗的缤纷天地”(http://www.luocong.com)
10 //****************************************************************
11
12 void Crash(void)
13 {
14 int i = 1;
15 int j = 0;
16 i /= j;
17 }
18
19 void main(void)
20 {
21 Crash();
22 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.