Debug assembly code in Linux

Source: Internet
Author: User

In Linux, You can debug assembly code by using a general debugger such as GDB and DDD, or by using an ALD (assembly language debugger) Specially Used to debug assembly code ).
From the perspective of debugging, the advantage of using gas is that you can include the symbol table in the generated target code ), in this way, you can use GDB and DDD for source code-level debugging. To include the symbol table in the generated executable program, you can compile and link the table in the following way:

[Xiaowp @ Gary Code] $ as -- maid-O hello. O hello. s
[Xiaowp @ Gary Code] $ LD-O hello. o
 

When running the as command, the parameter-ststabs can tell the assembler to add a symbol table to the generated target code. Note that the-S parameter is not added when the LD command is used for link, otherwise, the symbol table in the target code will be deleted during the link.
Assembly programmers usually face some harsh software and hardware environments. The short and concise ALD may better meet the actual needs. Therefore, the following describes how to use the ALD to debug the assembly program. First, run the ALD command in the command line mode to start the debugger. The parameter of this command is the executable program to be debugged:

[Xiaowp @ Gary Doc] $ ALD hello
Assembly Language debugger 0.1.3copyright (c) 2000-2002 Patrick alken
Hell elf intel 80386 (32 bit), LSB, executable, Version 1 (current)
Loading debugging symbols... (15 symbols loaded)
ALD>
 

When the ALD prompt appears, run the disassemble command to decompile the code segment:

ALD> disassemble-S. Text
Disconfiguring section. Text (0x08048074-0x08048096)
08048074 ba0f000000 mov edX, 0xf
08048079 b998900408 mov ECx, 0x8049098
0804807e bb0000000 mov EBX, 0x1
08048083 b804000000 mov eax, 0x4
08048088 CD80 int 0x80
0804808a bb00000000 mov EBX, 0x0
0804808f b80000000 mov eax, 0x1
08048094 CD80 int 0x80
 

The first column of the output information is the address code corresponding to the command. It can be used to set the breakpoint during program execution:

ALD> Break 0x08048088
Breakpoint 1 set for 0x08048088
 

After the breakpoint is set, run the Run Command to run the program. When a breakpoint occurs, the system automatically suspends the program and displays the current values of all registers:

ALD> RUN
Starting program: Hello
Breakpoint 1 encountered at 0x08048088
Eax = 0x00000004 EBX = 0x00000001 ECx = 0x08049098 edX = 0x0000000fesp = 0xbffff6c0 EBP = 0x00000000 ESI = 0x00000000 EDI = 0x00000000
DS = 0x0000002b es = 0x0000002b FS = 0x00000000 GS = 0x00000000
Ss = 0x0000002b cs = 0x00000023 EIP = 0x08048088 eflags = 0x00000246
Flags: pf ZF if
08048088 CD80 int 0x80
 

To debug the assembly code in one step, run the following command:

ALD> next
Hello, world!
Eax = 0x0000000f EBX = 0x00000000 ECx = 0x08049098 edX = 0x0000000fesp = 0xbffff6c0 EBP = 0x00000000 ESI = 0x00000000 EDI = 0x00000000
DS = 0x0000002b es = 0x0000002b FS = 0x00000000 GS = 0x00000000
Ss = 0x0000002b cs = 0x00000023 EIP = 0x0804808f eflags = 0x00000346
Flags: pf zf tf if
0804808f b80000000 mov eax, 0x1
 

To obtain a detailed list of all the Debugging commands supported by ALD, you can use the help command:

ALD> help
Commands may be abbreviated.
If a blank command is entered, the last command is repeated.
Type 'help <command> ''for more specific information on <command>.
General commands
Attach clear continue detach disassemble
Enter examine file help load
Next quit register run set
Step unload window write
Breakpoint related commands
Break Delete disable enable ignore
Lbreak tbreak

 

 

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.