[Delphi] use the map file to find the program address class error line

Source: Internet
Author: User

No intention of discovering the use of map files during csdn wandering, and recording them for your learning. K5z flat Software Park
K5z flat Software Park
Identify the source code error line through the crash address only k5z flat Software Park
I believe many people have encountered the "Access XXXXX" address error during code execution. Under normal circumstances, we cannot trace the error code lines based on the error information. We can only view the code one by one. In particular, your software has been released, and you just started to run OK, but errors may pop out from time to time-it is really a crash. But do you know? We already have methods to track errors, but you never know. K5z flat Software Park
K5z flat Software Park
Map File k5z flat Software Park

What is a map file? In short, a map file is the unique text representation of the program's global symbols, source files, and code line numbers. It can be used anywhere and anytime, no additional programs are required. K5z flat Software Park
How to generate a map file in Delphi: I only know the following two methods. If anyone knows other methods, please kindly advise thanks to k5z flat Software Park.
Method for generating detailed map information k5z flat Software Park
1. Project-> options-> linker-> map file select detailed. k5z flat Software Park
2. D:/Fred/code/Delphi/mypas/errlinebyaddr2> dcc32-Gd project1.dpr (Ha, I don't understand this, please first) k5z flat Software Park

The following example shows the functions of map files. K5z flat Software Park
Our program: k5z flat Software Park
Unit unit1; k5z flat Software Park
K5z flat Software Park
Interfacek5z flat Software Park
K5z flat Software Park
Usesk5z flat Software Park
Windows, messages, sysutils, variants, classes, graphics, controls, forms, k5z flat Software Park
Dialogs, stdctrls; k5z flat Software Park
K5z flat Software Park
Typek5z flat Software Park
Tform1 = Class (tform) k5z flat Software Park
Btn1: tbutton; k5z flat Software Park
Btn2: tbutton; k5z flat Software Park
Edt1: tedit; k5z flat Software Park
Procedure btn1click (Sender: tobject); k5z flat Software Park
Procedure btn2click (Sender: tobject); k5z flat Software Park
Privatek5z flat Software Park
{Private Declarations} k5z flat Software Park
Publick5z flat Software Park
{Public declarations} k5z flat Software Park
End; k5z flat Software Park
K5z flat Software Park
Vark5z flat Software Park
Form1: tform1; k5z flat Software Park
K5z flat Software Park
Implementationk5z flat Software Park
K5z flat Software Park
{$ R *. DFM} k5z flat Software Park
{Reference csnd-related articles in this example} k5z flat Software Park
Procedure tform1.btn1click (Sender: tobject); k5z flat Software Park
Vark5z flat Software Park
P: pchar; k5z flat Software Park
Begink5z flat Software Park
P: = nil; k5z flat Software Park
P ^: = 'a'; // address error will occur in k5z flat Software Park
End; k5z flat Software Park
K5z flat Software Park
// Because the map file involves a hex computing problem when searching for incorrect rows, I specially added an edit box for the test to calculate the final value k5z flat Software Park.
Procedure tform1.btn2click (Sender: tobject); k5z flat Software Park
Begink5z flat Software Park
Edt1.text: = inttohex (strtoint ('$' + edt1.text)-strtoint ('$00400000')-strtoint ('$00001000'), 8); k5z flat Software Park
End; k5z flat Software Park
K5z flat Software Park
End. k5z flat Software Park
Run your program and click btn1 to report an address class error. The following is the description on my machine: k5z flat Software Park.
Access violation at address 0044f8d1 in module 'project1.exe '. Write of address 00000000. k5z flat Software Park
K5z flat Software Park
The error address is 0044f8d1. K5z flat Software Park
K5z flat Software Park
Next we will find the corresponding code lines in the Delphi source code file based on this address. K5z flat Software Park
If the setting is correct, a 'Project. map' file will be generated in the same directory after the program runs and your map file will be opened. K5z flat Software Park
The file format is divided into several paragraphs, you can see: k5z flat Software Park
K5z flat Software Park
0002: 00002970 _ uninitializeflatsbk5z flat Software Park
0002: 20172b64 _ winnlsenableimek5z flat Software Park
K5z flat Software Park
K5z flat Software Park
Address publics by value // in this section, you can find the incorrect function name k5z flat Software Park Based on the address.
K5z flat Software Park
0002: ffbb0010 tlslastk5z flat Software Park
0001: 000001f4 getstdhandlek5z flat Software Park
0001: 000001fc raiseexceptionk5z flat Software Park
000:00000204 rtlunwindk5z flat Software Park
0001: 0000020c unhandledexceptionfilterk5z flat Software Park

---- Omitted ---- k5z flat Software Park
K5z flat Software Park
Line numbers for unit1 (unit1.pas) segment. textk5z flat Software Park
K5z flat Software Park
32 0001: 0004e8c8 33 0001: 0004e8c9 34 0001: 0004e8ce 35 0001: 0004e8d4k5z flat Software Park
38 0001: 0004e8d8 39 0001: 0004e8f4 40 0001: 0004e978 42 0001: 0004e9e4k5z flat Software Park
42 0001: 0004e9ebk5z flat Software Park

...... K5z flat Software Park
Note: k5z flat Software Park in line numbers line format
K5z flat Software Park
32 0001: 0004e8c8 k5z flat Software Park
K5z flat Software Park
The first number indicates the code line number in the source code, and the second number indicates the offset of the code line in the code segment to which it belongs. K5z flat Software Park
If you want to find the code row number, you need to use the following formula to perform some hexadecimal subtraction operations: k5z flat Software Park

Crash line offset = crash address-base address (imagebase address)-0x1000 k5z flat Software Park
K5z flat Software Park
Why? The crash address we get is obtained from the offset address + base address, so we need to subtract the base address when calculating the row number. Generally, the base address value is 0x00400000. In addition, because the code segments of a PE file generally start from 0x1000 offset, 0x1000 must be subtracted. K5z flat Software Park
(For the base address, you can use project-> options to obtain an image base value on the page.) k5z flat Software Park
K5z flat Software Park
If you do not know how to calculate the hexadecimal format, follow the code in btn2 above and enter the D1 crash line address => k5z flat Software Park
Get 0004e8d1k5z flat Software Park
Further Look: k5z flat Software Park
Line numbers for unit1 (unit1.pas) segment. textk5z flat Software Park
K5z flat Software Park
32 0001: 0004e8c8 33 0001: 0004e8c9 34 0001: 0004e8ce 35 0001: 0004e8d4k5z flat Software Park
38 0001: 0004e8d8 39 0001: 0004e8f4 40 0001: 0004e978 42 0001: 0004e9e4k5z flat Software Park
42 0001: 0004e9ebk5z flat Software Park
Find the number of equal to or less than 0004e8d1 (the nearest number, and cannot be greater than) --> 34 0001: 0004e8ce. k5z flat Software Park
The error line in the source code is 34!

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.