LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2020, 01:18 AM   #1
00x
LQ Newbie
 
Registered: Aug 2018
Distribution: OpenBSD
Posts: 20

Rep: Reputation: 0
gdb for assembly language


Hi. I'm fairly new with assembly language (using nasm on ubuntu 64bit) and I'm having some trouble using gdb for debugging.
Right now I have a sandbox asm file that I put code into just to test to see how registers and stuff change in gdb before I try writing an actual program.
This isn't a program I need help with I just want to know why gdb is responding like it is.

(lines 1-8 are just comments)
Code:
9	SECTION .data			; Section containing initialized data
10	
11	SECTION .bss			; Section containing uninitialized data
12	
13	SECTION .text			; Section containing code
14	
15	
16	global _start			; Linker needs this to find the entry point!
17	
18	_start:
19	   nop				; This no-op keeps gdb happy
20	
21	; put test code below {{{
22	
23	mov eax, "WXYZ"
24	
25	mov ebp,esi
26	mov bl,ch
27	add di,ax
28	add ecx,edx
29	mov ax,067FEh
30	xchg cl,ch
31	
32	; }}} put test code above
33	
34	nop
35	
36	; clean up
37	   mov eax,1			; Specify Exit syscall
38	   mov ebx,0			; Return a code of zero
39	   int 80H			; Make syscall to terminate program
Here is gdb output when I try just simple break and print commands:

Code:
(gdb) break 23
Breakpoint 1 at 0x400082: file sandbox.asm, line 23.
(gdb) run
Starting program: /home/gucci/CODE.d/ASMBLY.d/sandbox.d/sandbox 
[Inferior 1 (process 26302) exited normally]
Why doesn't it break? It does this for break 25, 29 and 30 as well

Only on line 26 can I successfully add a break
Code:
(gdb) break 26
Breakpoint 3 at 0x400090: file sandbox.asm, line 26.
(gdb) run
Starting program: /home/gucci/CODE.d/ASMBLY.d/sandbox.d/sandbox 

Breakpoint 3, 0x0000000000400090 in _start ()
When I add a break at 28 I get a segfault when I try running, why?
Code:
(gdb) b 28
Breakpoint 7 at 0x40009a: file sandbox.asm, line 28.
(gdb) run
Starting program: /home/gucci/CODE.d/ASMBLY.d/sandbox.d/sandbox 

Program received signal SIGSEGV, Segmentation fault.
0x00000000004000a2 in ?? ()
The program does not segfault if I just run it normally (`$ ./sandbox')

Also I am assembling with `nasm -f elf64 -g -F stabs sandbox.asm'
(The book I am working from is for 32bit but I am on a 64bit cpu)
but when I try to run `$ gdb -tui sandbox' it won't show the source code, I have to open the source code in a separate terminal and work from those line numbers.
However when I try `ddd sandbox' ddd will open sandbox.asm in it's source code window.

Any help would be appreciated,
Thanks.
 
Old 04-06-2020, 04:34 AM   #2
00x
LQ Newbie
 
Registered: Aug 2018
Distribution: OpenBSD
Posts: 20

Original Poster
Rep: Reputation: 0
Just in case anyone was interested, I realized the problem was that I had forgotten that when setting a breakpoint at say line 26, the debugger stops execution at line 25!

The segfaults are caused I think by add, xchg and `mov ax,067FEh' (I just copied these straight from the book I'm reading)

And the reason gdb isn't stopping at the breakpoints I'm setting and instead just runs straight through the program, is because gdb just gives the illusion that it is debugging with the source code, but it is instead working off machine code. Therefore `nops' need to be added,
Code:
...
	nop
	mov eax, "WXYZ"
	nop			; put the breakpoint on this line to pause at the line above it
	mov ebp,esi
	nop
...

Last edited by 00x; 04-06-2020 at 04:51 AM.
 
Old 04-06-2020, 06:48 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
I have a moderate amount of experience with gdb, but solely with C source.

Your second comment mentions line 26, but your original comment cites line 23 which should be before the fault.

I can understand that for assembly it doesn't follow the source file lines. And then it seems to accept the setting of a breakpoint...

Why do you say it won't print out those source lines? They should be easier to see because when they show in the debugger in assembly, they're obviously matching 100% what your file shows.

You can use 'b' as opposed with typing out 'break'.

You can use 'l' to 'list' the area in your source near or about where it thinks the program counter presently is located, or near a source line.

Like you can just type 'l' or 'list' or you can also type 'l 25' or 'list 25' and that should show you source line #25, but for C code is where I'm more familiar.

Listing the source doesn't work?

Last edited by rtmistler; 04-06-2020 at 06:49 AM.
 
Old 04-06-2020, 07:57 AM   #4
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
In my experience, which is limited, gdb isn't terribly assembler friendly. Still, I took your code, removed the nop since it shouldn't be necessary, and compiled it with yasm and it worked as expected. I use yasm, and with yasm, to use gdb you have to indicate a debugging format. i.e.
Code:
yasm -f elf64 -g dwarf2 -l forum_test1.lst -o forum_test1.o forum_test1.asm
This allowed me to set breakpoints and so forth.

I then linked with ld.

There are two debugging formats dwarf and stabs but I think dwarf has mostly replaced stabs.

Last edited by SoftSprocket; 04-06-2020 at 07:59 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] A problem. When using assembly language to call the C language function mirage1993 Programming 3 10-03-2014 08:15 AM
[SOLVED] Linux Assembly: write and receive assembly data errigour Programming 2 09-22-2012 09:54 AM
Is Assembly Language considered a Structured Language? theKbStockpiler Programming 4 01-30-2011 09:09 AM
gdb assembly dump centr0 Programming 3 07-07-2003 11:38 AM
gdb assembly dump "i386-slackware-linux" centr0 Linux - General 0 06-11-2003 11:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration