Linux0.11 Study Notes-technical preparations-simple AB Task Switching Program (3)-debugging methods and methods

Source: Internet
Author: User

Write bottom layerCode, We need special means to debug and debug. This article introduces several methods for several application scenarios.

1. view the binary value of the file-hexdump command

ToArticle(2) HeadProgramFor example

LD head. O-M elf_i386-ttext 0-e startup_32-O system, the Generated System is an executable file with a file header, not pure code.

Hexdump System

The command output is as follows:

0000000 457f 464c 0101 0001 0000 0000 0000
0000010 0002 0003 0001 0000 0000 0000 0034
0000020 1140 0000 0000 0000 0034 0020 0001
0000030 0009 0006 0001 0000 1000 0000 0000
0000040 0000 0000 0018 0000 0018 0000 0005
0000050 1000 0000 0000 0000 0000 0000 0000
0000060 0000 0000 0000 0000 0000 0000 0000
*
000100010b8 0000 8e00 8ed8 8ec0 8ee0 b8e8 00cc
0001010 0000 ba66 8000 feeb 001c 0000 0002 0000
0001020 0000 0004 0000 0000 0000 0000 0018
0001030 0000 0000 0000 0000 005f 0000 0002 0000
...

After objcopy-O Binary-R. Note-R. Comment system kernel is generated, the generated Kernel File is pure binary code.

Hexdump Kernel

The command output is as follows:

000000010b8 0000 8e00 8ed8 8ec0 8ee0 b8e8 00cc
0000010 0000 ba66 8000 feeb
0000018

Compared with the red part, we found that a lot of things were removed, leaving the executable code. Why is it executable binary code? See the following method.

 

2. bochs disassembly-u command

Bochs run. The result is as follows:

You can also start bochs with the-Q option to skip these menus.

1. Restore factory default configuration
2. Read options from...
3. Edit options
4. Save options...
5. Restore the bochs state from...
6. Begin simulation
7. Quit now

Please choose one: [6]

Press enter to continue:

Installing 000i [] installing X module as the bochs Gui
00000000000i [] Using Log File bochsout.txt
Next at T = 0
(0) [0x00000000fffffff0] f000: fff0 (unk. ctxt): JMP far f000: e05b; ea5be000f0
<Bochs: 1>
Enter the command Pb 0x7c00 and press enter, which means to set the breakpoint at the 0x7c00 address.

Enter the command C and the result is as follows:

Pb 0x7c00
C
(0) breakpoint 1, 0x00007c00 in ?? ()
next at t = 3915811
(0) [ 0x0000000000007c00 ] 0000: 7c00 (unk. ctxt): JMP far 07c0: 0005; ea0500c007

the program is set at 0000: 7c00. the green part is the physical address , and the blue 0000: 7c00 is Cs: IP, Pink JMP far 07c0: 0005 is an assembly instruction , and the black part is a binary machine instruction

Enter command N, execute the next command, and run it all the time. After bootloader loads the head to 0x1000, the result is as follows:

(0) [0x0000000000007c2c] 07c0: 002c (unk. ctxt): mov dx, 0x0000; ba0000
<Bochs: 19>
Next at t = 3925587
(0) [0x0000000000007c2f] 07c0: 002f (unk. ctxt): mov CX, 0x0002; b90200
<Bochs: 20>
Next at t = 3925588
(0) [0x0000000000007c32] 07c0: 0032 (unk. ctxt): mov ax, 0x1000; b80010
<Bochs: 21>
Next at t = 3925589
(0) [0x0000000000007c35] 07c0: 0035 (unk. ctxt): mov es, ax; 8ec0
<Bochs: 22>
Next at t = 3925590
(0) [0x0000000000007c37] 07c0: 0037 (unk. ctxt): xor bx, BX; 31db
<Bochs: 23>
Next at t = 3925591
(0) [0x0000000000007c39] 07c0: 0039 (unk. ctxt): mov ax, 0x0204; b80402
<Bochs: 24>
Next at t = 3925592
(0) [0x0000000000007c3c] 07c0: 003c (unk. ctxt): int 0x13; CD13
<Bochs: 25>
Next at t = 3973287
(0) [0x0000000000007c3e] 07c0: 003e (unk. ctxt): JNB. + 10 (0x00007c4a); 730a
<Bochs: 26>
Next at t = 3973288
(0) [0x0000000000007c4a] 07c0: 004a (unk. ctxt): CLI; fa

Enter the command u 0 and the result is as follows:

<Bochs: 45> u 0x10000 0x10010
00010000: (): mov ax, 0x0010; b81000
00010003: (): Add byte ptr ds: [bx + Si], Al; 0000
00010005: (): mov ds, ax; 8ed8
00010007: (): mov es, ax; 8ec0
00010009: (): mov FS, ax; 8ee0
0001000b: (): mov Gs, ax; 8ee8
0001000d: (): mov ax, 0x00cc; b8cc00

You must have suddenly realized that bootloader loads the head to 0x1000: 0x0, and the u command decomassembles the First Command at 0x1000: 0x0, mov ax, 0x0010 is the first command of the head program.

Check the output result of b81000 and hexdump kernel.10b8 0000 8e00The comparison is actually the same data, so the Binary command is used to determine the output after objcopy.

 

2. view the content of the target file-objdump command

Objdump system or objdump head

The output is as follows:

System: File Format elf32-i386

Disassembly of section. Text:

00000000 <startup_32>:
0: B8 10 00 00 00 mov $0x10, % eax
5: 8e D8 mov % eax, % DS
7: 8e C0 mov % eax, % es
9: 8e E0 mov % eax, % FS
B: 8e E8 mov % eax, % GS
D: B8 CC 00 00 00 mov $ 0xcc, % eax
12: 66 Ba 00 80 mov $0x8000, % DX

00000016 <die>:
16: EB Fe JMP 16 <die>

The result of hexdump and bochs disassembly on the front side is the same as that of B8 10 00.

 

4. bochs command

(1) Pb 0x7c00-breakpoint

(2) c-continue execution

(3) N-execute an instruction

(4) S-enter the Function

(5) U 0x10000 0x10010-disassembly 0x0000-0x10010 address

(6) x 0x10000-view the content of address 0x10000. Note whether the addressing mode is real mode or protection mode 0x10: 0x0 has different meanings in two modes.

... For other commands, refer to the bochs manual or enter the H command on the bochs command line.

 

This article is complete.

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.