Modify the register value of GDB for debugging

Source: Internet
Author: User

When I tested a piece of code from a colleague yesterday, the returned value was not saved to the variable, but directly determined, similar to the following:

Int check_state (); // a function from other library, didn't have it's definition, just a declaration. </P> <p> int func1 () <br/>{< br/> If (! Check_state () <br/> return 0; <br/> else <br/> return-1; <br/>}</P> <p> int parent_func () <br/>{< br/> If (! Func1 () <br/>{< br/> // do something, code segment 1 <br/> ...... <br/> ...... <br/> return-1; <br/>}</P> <p> // do something, code segment 2 <br/> ...... <br/> ...... <br/> return 0; <br/>}

 

Because check_state () is a function from another. A file, its return value cannot be changed. Every time it returns a non-0 value (resulting in func1 (),-1 is returned each time .), However, I want to test the execution status of the code 0 returned by func1. Colleagues do not use a variable to store the returned value here, so they cannot set the value of the variable to achieve the effect.

Every C-language function will return values through registers. Therefore, we can test the function by modifying the register value in func1.


Write a simple test code to describe the usage (check whether the input parameter is greater than 10 ):

# Include <stdio. h> <br/> # include <stdlib. h> <br/> int func1 (INT input) <br/>{< br/> If (input <10) <br/> return-1; <br/> else <br/> return 0; <br/>}< br/> int main (INT argc, char * argv []) <br/>{< br/> int input_val = 0; <br/> If (argc <2) <br/>{< br/> printf ("need argument. /n "); <br/> return-1; <br/>}< br/> input_val = atoi (argv [1]); <br/> If (func1 (input_val) <br/> printf ("input numeric is little than 10. /n "); <br/> else <br/> printf (" input numeric is bigger than 10. /n "); <br/> return 0; <br/>}

 

Compile:

~ # Gcc-g-Wall Main. C-o main

 

Test process:

~ # GDB main

Gnu gdb fedora (6.8-37. EL5)
Copyright (c) 2008 Free Software Foundation, Inc.
License gplv3 +: gnu gpl Version 3 or later This is free software: You are 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 was configured as "i386-redhat-linux-gnu "...
(GDB) B Main
Breakpoint 1 at 0x80483e9: file main. C, line 14.
(GDB) r 7
Starting program:/root/Program/testgdb/main 7

Breakpoint 1, main (argc = 2, argv = 0xbf89e694) at main. C: 14
14 int input_val = 0;
(GDB) N
15 if (argc <2)
(GDB)
20 input_val = atoi (argv [1]);
(GDB)
21 if (func1 (input_val ))
(GDB) S
Func1 (input = 7) at main. C: 6
6 if (input <10)
(GDB) N
7 Return-1;
(GDB) disassemble ---- decompile the current function
Dump of worker er code for function func1:
0x080483b4 <func1 + 0>: Push % EBP
0x080483b5 <func1 + 1>: mov % ESP, % EBP
0x080483b7 <func1 + 3>: Sub $0x4, % ESP
0x080483ba <func1 + 6>: CMPL $0x9, 0x8 (% EBP)
0x080483be <func1 + 10>: JG 0x80483c9 <func1 + 21>
0x080483c0 <func1 + 12>: movl $0 xffffffff,-0x4 (% EBP)
0x080483c7 <func1 + 19>: JMP 0x80483d0 <func1 + 28>
0x080483c9 <func1 + 21>: movl $0x0,-0x4 (% EBP)
0x080483d0 <func1 + 28>: mov-0x4 (% EBP), % eax ---- the return value is saved to the eax register.
0x080483d3 <func1 + 31>: Leave
0x080483d4 <func1 + 32>: Ret
End of worker er dump.
(GDB) I r ---- view the value of the current Register, mainly eax. At this time, "Return-1" has not been executed"
Eax 0x7 7
ECX 0x0 0
EdX 0x0 0
EBX 0x9f8ff4 10457076
ESP 0xbf89e5c4 0xbf89e5c4
EBP 0xbf89e5c8 0xbf89e5c8
ESI 0x8b0ca0 9112736
EDI 0x0 0
EIP 0x80483c0 0x80483c0 <func1 + 12>
Eflags 0x293 [Cf af SF if]
CS 0x73 115
SS 0x7b 123
DS 0x7b 123
Es 0x7b 123
FS 0x0 0
GS 0x33 51
(GDB) N
10}
(GDB) I r ----- view the information of the current Register. "Return-1" has been executed, and the value of eax has changed to-1.
Eax 0 xffffffff-1
ECX 0x0 0
EdX 0x0 0
EBX 0x9f8ff4 10457076
ESP 0xbf89e5c4 0xbf89e5c4
EBP 0xbf89e5c8 0xbf89e5c8
ESI 0x8b0ca0 9112736
EDI 0x0 0
EIP 0x80483d3 0x80483d3 <func1 + 31>
Eflags 0x293 [Cf af SF if]
CS 0x73 115
SS 0x7b 123
DS 0x7b 123
Es 0x7b 123
FS 0x0 0
GS 0x33 51
(GDB) set $ eax = 0 ---- use the set command to modify the value of eax. Unlike variables, add '$' before the register name to modify the value of the Register'
(GDB) I r ---- view the value of eax again, And eax has changed to 0.
Eax 0x0 0
ECX 0x0 0
EdX 0x0 0
EBX 0x9f8ff4 10457076
ESP 0xbf89e5c4 0xbf89e5c4
EBP 0xbf89e5c8 0xbf89e5c8
ESI 0x8b0ca0 9112736
EDI 0x0 0
EIP 0x80483d3 0x80483d3 <func1 + 31>
Eflags 0x293 [Cf af SF if]
CS 0x73 115
SS 0x7b 123
DS 0x7b 123
Es 0x7b 123
FS 0x0 0
GS 0x33 51
(GDB) n ---- continue to execute the code. The program output is "input numeric is bigger than 10 .". By modifying the register value, we control the execution process of the program code.
Main (argc = 2, argv = 0xbf89e694) at main. C: 24
24 printf ("input numeric is bigger than 10./N ");
(GDB) c
Continuing.
Input numeric is bigger than 10.

Program exited normally.
(GDB)

 

This test is very effective for some small programs.

 

 

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.