text GDB(GNU调试器)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text GDB(GNU调试器)相关的知识,希望对你有一定的参考价值。
GDB ( The GNU Debugger )
In CS50 IDE there's a graphic tool to use for GDB.
But if you are no in CS50 IDE, you might have to use GDB by typing command lines. So here we will learn how to use it this way.
To open up and start using GDB, we type in the command line:
gdb <program name> // the program name is the compiled executable program, the source file, not the original c program. So the program name after "gdb" would not be "hello.c", but should be "hello" instead.
For example:
gdb hello
You will know that you've entered the GDB enviroment by noticing that the prompt before the curser shows:
(gdb)
After we enter the GDB enviroment, the first thing we want to do usually, is to set up a break point:
b [ function name, line number ]
The break point can be at the start of a function, or a specific line number.
b main
So after we've set our break point, we want to start running the program. Once it started running, it will break at the break point that you've just set.
So we type the command "r" to start running the program. If the program doesn't have any command-line arguments, you just type "r".
But if there are some command-line arguments, you add those after "r":
r [ command-line arguments ]
After you've set a break point, you can use following commands to proceed slowly, so you can find out where the problem is:
n Will step forward one block of code.
So this will not step into the current function, but run it, and go to the line right after this function.
s Will step forward one line of code.
So this will actually step into the function, and run it one line at a time. So you can use this command if you think the problem might lie inside this function.
p [variable] Prints out the value of the variable given. If you what to know the value of a specific variable then you can use this command.
info locals Prints out the values of all local variables. So if you want information of all of the local variables, you can use this command.
bt Means "back trace". Shows you what series of function calls have led you to the current point in the program. In more complex programs, it might be that "main" calls a function, and then this function might call another function, and then this function might call another function. Several nesting going on there. Also sometimes we do recursion in our functions.
So if you are inside the function "h", and you want to know how did you get here. You can type "bt", and then the result might show:
h g f main
So this means you are currently in function "h", because it's the first function name showing here. And now you know what happened so far, is that "main" called "f", "f" called "g", and then "g" called "h". And that's where I'm currently at in this moment. So this could be helpful for finding out exactly where things are.
q Quits GDB. When you want to get out of the GDB enviroment.
以上是关于text GDB(GNU调试器)的主要内容,如果未能解决你的问题,请参考以下文章
gnu ld/gdb:单独的调试文件。当需要链接的调试信息过多时如何生成调试文件?