Author Topic: Breakpoints in assembly code  (Read 11630 times)

Offline druizm

  • Multiple posting newcomer
  • *
  • Posts: 12
Breakpoints in assembly code
« on: May 10, 2019, 01:40:10 pm »
Hello,

I would like to add a breakpoint in my assembly code. My project is composed by two files: one C file and one ASM file. I compile asm file with "nasm -g -f elf64" and c file with "gcc -g -O0 -o". Project compiles OK and, then, I can debug it. If I add a breakpoint in my c source code, when I run "Debug" execution stops at the line that contains the breakpoint in the c file. However, if I remove that breakpoint and add another one in one "mov" line in the asm file, the debug execution stops at the line that calls the asm file from the c file, but NOT ENTER into the asm file.

Could anybody help me?

Thanks

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Breakpoints in assembly code
« Reply #1 on: May 11, 2019, 03:39:22 pm »
What is the output of  command "info breakpoints" (you can execute it in the debugger's log)?
Are you sure nasm could produce compatible debug symbols?
What OS are you using?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline druizm

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Breakpoints in assembly code
« Reply #2 on: May 13, 2019, 09:47:23 am »
If I execute "info breakpoints" from the Debugger tab (during debugging), I receive this information:
Quote
> info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00007ffff7b32920 exception throw
2       breakpoint     keep y   0x00000000004005c1 /root/CodeBlocks-workspace/Test/assembler.asm:21
   breakpoint already hit 1 time
In arand.cont () ()
#2  0x0000000000400668 in main () at /root/CodeBlocks-workspace/Test/Test-manual-Tomas.c:25
/root/CodeBlocks-workspace/Test/Test-manual-Tomas.c:25:483:beg:0x400668
At /root/CodeBlocks-workspace/Test/Test-manual-Tomas.c:25

So it seems debugger detects correctly breakpoint located within the asm file, but execution stops at C code and not jumps to ASM code, so F7 (Next Line) execute all line in asm code but remains at the same line in C code (it's a function in assembler code), so I can't debug line-by-line after breakpoint inside the asm code.

I can't know if nasm is producting compatible debug symbols. My CodeBlocks configuration for asm file is this:
Quote
Command line macro:
nasm -g -f elf64 -o obj/Debug/$file_name.o $file

so I suppose nasm is generating compatible debug symbols.

I'm running CodeBlocks 17.12 in a CentOS-7_x86_64

Thanksk.

Offline druizm

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Breakpoints in assembly code
« Reply #3 on: May 13, 2019, 03:31:23 pm »
I don't know what I have reconfigured in my CodeBlocks user interface (options) but I have seen this message when I run F7:
"Cannot find bounds of current function"

Could be that message the reason I can't debug line-by-line inside assembler code?

Thanks.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Breakpoints in assembly code
« Reply #4 on: May 14, 2019, 01:00:54 am »
This probably means that you nasm compiler has failed to generate a proper frame info for your asm function or something similar. Does stepping using the instruction commands? Does breaking on an address works?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline druizm

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Breakpoints in assembly code
« Reply #5 on: May 14, 2019, 11:20:51 am »
I answer you between lines...

--> This probably means that you nasm compiler has failed to generate a proper frame info for your asm function or something similar.
How can I know if nasm has compiled my assembler code correctly?

--> Does stepping using the instruction commands?
Well, in Dissassembly window I can see assembler generated code from my binary. Also, I can mark "Mixed Mode" that allows me check dissassembly assembler code and C source code (from my source C file). But if my breakpoint is inside my asm file, step-by-step doesn't jump to asm code, so F7 executes correctly each line from my asm code but breakpoint mark stills at C file. So I can debug each of my asm code.

--> Does breaking on an address works?
Mmm, not. I have added breakpoints asm function start point (in asm file) and, also, I have added some breakpoints in "mov" or "jmp" instructions, but F7 still remains at line in C file from I call my asm function.


Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Breakpoints in assembly code
« Reply #6 on: May 14, 2019, 07:08:14 pm »
Try something simple:
1. Put a breakpoint on the line calling your asm function in C
2. Start debugger and wait it to hit the breakpoint
3. Use the Debug -> Step into instruction command

What happens at 3? What is the code generated from your asm code? If the code is inline the behaviour is expected.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Krice

  • Almost regular
  • **
  • Posts: 150
Re: Breakpoints in assembly code
« Reply #7 on: May 16, 2019, 11:00:51 am »
but NOT ENTER into the asm file.

It's possible the C debugger treats assembler parts as a single "line" of C code.

Offline druizm

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Breakpoints in assembly code
« Reply #8 on: May 16, 2019, 11:59:04 am »
Try something simple:
1. Put a breakpoint on the line calling your asm function in C
2. Start debugger and wait it to hit the breakpoint
3. Use the Debug -> Step into instruction command

What happens at 3? What is the code generated from your asm code? If the code is inline the behaviour is expected.

At 3, code generated from my asm code (in Dissassembly view) is 99.99% like mine (some registers have changed name)... So, is it normal? I suppose that... but my problem persists: I can't apply a breakpoint directly in the asm code (I only can in the line calling my asm function from C)

Offline druizm

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Breakpoints in assembly code
« Reply #9 on: May 16, 2019, 11:59:37 am »
but NOT ENTER into the asm file.

It's possible the C debugger treats assembler parts as a single "line" of C code.

Uppps, so... is there no solution?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Breakpoints in assembly code
« Reply #10 on: May 16, 2019, 07:24:21 pm »
So at step 3 you're able to go to your function and debug it line by line, aren't you?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Breakpoints in assembly code
« Reply #11 on: May 16, 2019, 07:27:01 pm »
Also make sure to pass -F dwarf to nasm...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline druizm

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Breakpoints in assembly code
« Reply #12 on: May 17, 2019, 12:18:20 pm »
So at step 3 you're able to go to your function and debug it line by line, aren't you?

Yes. When I add the breakpoint in the C file where ASM is called, in the dissassembly view I can run a "Step into instruction" with no problem.

Offline druizm

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Breakpoints in assembly code
« Reply #13 on: May 17, 2019, 12:19:27 pm »
Also make sure to pass -F dwarf to nasm...

OK, reconfiguring my "Settings --> Compiler" configuration.

Why this change?

Thanks!

Offline druizm

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Breakpoints in assembly code
« Reply #14 on: May 17, 2019, 12:39:37 pm »
Also make sure to pass -F dwarf to nasm...

OK, reconfiguring my "Settings --> Compiler" configuration.

Why this change?

Thanks!


Yeahhhh!!!!!!! Adding "-F dwarf" I can debug "line to line, instruction to instruction" my ASM source file...

Thaaaaaaaaaaaanks!!!!!!!!!!!

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Breakpoints in assembly code
« Reply #15 on: May 18, 2019, 12:04:45 pm »
Quote
Why this change?
The debugger needs a specific format (called dwarf) for debug symbols in the executable. With this setting you say that this symbols should be in dwarf format, that gdb can understand....