How to passing input data in GDB mode for programming C. Already passed parameters and run program

10,565

For real, just type it in. Sample session:

paul@local:~/src/c/scratch$ gdb ./deb
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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 "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/paul/src/c/scratch/deb...done.
(gdb) list
1   #include <stdio.h>
2   
3   int main(void) {
4       char buffer[100];
5       fgets(buffer, 100, stdin);
6       printf("You entered: %s", buffer);
7       return 0;
8   }
(gdb) break 4
Breakpoint 1 at 0x400644: file deb.c, line 4.
(gdb) run
Starting program: /home/paul/src/c/scratch/deb 

Breakpoint 1, main () at deb.c:5
5       fgets(buffer, 100, stdin);
(gdb) n
Hello, world!
6       printf("You entered: %s", buffer);
(gdb) n
You entered: Hello, world!
7       return 0;
(gdb) continue
Continuing.
[Inferior 1 (process 4290) exited normally]
(gdb) 

The Hello, world! after the first n was just typed in normally.

Share:
10,565

Related videos on Youtube

zuhakasa
Author by

zuhakasa

Updated on May 25, 2022

Comments

  • zuhakasa
    zuhakasa almost 2 years

    I already know how to pass parameters in GDB mode by running: "run parameters". However, when continuing to debug by using n or s to go, I would like to pass data to my program, let say a text/string. For example, I want to send a string as "Testing" to my program because my program always waits to receive command from console. If I type "Testing" it will say "undefined command: "Testing". Try help".

    (gdb) b 100
    (gdb) run "pass parameters to program here"
    (gdb) n 
    (gdb) Now I want to send a string to my program, how can I do it?
    

    So how can I do to send this text to my program while debugging GDB in step mode? Thanks very much.

    • zuhakasa
      zuhakasa almost 10 years
      Hi Paul, what do you mean type it in? For example, after sometimes using n or s to go, now I want to send a text as "Testing" to my program because my program always waits to receive command from console. If I type "Testing" it will say "undefined command: "Testing". Try help". So how can I do to send this text to my program? Thanks very much.
    • Josh
      Josh almost 10 years
      You want the program to run while accepting input from the user, then return control to the debugger after that input is entered?
    • zuhakasa
      zuhakasa almost 10 years
      Yes, I mean exactly this. Can GDB or other debuggers do it to debug C program?
  • zuhakasa
    zuhakasa almost 10 years
    Thanks very much. I got it.
  • Crowman
    Crowman over 8 years
    @TimothyLeung: What if it is? How are you going to get that input into your program outside of the debugger? Do it the same way in gdb.
  • Timothy Leung
    Timothy Leung over 8 years
    The input require some information from the output of the prorgram. So I have to run the program first before I can input the data which are non-printable ascii characters. I hope I make my question clearer :) Thanks.
  • Crowman
    Crowman over 8 years
    Not really clearer, no, I have no idea what your question actually is. Furthermore, if you have a particular question, you should ask an actual question, rather than ask it in comments to a different way question.