Click here to Skip to main content
11,719,185 members (77,675 online)
Click here to Skip to main content

Tagged as

How to determine corresponding source line from offset number

, 22 Jul 2003 95.2K 1.6K 73
Rate this:
Please Sign up or sign in to vote.
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.cod file 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

Share

About the Author

eymre
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionIrrelevant to managed code, correct? Pin
Sam Hobbs17-Mar-12 1:18
memberSam Hobbs17-Mar-12 1:18 
QuestionWhat about using libraries ? Pin
Davide Zaccanti1-Oct-06 20:15
memberDavide Zaccanti1-Oct-06 20:15 
GeneralI did not set theMAP file option when I release last...HELP Pin
Gautam Jain4-Nov-04 23:40
memberGautam Jain4-Nov-04 23:40 
GeneralRe: I did not set theMAP file option when I release last...HELP Pin
qwerty666qwerty6666-Feb-06 21:00
memberqwerty666qwerty6666-Feb-06 21:00 
GeneralExcellent Piece of Information!!! Pin
Balkrishna Talele26-Aug-04 19:31
memberBalkrishna 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
memberJigar Mehta10-Jul-04 0:36 
QuestionHow you will find the offset address? Pin
Thangs31-Jul-03 7:02
memberThangs31-Jul-03 7:02 
AnswerRe: How you will find the offset address? Pin
Emre BURHAN1-Aug-03 3:13
memberEmre BURHAN1-Aug-03 3:13 
Questionhow do you find crashed function in system dlls? Pin
diamant31-Jul-03 0:59
memberdiamant31-Jul-03 0:59 
AnswerRe: how do you find crashed function in system dlls? Pin
Mike Dimmick31-Jul-03 9:56
memberMike Dimmick31-Jul-03 9:56 
AnswerRe: how do you find crashed function in system dlls? Pin
coolvcguy16-Aug-03 19:36
membercoolvcguy16-Aug-03 19:36 
GeneralRe: how do you find crashed function in system dlls? Pin
diamant17-Aug-03 23:02
memberdiamant17-Aug-03 23:02 
GeneralDebugging book Pin
nerd_biker30-Jul-03 4:08
membernerd_biker30-Jul-03 4:08 
GeneralCrash Finder Pin
mwilliamson23-Jul-03 6:44
membermwilliamson23-Jul-03 6:44 
GeneralRe: Crash Finder Pin
Shaun Wilde28-Jul-03 20:56
memberShaun Wilde28-Jul-03 20:56 
GeneralRe: Crash Finder Pin
Carlos29-Jul-03 22:28
memberCarlos29-Jul-03 22:28 
GeneralCool! Pin
Ryan Binns22-Jul-03 22:50
memberRyan Binns22-Jul-03 22:50 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150901.1 | Last Updated 23 Jul 2003
Article Copyright 2003 by eymre
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid