GDB series of one by one GDB session example

Source: Internet
Author: User
Tags builtin




Translation: shyboysby.spaces.live.com

This translation complies with the GPL. See:
GDB is free software, protected by the GNU General Public License (GPL). The GPL gives
Freedom to copy or adapt a licensed program-but every person getting a copy Also
Gets with it the freedom to modify that copy (which means that they must get access to the
source code), and the freedom to distribute further copies. Typical software companies use
Copyrights to limit your freedoms; The free software Foundation uses the GPL to preserve
These freedoms.
Fundamentally, the general public License is a License which says so you have these
Freedoms and that's cannot take these freedoms away from anyone else.
Welcome Reprint (Please specify the source), but do not allow commercial profit. This translation reserves the right to do so.
Free software requires free documentation.
Freedom belongs to the people.

GDB: Chapter I.
Chapter One: a GDB session example
11 Examples of GDB sessions
Feel free to use this manual to learn everything about GDB. However, some commands that take advantage of the hand are enough to start using the debugger. These commands are described in this chapter.
In this simple session, we emphasize that the user input is shown in bold, which can be clearly distinguished from the output of the environment.
The previous version of the GNU M4 (Universal macro processor) has one of the following bugs: Sometimes, when we change the default quote string for a macro, we use it to capture it in another macro.
A macro-defined command is invalidated. In the next short M4 example, we define a macro foo that expands to be "0000", and we then use the M4 built-in Defn to define the macro Bar,bar
The value is also "0000". However, after we use <QUOTE> to replace the open quote character with <UNQUOTE> instead of the closed-quote characters, the same process that defines a synonym Baz fails.
Baz
$ CD GNU/M4
$./m4
Define (foo,0000)
Foo
0000
Define (Bar,defn (' foo '))
Bar
0000
Changequote (<QUOTE>,<UNQUOTE>)
Define (Baz,defn (<QUOTE>foo<UNQUOTE>))
Baz
Ctrl-d
M4:end of input:0: Fatal error:eof in string
Let's try to use GDB to see what's going on.
$ GDB M4
GDB is free software and you were welcome to distribute copies
of it under certain conditions; Type "show copying" to see
The conditions.
There is absolutely no warranty for GDB; Type "Show Warranty"
For details.
GdB 6.8.50.20080307, Copyright 1999 free Software Foundation, Inc.
(GDB)
GdB simply reads only enough data to find out where to find subsequent content when it is needed; This will be the first hint of gdb that appears very open. Now we
Let GDB use a narrower-than-usual display area, so that the example of this book can be shown better.
(GDB) Set width 70
We need to explore how the M4 built-in function changequote works. Since we have seen the source code, we know that the relevant sub-function is m4_changequote, so we
Use the GDB break command to set a breakpoint on this function.
(GDB) Break M4 Changequote
Breakpoint 1 at 0x62f4:file builtin.c, line 879.
With the Run command, M4 is running under GdB's control. As long as you have not run to the M4_changequote child function, the program runs as usual:
(GDB) Run
Starting program:/WORK/EDITORIAL/GDB/GNU/M4/M4
Define (foo,0000)
Foo
0000
In order to set the breakpoint, we call Changequote. GDB suspends the M4 and displays contextual information at the point where it stops executing.
Changequote (<QUOTE>,<UNQUOTE>)
Breakpoint 1, M4_changequote (argc=3, Argv=0x33c70)
At builtin.c:879
879 if (BAD_ARGC (Token_data_text (argv[0)), argc,1,3))
Now we can run the next line of the current function with command N.
(GDB) n
882 set_quotes ((argc >= 2)? Token_data_text (Argv[1])
: Nil,
Set_quotes looks like a sub-function that might fail. In order to enter the sub-function, we can use the command s (step) instead of next. Step enters the called
The first line of the child function, so step enters Set_qoutes.
(GDB) s
Set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>")
At input.c:530
530 if (lquote! = def_lquote)
These outputs show the call function stack when the M4 hangs. It shows a summary of the stack. We can use the command BackTrace (or we can use the abbreviation BT) to see where it is currently:
The BackTrace command shows the stack for each active subroutine.
GDB) bt
#0 set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>")
At input.c:530
#1 0x6344 in M4_changequote (argc=3, Argv=0x33c70)
At builtin.c:882
#2 0x8174 in Expand_macro (sym=0x33320) at macro.c:242
#3 0x7a88 in Expand_token (obs=0x0, t=209696, Td=0xf7fffa30)
At macro.c:71
#4 0X79DC in Expand_input () at macro.c:40
#5 0x2930 in Main (argc=0, Argv=0xf7fffb20) at m4.c:195

Let's take a few lines to see what happens. At the beginning two times in order to avoid entering the Xstrdup subroutine, we use ' s '; the next two we use ' n '.
(GDB) s
0X3B5C 532 if (rquote! = def_rquote)
(GDB) s
0X3B80 535 Lquote = (LQ = = Nil | | *lq = = ")?
Def_lquote:xstrdup (LQ);
(GDB) n
536 Rquote = (RQ = Nil | | *rq = = ")? Def_rquote
: Xstrdup (RQ);
(GDB) n
538 Len_lquote = strlen (rquote);
The last line of output looks a bit weird; we can check the values of variables lquote and rquote to see if they're really the new we're looking for.
Left and right quotes. We use the command P (print)
(GDB) P Lquote
$ = 0X35D40 "<QUOTE>"
(GDB) P Rquote
$ = 0X35D50 "<UNQUOTE>"
Lquote and Rquote are really new left and right quotes. To see some of the relevant information, we can use the command L (list) to display 10 lines of code near the current code.
(GDB) L
533 Xfree (Rquote);
534
535 Lquote = (LQ = = Nil | | *lq = = ")? Def_lquote
: Xstrdup (LQ);
536 Rquote = (RQ = Nil | | *rq = = ")? Def_rquote
: Xstrdup (RQ);
537
538 Len_lquote = strlen (rquote);
539 Len_rquote = strlen (lquote);
540}
541
542 void
Let's step two lines (set Len_lquote and Len_rquote) and check the values of the two variables.
(GDB) n
539 Len_rquote = strlen (lquote);
(GDB) n
540}
(GDB) P Len Lquote
$9
(GDB) P Len Rquote
$4 = 7
Assuming that Len_lqoute and Len_rquote represent the lengths of Lqoute and Rquote respectively, it is clear that the result is wrong, and we can use the command p to set
A more reasonable value, the P command can not only print the value of an expression, but also the expression can call a child function, or you can assign a value to an expression.
(GDB) P len Lquote=strlen (lquote)
$7
(GDB) P len Rquote=strlen (rquote)
$6 = 9
Is this enough to correct the M4 built-in Defn about using new quotes? We can use command C to let M4 continue, and then try the example that just passed the problem:
(GDB) C
Continuing.
Define (Baz,defn (<QUOTE>foo<UNQUOTE>))
Baz
0000
It worked! This new reference is now as correct as the default. The problem seems to be that two of the formal parameters are mistaken. We enter an EOF to let M4 exit:
Ctrl-d
Program exited normally.
The message ' program exited normally ' is GDB output; it shows that M4 is done. We can use the QUIT command to end the GDB session.


Reprint http://zhiwei.li/text/2010/01/gdb%e6%89%8b%e5%86%8c1%e4%b8%80%e4%b8%aagdb%e4%bc%9a%e8%af%9d%e6%a0%b7%e4%be%8b/

If you have copyright issues, please contact QQ 858668791

GDB series of one by one GDB session example

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.