View Issue Details

IDProjectCategoryView StatusLast Update
0021106LazarusDebuggerpublic2012-02-06 19:53
ReporterNorayr ChilingarianAssigned ToMartin Friebe 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Platformx86_64OSGNU/LinuxOS VersionRHEL6
Product Version0.9.31 (SVN)Product Build 
Target VersionFixed in Version0.9.31 (SVN) 
Summary0021106: Lazarus debugger fails to work with PIE binaries
DescriptionIn the beginning, Lazarus ide calls gdb with an "info address main" command and sets a breakpoint with an address, not the name.

---
(gdb)
<info address main>
&"info address main\n"
~"Symbol \"main\" is a function at address 0x724.\n"
^done
(gdb)
<-break-insert -t *1828>
---

Later, when PIE elf is loaded that address changes and that's why debugging fails.


---
(gdb)
=library-loaded,id="/lib64/ld-linux-x86-64.so.2",target-name="/lib64/ld-linux-x86-64.so.2",host-name="/lib64/ld-linux-x86-64.so.2",symbols-loaded="0",thread-group="i1"
^error,msg="Warning:\nCannot insert breakpoint 1.\nError accessing memory address 0x724: Input/output error.\n"
(gdb)
---

It seems that the beginning of the SetTempMainBreak function of the GDBMIDebugger may be removed:

---
 // Try to retrieve the address of main. Setting a break on main is past initialization
    if ExecuteCommand('info address main', R)
    and (R.State <> dsError)
    then begin
      S := GetPart(['at address ', ' at '], ['.', ' '], R.Values);
      if S <> ''
      then begin
        FTheDebugger.FMainAddr := StrToQWordDef(S, 0);
        ExecuteCommand('-break-insert -t *%u', [FTheDebugger.FMainAddr], R);
        Result := R.State <> dsError;
        if Result then Exit;
      end;
    end;
---

Because later it calls
 ExecuteCommand('-break-insert -t main', R);
which works with PIE elf files ok.

FTheDebugger.FMainAddr seem to not be used, so that change may be the right solution.

Steps To Reproducedebug a pie executable.
Additional Informationwe found out this by debugging apache module which is pie because apache is pie in rhel6
TagsNo tags attached.
Fixed in Revision34811
LazTarget1.0
Widgetset
Attached Files

Activities

Martin Friebe

2012-01-17 19:32

manager   ~0055813

If using '-break-insert -t main' then GDB may analyse the code at the start of main and set the breakpoint several assembler instructions into the function.

Therefore trying <info address main> first is essential. This code is used with many versions of GDB on many platforms. The effects of simply abandoning it a very hard to predict.

The best solution may be to always set both. But this may need further assessment.

Please provide:
- the version of GDB on your system.
- the output of GDB when running the following (using the name of your exe)

gdb -i mi

-file-exec-and-symbols "/tmp/project1"
-exec-arguments
-gdb-set language pascal
info address main
-break-insert -t main

Norayr Chilingarian

2012-01-18 09:09

reporter   ~0055819

What if first check for a gdb version and OS (it seems that the problem exists only under modern Linuxes), then if gdb version is >= 7.1 (earlier gdb versions does not support PIE files debugging) then immediately call "-break-insert -t main" instead of "break-insert -t *address"

$ readelf -h hello | grep -i type
  Type: DYN (Shared object file)

 $ gdb -i mi
=thread-group-added,id="i1"
~"GNU gdb (Gentoo 7.3.1 p1) 7.3.1\n"
~"Copyright (C) 2011 Free Software Foundation, Inc.\n"
~"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law. Type \"show copying\"\nand \"show warranty\" for details.\n"
~"This GDB was configured as \"x86_64-pc-linux-gnu\".\nFor bug reporting instructions, please see:\n"
~"<http://bugs.gentoo.org/>.\n"
(gdb)
-file-exec-and-symbols "/home/noch//hello"
^done
(gdb)
-exec-arguments
^done
(gdb)
-gdb-set language pascal
^done
(gdb)
info address main
&"info address main\n"
~"Symbol \"main\" is a function at address 0x724.\n"
^done
(gdb)
-break-insert -t main
^done,bkpt={number="1",type="breakpoint",disp="del",enabled="y",addr="0x0000000000000728",func="main",file="hello.c",fullname="/home/noch/hello.c",line="3",times="0",original-location="main"}
(gdb)
run
&"run\n"
~"Starting program: /home/noch/hello \n"
=thread-group-started,id="i1",pid="12858"
=thread-created,id="1",group-id="i1"
^running
*running,thread-id="all"
(gdb)
=library-loaded,id="/lib64/ld-linux-x86-64.so.2",target-name="/lib64/ld-linux-x86-64.so.2",host-name="/lib64/ld-linux-x86-64.so.2",symbols-loaded="0",thread-group="i1"
=library-loaded,id="/lib64/libc.so.6",target-name="/lib64/libc.so.6",host-name="/lib64/libc.so.6",symbols-loaded="0",thread-group="i1"
~"\nTemporary breakpoint "
~"1, main () at hello.c:3\n"
~"3\treturn 0;\n"
*stopped,frame={addr="0x00007ffff7ffe728",func="main",args=[],file="hello.c",fullname="/home/noch/hello.c",line="3"},thread-id="1",stopped-threads="all",core="0"
~"Warning: the current language does not match this frame.\n"
(gdb)
show version
&"show version\n"
~"GNU gdb (Gentoo 7.3.1 p1) 7.3.1\n"
~"Copyright (C) 2011 Free Software Foundation, Inc.\n"
~"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law. Type \"show copying\"\nand \"show warranty\" for details.\n"
~"This GDB was configured as \"x86_64-pc-linux-gnu\".\nFor bug reporting instructions, please see:\n"
~"<http://bugs.gentoo.org/>.\n"
^done
(gdb)

Martin Friebe

2012-01-18 12:55

manager   ~0055825

Thanks for feedback.

I will probably make it insert both break points. Maybe make it configurable
 

Martin Friebe

2012-01-20 01:37

manager   ~0055867

Please try with revision 34811

Could you please also provide some more info (even if it works):
See http://wiki.lazarus.freepascal.org/GDB_Debugger_Tips#Log_info_for_debug_session

The content of the debug output window was (open before you press F9). In order to attach this info you must do so before closing this issue.

Also if it works please test if exceptions are shown correctly. That this name and message are shown if an exception occurs.

2012-01-27 15:28

 

dbg.txt.gz (3,184 bytes)

Norayr Chilingarian

2012-01-27 15:32

reporter   ~0056115

I've checked out today's revision and it does not work.
File with the complete debug output uploaded.

Martin Friebe

2012-01-27 17:45

manager   ~0056118

Last edited: 2012-01-27 17:46

I did remove
 ExecuteCommand('-break-insert -t main', R);

and replaced it by entry point.
But the entry point is also am address and does not work (seen in your log)

It turned out that
 ExecuteCommand('-break-insert -t main', R);
is unreliable, once gdb has loaded some libraries, if they also contain a label main


Until I can do further investigation , you cam try modifying gdbmidebugger.pp
Find (line 3959)
  FTheDebugger.FMainAddrBreak.SetAtCustomAddr(Self, StrToQWordDef(EntryPoint, 0));

replace, without "IF"
  FTheDebugger.FMainAddrBreak.SetNamed(Self, StrToQWordDef(EntryPoint,

Martin Friebe

2012-01-28 16:47

manager   ~0056140

Last edited: 2012-01-28 17:34

Please test revision 34987 (EDIT: r 34989)

The default still tries entry-point first. It should detect the error and switch to name.

Mingw gdb does however not crash if I simulate what happened in the session you attached.
So it may be that, at the time the IDE can auto-detect, your gdb already crashed. In that case, I can not fix the *default settings* for your case.

I did however add additional settings. In the Options dialog (page debugger / general) is a property grid. It has an entry "InternalStartBreak" with the following values:

gdsbDefault
  Entrypoint, Main by Name, '-break-insert +0'
  May work for you (unless your gdb crashes)
gdsbEntry,
  Entrypoint, '-break-insert +0'
  May work for you (unless your gdb crashes)
gdsbMainAddr,
  Main by addr, '-break-insert +0'
  May work for you (unless your gdb crashes)
gdsbMain,
  Main by Name, '-break-insert +0'
  ** Should work
gdsbAddZero
  '-break-insert +0', Entrypoint
  ** Should work



 '-break-insert +0' appears to always work.
But before making it default it will need a lot more testing....

---
If at least "gdsbMain" works for you, then I consider this issue fixed

Please note:
Some versions of GDB will fail to use "Main by Name" on the 2nd (or further) run, if any library was loaded, that contained "main" as symbol in its own debug info.
In that case, there is an option, to auto reset the debugger after each run.

---

I would like to ask you to test all of the setting. (Reset debugger between tests)
For any test failed (only failed) please submit "debug output"

Norayr Chilingarian

2012-02-06 09:32

reporter   ~0056561

Lazarus v0.9.31 build 35134
fpc 2.6.0.x86_64

gdsbDefault
 Entrypoint, Main by Name, '-break-insert +0'
 May work for you (unless your gdb crashes)
Works!

gdsbEntry,
 Entrypoint, '-break-insert +0'
 May work for you (unless your gdb crashes)
Crash!

gdsbMainAddr,
 Main by addr, '-break-insert +0'
 May work for you (unless your gdb crashes)
Crash!

gdsbMain,
 Main by Name, '-break-insert +0'
 ** Should work
Works!

gdsbAddZero
 '-break-insert +0', Entrypoint
 ** Should work
Works!

Martin Friebe

2012-02-06 19:53

manager   ~0056594

fixed then.

Issue History

Date Modified Username Field Change
2012-01-17 14:52 Norayr Chilingarian New Issue
2012-01-17 14:52 Norayr Chilingarian Status new => assigned
2012-01-17 14:52 Norayr Chilingarian Assigned To => Marc Weustink
2012-01-17 18:02 Martin Friebe Assigned To Marc Weustink => Martin Friebe
2012-01-17 19:32 Martin Friebe LazTarget => -
2012-01-17 19:32 Martin Friebe Note Added: 0055813
2012-01-17 19:32 Martin Friebe Status assigned => feedback
2012-01-18 09:09 Norayr Chilingarian Note Added: 0055819
2012-01-18 12:55 Martin Friebe Note Added: 0055825
2012-01-18 12:55 Martin Friebe Status feedback => assigned
2012-01-18 12:55 Martin Friebe Target Version => 0.99.0
2012-01-20 01:37 Martin Friebe Fixed in Revision => 34811
2012-01-20 01:37 Martin Friebe LazTarget - => 1.0
2012-01-20 01:37 Martin Friebe Status assigned => resolved
2012-01-20 01:37 Martin Friebe Fixed in Version => 0.9.31 (SVN)
2012-01-20 01:37 Martin Friebe Resolution open => fixed
2012-01-20 01:37 Martin Friebe Note Added: 0055867
2012-01-27 15:28 Norayr Chilingarian File Added: dbg.txt.gz
2012-01-27 15:32 Norayr Chilingarian Status resolved => assigned
2012-01-27 15:32 Norayr Chilingarian Resolution fixed => reopened
2012-01-27 15:32 Norayr Chilingarian Note Added: 0056115
2012-01-27 17:45 Martin Friebe Note Added: 0056118
2012-01-27 17:46 Martin Friebe Note Edited: 0056118
2012-01-28 16:47 Martin Friebe Note Added: 0056140
2012-01-28 16:50 Martin Friebe Note Edited: 0056140
2012-01-28 17:34 Martin Friebe Note Edited: 0056140
2012-02-06 09:32 Norayr Chilingarian Note Added: 0056561
2012-02-06 19:53 Martin Friebe Status assigned => resolved
2012-02-06 19:53 Martin Friebe Resolution reopened => fixed
2012-02-06 19:53 Martin Friebe Note Added: 0056594