Convert access violation address to line no.
Convert access violation address to line no in Delphi 3.
It is possible to take the address given when an access violation occurs in a running application and convert this back to a unit name, function name and line number in source code. This is done using a detailed map file generated when application was compiled.
Creating a map file.
First you need to create map file for project as follows:
1. First make sure code optimization is switch off in Project|Options dialog Compiler tab.
2. Then switch on detailed map file in the Project|Options dialog Linker tab.
3. Now rebuild your application with a Project|Build All. A map file will be generated with the same name as the project.exe e.g. project.map.
Converting access violation address.
Now if you get a access violation here's how to convert the address given back to unit name, function name and source code line no using the a detailed map file:
1. Take the value in Project|Options dialog Linker tab Image Base and add $1000 to it, note both values are in hexadecimal. The resulting value is the offset:
Offset = Linker|Memory Sizes|Image Base value + $1000 (standard value is $401000)
Now subtract this offset from the address given in the access violation to give you the map file address:
Map file address = Access violation address - Offset
E.g. If a access violation address was $00437575 then
Offset = $400000 + $1000 = $401000 and
Map file address = $00437575 - $401000 = $36575.
2. Open the map file using any text editor, if using Delphi just open in IDE. Note the map file is just a text file.
3. Finding unit name. Within the map file find the section containing unit names under the heading of 'Detailed map of segments', at top of file after segment map. This section contains a list of segment, start address, length, other info and unit name in address order. This maps out where each compiled unit resides in memory. To find unit name where AV occurred find the map file address in the list using start address and length. E.g. If the AV map file address is $36575 the unit name would be 'Unit1' given by the following line in the map file:
0001:00035184 00001448 C=CODE S=.text G=(none) M=Unit1 ACBP=A9
Note: Unit1 is in address range $35184 up to $365CC = ($35184 + $1448) $1448 = length.
4. Finding function name. Within the map file there are two sections listing functions, one in function name order titled 'Publics by Name' and one in address order titled 'Publics by Value'. Both list segment, function start address and function name. To find the function name where the AV occurred use the 'Publics by Value' section locating the map file address in the list. The address may not match up exactly as the AV may have occurred anywhere in the function, so look for the highest address before the map file address, that is the start address of the function. E.g. if the AV. map file address is $36575 the function name would be 'TForm1.Button11Click' given the following lines in the map file:
0001:0003642C TForm1.Button9Click
0001:00036484 TForm1.Button10Click
0001:00036564 TForm1.Button11Click
0001:0003657C Finalization
0001:000365C4 Unit1
5. Finding line no. At the end of the map file there will be a section for each compiled unit giving the the source line no, segment and start address in line address order, four line no's to a line in the map file. These map compiled address to source code line no. Find the section for the unit name found in step (3). Then to find the line no where the AV occurred find the highest address before the map file address. E.g. if the AV map file address is $36575 the line no would be 367 given the following line in the map file for 'Line numbers for Unit1(Unit1.pas)':
366 0001:00036564 367 0001:00036570 368 0001:00036578 370 0001:000365CB
Conclusion.
You should have now the unit name, function name and source code line no where the given access violation occurred.
Note 1:
If the access violation occurred in code other than yours, e.g. in SysUtils you will not be able to determine the function and line no unless you compile your application using the VCL source code. This does not mean that the AV was caused by a bug in the VCL but the AV just surfaced within some VCL code, in all probability the cause of the AV is still your code calling the VCL incorrectly.
Note 2:
You must always use the map file created for the exe when trying to locate source of AV using this technique. Ever time you recompile the map file could change, so using say newly compiled map file against a exe compiled several version ago in a project you will get erroneous result.