Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C++
Article

How to determine corresponding source line from offset number

Rate me:
Please Sign up or sign in to vote.
4.82/5 (37 votes)
22 Jul 20032 min read 128.2K   74   17
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.

    ASM
    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.cod file includes these lines:

    ASM
    ?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
    ASM
    ; 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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIrrelevant to managed code, correct? Pin
Sam Hobbs17-Mar-12 1:18
Sam Hobbs17-Mar-12 1:18 
It would help if there was something in the article header indicating this is only relevant to C++.
QuestionWhat about using libraries ? Pin
Davide Zaccanti1-Oct-06 20:15
Davide Zaccanti1-Oct-06 20:15 
GeneralI did not set theMAP file option when I release last...HELP Pin
Gautam Jain4-Nov-04 23:40
Gautam Jain4-Nov-04 23:40 
GeneralRe: I did not set theMAP file option when I release last...HELP Pin
qwerty666@codeproject.com6-Feb-06 21:00
qwerty666@codeproject.com6-Feb-06 21:00 
GeneralExcellent Piece of Information!!! Pin
Balkrishna Talele26-Aug-04 19:31
Balkrishna Talele26-Aug-04 19:31 
QuestionIs there any way to get the exported function's name with proper parameter (in short signature)??? Pin
Jigar Mehta10-Jul-04 0:36
Jigar Mehta10-Jul-04 0:36 
QuestionHow you will find the offset address? Pin
Thangs31-Jul-03 7:02
Thangs31-Jul-03 7:02 
AnswerRe: How you will find the offset address? Pin
1-Aug-03 3:13
suss1-Aug-03 3:13 
Questionhow do you find crashed function in system dlls? Pin
diamant31-Jul-03 0:59
diamant31-Jul-03 0:59 
AnswerRe: how do you find crashed function in system dlls? Pin
Mike Dimmick31-Jul-03 9:56
Mike Dimmick31-Jul-03 9:56 
AnswerRe: how do you find crashed function in system dlls? Pin
coolvcguy16-Aug-03 19:36
coolvcguy16-Aug-03 19:36 
GeneralRe: how do you find crashed function in system dlls? Pin
diamant17-Aug-03 23:02
diamant17-Aug-03 23:02 
GeneralDebugging book Pin
nerd_biker30-Jul-03 4:08
nerd_biker30-Jul-03 4:08 
GeneralCrash Finder Pin
mwilliamson23-Jul-03 6:44
mwilliamson23-Jul-03 6:44 
GeneralRe: Crash Finder Pin
Shaun Wilde28-Jul-03 20:56
Shaun Wilde28-Jul-03 20:56 
GeneralRe: Crash Finder Pin
Carlos29-Jul-03 22:28
Carlos29-Jul-03 22:28 
GeneralCool! Pin
Ryan Binns22-Jul-03 22:50
Ryan Binns22-Jul-03 22:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.