Step into using GDB - gdb

I am performing some analysis of code using gdb. Just suppose the program is like
#include<stdio.h>
getinput()
{
char buffer[8];
gets(buffer);
puts(buffer);
}
int main()
{
getinput();
return 0;
}
I have complied it using gcc withsome other switches as mentioned below:
gcc -ggdb -mpreferred-stack-boundary -fno-stack-protection -o demo demo.c
On analysis with gdb, when I insert break point on getinput() and gets(buffer) function..
Then using "s" step through function.. on gets(buffer) function..
it does not ask for the user input, rather it asks for userinput. GDB directly steps into function gets...but i don't want it to move into gets functions..
(gdb) break 6
Breakpoint 2 at 0x8048441: file demo.c, line 6.
(gdb) s
The program is not being run.
(gdb) run
Starting program: /root/BufferOverflow/demo
Breakpoint 1, main () at demo.c:11
11 getinput();
(gdb) s
Breakpoint 2, getinput () at demo.c:6
6 gets(buffer);
(gdb) s
_IO_gets (buf=0xbfffeda8 "y\204\004\b") at iogets.c:32
32 iogets.c: No such file or directory.
(gdb)
I am getting the error shown above. Can anyone please help me regarding this ???

Then using "s" step through function.. on gets(buffer) function..
Using step command on line 6 will step into gets() function. If you don't want this, use next command. It will move to the next line of code in getinput() function (to puts() call).

gdb has a variety of ways to advance through a program. An excellent overview can be found here.
The most common sequence will be something like:
break [line or function/method]
run args
next
print [interesting variables]
The next will skip any function calls on the line and move to the next source line in the current function. The step will step into a function.
If you accidentally step into a non-interesting function the finish command will run until the end of the current function.
A common case is stepping into a function that is evaluating an argument.
my_method(gets(buffer));
A step on this line will lead you into the assembler of the gets() function. Usually this is not what you want. For such cases, I will usually just set a breakpoint on my_method and then cont.
gdb tries to avoid this situation with the step-mode setting. It doesn't always work however as you see in your example.
gdb is a great tool that will save you time and misery. If you are working as a developer then certainly take time to master it.

Related

gdb print file name line number when program is running

I need to debug a flow using gdb - I do not know the call stack hence cannot set break points and going thru first entry points will be really very tedious in project code that runs thru thousands of line.
In same regards is there a way that when I start the program execution via gdb we enable some commands (after some initial breakpoint) - hence when the program starts processing further on it print file name line number without user interaction - something like code flow itself?
Well I want to list lines of code when executing via GDB - like we do
when breakpoint is set and we run 'step'.
You can run step in infinite loop like this:
(gdb) start
Temporary breakpoint 2, main () at ttt123.cpp:23
23 vector<A> v1;
(gdb) while 1
>step
>end

GDB: disable printing of current line after every step

The GNU gdb commandline debugger prints the line it is currently on after every step and next command. Consider the following gdb session where I step through some code:
...
Temporary breakpoint 1, main (argc=1, argv=0x7fffffffd848)
at src/main.cc:3
3 int main(int argc, char **argv){
(gdb) next
4 Printf("Hello World\n"); // <--- disable this
(gdb)
5 printf("Hello World 2\n"); // <--- disable this
(gdb)
Is there a gdb setting to disable this printing? I know this is technically possible because the gdb TUI has exactly the behaviour i'm looking for (accessible through gdb command set enable tui).
Thanks!
I achieved it through redirection:
define n
set logging file /dev/null
set logging redirect on
set logging on
next
set logging off
display
end
I found that capturing the output of next did not work using gdb.execute (gdb's python API). I expect that this is the case because the source line is not printed by next itself, but by the stop event that is triggered.
There is no straightforward way to do this when using the gdb CLI. The code that handles printing the "stop" to the user does not check anything that the user can set.
One way you could try solving this would be to alias n to a command that runs the command using a different interpreter, like interpreter-exec tui next. I am not sure if this will actually work.
Another way to accomplish this would be to write a Python command named n that uses gdb.execute to invoke next -- while capturing the output and ignoring it. This approach is somewhat dangerous because sometimes you probably do want some of the stop message, just not the source display.
The best approach would be to modify gdb to add a new set command to disable the source printing. This is easy to do.

Debugging attached process with gdb - how to escape from a loop

I am debugging code that looks like this:
while (true){
// do something ...
size_t i = foo(); // <- bp set here
if (flag_set) break;
}
// More code follows here ...
I want to break at the foo() function call, invoke it a few times and then jump out of the while loop completely (lets assume that we are guaranteed that the flag will be set - so we can break out of the loop.
How do I break out of the loop completely?. finish simply runs to the next iteration. What I want to do is to exit the current "code chunk" (in this case, the while loop)
You want the advance command, which takes the same arguments as the break command. Using your code as an example (but with line numbers added):
10 while (true){
11 // do something ...
12 size_t i = foo(); // <- bp set here
13 if (flag_set) break;
14 }
15
16 // More code follows here ...
17 someFunction();
Say your original breakpoint on line 12 was breakpoint 1, and after breaking a few times you wanted to skip to line 17, you would type something like:
disable 1
advance 17
which would disable breakpoint 1 (so it doesn't get hit for the rest of the loop) and then keep executing the program until it hit line 17.
Set a breakpoint before the loop. Then cursor to the foo() call, and use Debug|Run to Line. This is so useful that I have dedicated a function key to it.
Set a second breakpoint after the loop. disable the breakpoint inside the loop. cont. enable the breakpoint again.
I don't know of any easier way.
Try using the jump command. Per gdb help, on this system at least:
jump -- Continue program being debugged at specified line or address
What you need is until command. This is the easiest way to avoid stepping through the loop. From gdb manual:
Continue running until a source line past the current line, in the current stack frame, is reached. This command is used to avoid single stepping through a loop more than once. It is like the next command, except that when until encounters a jump, it automatically continues execution until the program counter is greater than the address of the jump.

GDB question - how do I go through disassembled code line by line?

I'd like to go through a binary file my teacher gave me line by line to check addresses on the stack and the contents of different registers, but I'm not extremely familiar with using gdb. Although I have the C code, we're supposed to work entirely from a binary file. Here are the commands I've used so far:
(gdb) file SomeCode
Which gives me this message:
Reading symbols from ../overflow/SomeCode ...(no debugging symbols found)...done.
Then I use :
(gdb) disas main
which gives me all of the assembly. I wanted to set up a break point and use the "next" command, but none of the commands I tried work. Does anyone know the syntax I would use?
try using ni which is nexti. equivalent is si which is step instruction
nexti if you want to jump over function calls.
stepi if you want to enter a function call.
The following documentation is very helpful; it has a list of all the important commands you could use on gdb.
X86-64: http://csapp.cs.cmu.edu/public/docs/gdbnotes-x86-64.pdf
IA32: http://csapp.cs.cmu.edu/public/docs/gdbnotes-ia32.pdf

help for gdb's stepi command

I need to trace all instrutions of a program using gdb.
After every execution of a instruction, I want gdb invokes a specified function.
Is it a possiable work? How to achieve this?
I searched internet and found "stepi arg" command in gdb could step arg instructions.
But how to find total number of instructions?
After every instruction, how to make gdb to invoke my function automately?
cat t.c
int main() { int x=1; int y=2; int z=x+y; printf("%d",z); return 0; }
gcc t.c
gdb -q ./a.out
break main
run
(no debugging symbols found)...
Breakpoint 1, 0x0000000000400488 in main ()
set logging on
while 1
>stepi
>info registers
end
quit
Now examine gdb.log: it should contain the info you are seeking.
P.S. This isn't a discussion forum. Please don't append questions as "answers". Instead edit your original question to clarify it, or use comments.
GDB always prints "---Type to continue, or q to quit---" during execution because of the height or pagination parameter.
In order to avoid or disable this you have to give the following command either in gdb prompt or .gdbinit file
set height 0 or set pagination off