Author Topic: GDB - Monster headache  (Read 14024 times)

Offline MrJay

  • Jr. Member
  • *
  • Posts: 2
GDB - Monster headache
« on: August 21, 2014, 10:56:45 PM »
I picked up an interesting book (assembly language step by step) on writing assembler on linux machines with NASM.   I've got stuff compiling, linking and running.   The problem is that I can't get debugging figured out.   I tried DDD GDB and found (I think) that gdb is my problem.   DDD adds a layer of confusion so I've been trying to do simple things with gdb without ddd.

Anyway - I assemble one (several different) moronic hello worlds program like this.
nasm -f elf -g -F stabs -o hello.obj hello.asm -l hello.lst

I then link this masterpeice
ld -s -o hello hello.obj

if I run it
./hello

It works just dandy.   Then I try debugging with...
gdb hello
which gets me a mighty fine error message (no debugging symbols found)   

So I type...
break _start
gdb tells me, "No symbol table is loaded.  Use the "file" command."
If I try...
disass
I get,  "No frame selected."

I'm starting to get complaints from Google that I'm wearing it out.   Does anyone have any ideas?   I really want to get past this road cone.

I've tried using dwarf rather then stabs with no better success.


*I don't get "no debugging symbols found" error when I do...
gdb hello.obj
But it doesn't work either.

Is GDB a screwdriver that I'm trying to pound nails in with or??

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: GDB - Monster headache
« Reply #1 on: August 22, 2014, 01:27:50 AM »
I may not be the best one to advise you about gdb. I consider gdb "unfriendly". As a consequence, I don't use it much. As a consequence, I haven't learned to use it. As a consequence, I continue to find it "unfriendly". I suggest you be smarter and learn to use it!

One clue I can give you is that giving the "-s" switch to ld strips the debugging symbols that you've told Nasm to add. Lose that, for starters.

The late Chuck Crayne, as one of his last contributions to Nasm, added the "dwarf" debugging symbols (previously it was just "stabs"). It's supposed to be the "native" format for gdb. To be honest, I haven't noticed any difference (note that I don't use it much!), but I use "-F dwarf" as a tribute to Chuck, when I use it. You don't need to use both "-g" and "-F dwarf" (used to need both - another change Chuck made). Just "-g" will give you the "default" debugging info format, which is "stabs". Just "-F dwarf" is all you need (the "-g" does no harm). That, and omit the toxic "-s" to ld... Hey, the command line's getting shorter! :)

It also helps to put a "nop" as the first instruction after your "_start" label. Any single-byte instruction may work (cld ?), but "nop" definitely helps. If you're starting with "main" (give gcc the "-g" switch), I don't think you need this, but it shouldn't hurt...

I have a few debuggers that I sometimes use as alternatives to gdb. Some of 'em are even written in Nasm syntax! I'll attach 'em or something if you (anyone) wants to look at 'em, but I think the "right" answer is to learn to use the "monster headache"! There may be a tutorial around: http://asm.sf.net somewhere(?).

I think your main problem is actually an "ld error" rather than a "Nasm error" or a "gdb error", but it results in thwarting us either way. Lose the "-s" switch to ld, and let us know how it goes from there (if you would).

Best,
Frank


Offline dalfonso01

  • Jr. Member
  • *
  • Posts: 44
Re: GDB - Monster headache
« Reply #2 on: August 22, 2014, 09:45:13 AM »
Hi,
I do not know precisely the difference between the two formats. What I saw is that dwarf works with insight while stabs seems not.

Fabio D'Alfonso

Offline InfinitelyManic

  • Jr. Member
  • *
  • Posts: 6
  • Country: 00
  • x86-64 NASM on Linux
Re: GDB - Monster headache
« Reply #3 on: November 15, 2014, 07:33:43 AM »
I think Frank is on the right track.   I started with 32-bit NASM Assembly for Linux and remember some of the points that Frank is making.

Notwithstanding, I now only use 64-bit NASM for Linux (command line only) with heavy use of printf/scanf/malloc, etc. so I generally only use GCC to compile & link and then GDB to debug. 

Please excuse me for not using the code text box; I'm also lazy and I'm just priming the pump.   

The following lines work just fine w/ GDB assuming you are using main vs _start in your program.
...
global main
  main:
...

$ nasm -felf64 -g -F stabs program.asm
$ gcc program.o -o program
$ gdb -q -tui ./program

David a/k/a @InfinitelyManic

Offline mega

  • Jr. Member
  • *
  • Posts: 4
Re: GDB - Monster headache
« Reply #4 on: December 10, 2014, 11:38:57 AM »
On a side note, you'll find gdb a lot friendlier if you use https://github.com/gdbinit/Gdbinit .. and if you're using _start instead of main then 'set breakpoint pending on' and 'b _start' will help:

Code: [Select]
cat ~/.gdbinit.local
set $COLOR_REGVAL = $WHITE
set $SETCOLOR1STLINE = 1
set $SHOWSTACK = 1
set follow-fork-mode child
set breakpoint pending on
echo [*] setting breakpoint on main\n
b main
echo [*] setting breakpoint on _start\n
b _start

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: GDB - Monster headache
« Reply #5 on: January 21, 2015, 08:34:52 PM »
I picked up an interesting book (assembly language step by step) on writing assembler on linux machines with NASM.   I've got stuff compiling, linking and running.   The problem is that I can't get debugging figured out.   I tried DDD GDB and found (I think) that gdb is my problem.   DDD adds a layer of confusion so I've been trying to do simple things with gdb without ddd.

Debugging can be difficult when your debugger is acting buggy. :D In all seriousness though, GDB actually isn't your problem.

Anyway - I assemble one (several different) moronic hello worlds program like this.
nasm -f elf -g -F stabs -o hello.obj hello.asm -l hello.lst

I then link this masterpeice
ld -s -o hello hello.obj

if I run it
./hello

It works just dandy.   Then I try debugging with...
gdb hello
which gets me a mighty fine error message (no debugging symbols found)

This is where your problem is. The '-s' option for the GNU Loader (ld) is used to strip all debugging symbols from your program. This means that LD is deleting all those handy symbols you added with the NASM '-g' option.

So I type...
break _start
gdb tells me, "No symbol table is loaded.  Use the "file" command."
If I try...
disass
I get,  "No frame selected."

I'm starting to get complaints from Google that I'm wearing it out.   Does anyone have any ideas?   I really want to get past this road cone.

I've tried using dwarf rather then stabs with no better success.


*I don't get "no debugging symbols found" error when I do...
gdb hello.obj
But it doesn't work either.

Is GDB a screwdriver that I'm trying to pound nails in with or??

See if fixing your LD command helps you out. If you still get all these errors, post a copy of your source on this post and what version of GDB you're using so I can get a first hand look at what's going on.

Best Regards,
Bryant Keller

About Bryant Keller
bkeller@about.me