Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to determine corresponding source line from offset number

0.00/5 (No votes)
22 Jul 2003 1  
If you know offset number within exe file, you can specify where it corresponds in source file

Sample error

Introduction

Consider that your customer has reported you an error with the offset number; you can determine the line in the source which caused the error. This article explains how the erroneous source line can be detected using the offset address in release exe.

Advantage of this method is that it does not require neither to send any extra programs nor a debug exe to your customer nor rebuilding your program like explained in other articles submitted. Disadvantage is that you have to spend some extra effort by digging in two extra compiler generated file types: a .map and some .cod files.

A *.map file basically includes base addresses of compiled functions. A *.cod file generally includes Assembly, machine and source codes, if you apply settings below.

Using the code

  1. Open the supplied demo project OR create a new MFC dialog application using wizard and put some crashy code. Supplied project provides a button that crashes when clicked. Select Release from Build -> Set Active Configuration.
  2. Project -> Settings ->. Under "C/C++", Category "Listing Files". Select "Assembly, Machine Code, and Source" as "Listing File type:". This option forces compiler to generate a Source_File_Name.cod file for every source file separately.
  3. Again Project -> Settings ->. Under "Link", category "Debug". Mark "Generate map file", Click OK. This option forces compiler to generate a Project_Name.map file.
  4. The provided project exe generates error at address 148f: Open map file, determine function including code at 148f.

    These lines lie in sample map file.

     0001:00000470   ?OnQueryDragIcon@CDebugSampleDlg@@IAEPAUHICON__
                                     @@XZ 00401470 f   Debug SampleDlg.obj
     0001:00000480   ?OnButton1@CDebugSampleDlg@@IAEXXZ 
                                     00401480 f   Debug SampleDlg.obj
     0001:000004a0   ?BeginModalState@CWnd@@UAEXXZ 004014a0 
                                     f i Debug SampleDlg.obj

    The leading '1' is the segment address. Thus the function includes code at 48f is OnButton1 which begins at 480.

  5. Search string OnButton1 in cod files. Locate implementation of this function. In this listing, expressions such ; 174 : corresponds to line numbers in source code.

    Debug SampleDlg.codfile includes these lines:

    ?OnButton1@CDebugSampleDlg@@IAEXXZ PROC NEAR   
                                ; CDebugSampleDlg::OnButton1, COMDAT
    
    
    ; 174  : {
    
    
      00000    83 ec 0c     sub     esp, 12            ; 0000000cH
    
    
    ; 175  :     // TODO: Add your control notification handler code here
    
    ; 176  :     
    
    ; 177  :     // Some garbage code here {
    
    
    ...
    
    ; 182  :     //    }
    
    ; 183  :     
    
    ; 184  :     *( (int *) 0x0000) = 1;
    
    
      0000f    c7 05 00 00 00
        00 01 00 00 00     mov     DWORD PTR ds:0, 1
    
    ; 185  : }
  6. Subtract beginning address of function from the offset that error occurred: 148f - 1480 = fh. Source code generates machine code on line 000f exists on 182 - 184 numbered source code lines. That's
    ; 183  :     
    
    ; 184  :     *( (int *) 0x0000) = 1;
    
    
      0000f    c7 05 00 00 00
        00 01 00 00 00    mov   DWORD PTR ds:0, 1

    Error hides within OnButton1 function at line *( (int *) 0x0000) = 1;

Please let me now if you find this article useful, or know a better way.

History

  • 21.07.2003 : First release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here