C + + Single-Step View assembly code "Go"

Source: Internet
Author: User

form here

Use GDB to view the assembly code, using the disassemble and X commands. Nexti, Stepi can be executed in single-step order

The following example:

------------------------------------------------------------
Source:
------------------------------------------------------------
[email protected] ~]# cat 1.c
#include <stdio.h>

int main (int argc, char *argv[])
{
int size=sizeof ("HJJ");
printf ("Size is%d\n", size);
return 0;
}

------------------------------------------------------------
Compile
------------------------------------------------------------
[Email protected] ~]# Gcc-g3-o 1 1.c

------------------------------------------------------------
Debugging
------------------------------------------------------------
[[email protected] ~]# GDB 1
GNU gdb (GDB) 7.6
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "X86_64-unknown-linux-gnu".
For bugs reporting instructions, please see:
Reading symbols From/root/1...done.
(GDB) B main
Breakpoint 1 at 0x4004d3:file 1.c, line 5.
(GDB) R
Starting program:/ROOT/1

Breakpoint 1, Main (argc=1, ARGV=0X7FFFFFFFE5C8) at 1.c:5
5 int size=sizeof ("HJJ");

------------------------------------------------------------

Observation. With Disasseble.

/m source and assembly arranged together

/R can also see 16 binary code

------------------------------------------------------------
(GDB) disassemble/m main
Dump of assembler code for function main:
4 {
0X00000000004004C4 <+0>: Push%RBP
0X00000000004004C5 <+1>: mov%rsp,%rbp
0x00000000004004c8 <+4>: Sub $0x20,%rsp
0x00000000004004cc <+8>: mov%edi,-0x14 (%RBP)
0X00000000004004CF <+11>: mov%rsi,-0x20 (%RBP)

5 int size=sizeof ("HJJ");
= 0x00000000004004d3 <+15>: Movl $0x4,-0x4 (%RBP)

6 printf ("Size is%d\n", size);
0x00000000004004da <+22>: mov $0x4005f8,%eax
0X00000000004004DF <+27>: mov-0x4 (%RBP),%edx
0x00000000004004e2 <+30>: mov%edx,%esi
0x00000000004004e4 <+32>: mov%rax,%rdi
0x00000000004004e7 <+35>: mov $0x0,%eax
0x00000000004004ec <+40>: callq 0x4003b8 <[email protected]>

7 return 0;
0X00000000004004F1 <+45>: mov $0x0,%eax

8}
0x00000000004004f6 <+50>: Leaveq
0x00000000004004f7 <+51>: retq

End of assembler dump.
------------------------------------------------------------
Use x/i to view instructions
------------------------------------------------------------
(GDB) x/15i main
0X4004C4 <main>: Push%RBP
0X4004C5 <main+1>: mov%rsp,%rbp
0x4004c8 <main+4>: Sub $0x20,%rsp
0x4004cc <main+8>: mov%edi,-0x14 (%RBP)
0X4004CF <main+11>: mov%rsi,-0x20 (%RBP)
= 0x4004d3 <main+15>: Movl $0x4,-0x4 (%RBP)
0x4004da <main+22>: mov $0x4005f8,%eax
0X4004DF <main+27>: mov-0x4 (%RBP),%edx
0x4004e2 <main+30>: mov%edx,%esi
0x4004e4 <main+32>: mov%rax,%rdi
0x4004e7 <main+35>: mov $0x0,%eax
0x4004ec <main+40>: callq 0x4003b8 <[email protected]>
0X4004F1 <main+45>: mov $0x0,%eax
0x4004f6 <main+50>: Leaveq
0x4004f7 <main+51>: retq
------------------------------------------------------------
$PC point to the current program run address
------------------------------------------------------------
(GDB) x/5i $pc
= 0x4004d3 <main+15>: Movl $0x4,-0x4 (%RBP)
0x4004da <main+22>: mov $0x4005f8,%eax
0X4004DF <main+27>: mov-0x4 (%RBP),%edx
0x4004e2 <main+30>: mov%edx,%esi
0x4004e4 <main+32>: mov%rax,%rdi
(GDB)
--------------------------------------------------------------------------------
Debug the assembly code (binary code) with GDB.
View:
Disassembler $pc
display/i $pc
x/i $pc
Execution: Single instruction.
Ni
Si

Use P $eax

P $edi and so on can view registers.

Typing help layout in gdb

(GDB) Help layout
Change the layout of Windows.
Usage:layout Prev | Next | <layout_name>
Layout names is:
Src:displays source and command windows.
Asm:displays disassembly and command windows.
Split:displays source, Disassembly and command windows.
Regs:displays Register window. If Existing layout
is Source/command or Assembly/command, the
Register window is displayed. If the
Source/assembly/command (split) is displayed,
The Register window is displayed with
The window that had current logical focus.

Then you can use layout to debug, very convenient, such as layout asm.

C + + Single-Step View assembly code "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.