0

This is a follow-up question that originates from the answer from thiefmaster to this question: gdb input redirection using cygwin

I am a beginner, so kindly pardon my ignorance.

I want to know how to redirect input to gdb using a text file while using a command-line argument? if you can elaborate the answer, it would be better. When should I enter command-line argument -f filename. i.e. i tried following but it didn't work

gcc -g testing3.c -o testing 3.exe
gdb testing3.exe
b main // to ensure it breaks on main
run -f userinput.txt
  • I have installed cygwin for compiling, running and debugging c programs. (CYGWIN_NT-10.0-18363 version 3.3.3-341.x86_64)
  • My userinput.txt is also saved in the same location as the pwd of gdb

output of gdb is like follows:

I have tried all the following ways, but none seem to work:

  1. attempt#1

    gdb ./testing3.exe -i userinput.txt

output of gdb is

Interpretor 'userinput.txt' unrecognized
  1. attempt#2

    gdb -ex 'set args < userinput.txt' ./exercise5-7.exe

gdb output is as follows: basically, it still asks for input from STDIN interactively rather than reading from userinput.txt

51009396@NHQ-GF-51009396 /cygdrive/d/Let us C/Practice
$ gdb -ex 'set args < userinput.txt' ./exercise5-7.exe
GNU gdb (GDB) (Cygwin 10.2-1) 10.2
Copyright (C) 2021 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-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

    For help, type "help".
    Type "apropos word" to search for commands related to "word"...
    Reading symbols from ./exercise5-7.exe...
    (gdb) r
    Starting program: /cygdrive/d/Let us C/Practice/exercise5-7.exe < userinput.txt
    [New Thread 28036.0x1a48]
    [New Thread 28036.0x6cbc]
    [New Thread 28036.0x5ab0]
    readlines: user input would start shortly
    [New Thread 28036.0x4f74]
    i am
    manually
    typing the
    input
    writelines
    i am
    manually
    typing the
    input
    [Thread 28036.0x1a48 exited with code 0]
    [Thread 28036.0x6cbc exited with code 0]
    [Thread 28036.0x6e30 exited with code 0]
    [Thread 28036.0x4f74 exited with code 0]
    [Inferior 1 (process 28036) exited normally]
    (gdb)
  1. attempt#3

    gdb ./testing3.exe b main r call dup2(open("userinput.txt",0),0)

gdb outputs

'open' has unknown return type; cast the call to its declared return type

Edit #2: basis comments from matzeri and ssbssa

I tried the suggestion. following is the gdb output:

51009396@NHQ-GF-51009396 /cygdrive/d/Let us C/Practice
$ cat userinput.txt
God
is
great

51009396@NHQ-GF-51009396 /cygdrive/d/Let us C/Practice
$ gdb exercise5-7.exe
GNU gdb (GDB) (Cygwin 10.2-1) 10.2
Copyright (C) 2021 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-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

    For help, type "help".
    Type "apropos word" to search for commands related to "word"...
    Reading symbols from exercise5-7.exe...
    (gdb) b main
    Breakpoint 1 at 0x100401090: file exercise5-7.c, line 12.
    (gdb) delete 1
    (gdb) i b
    No breakpoints or watchpoints.
    (gdb) run < userinput.txt
    Starting program: /cygdrive/d/Let us C/Practice/exercise5-7.exe < userinput.txt
    [New Thread 22400.0x70d4]
    [New Thread 22400.0x70e0]
    [New Thread 22400.0x719c]
    readlines: user input would start shortly
    [New Thread 22400.0x1bec]
    God
    is still
    great
    writelines
    God
    is still
    great
    [Thread 22400.0x70f0 exited with code 0]
    [Thread 22400.0x70e0 exited with code 0]
    [Thread 22400.0x70d4 exited with code 0]
    [Thread 22400.0x1bec exited with code 0]
    [Inferior 1 (process 22400) exited normally]
    (gdb)

it basically asks for user input from the STDIN and ignores the userinput.txt file. the messages readlines: use input would start shortly and writelines are my print statements in the code.

Singh
  • 55
  • 1
  • 6
  • I also tried using following: 'gdb -f userinput.txt' but message displayed by gdb was '"/cygdrive/d//Let us C/Practice/userinput.txt" is not a core dump: file format not recognized – Singh Jan 06 '22 at 09:45
  • 1
    `run < userinput.txt` see https://sourceware.org/gdb/onlinedocs/gdb/Input_002fOutput.html – matzeri Jan 06 '22 at 12:26
  • @matzeri sorry for not informing. I tried this already. it doesn't work. Also, I checked the link which you mentioned, it doesn't work – Singh Jan 06 '22 at 13:07
  • "doesn't work" doesn't tell us much. Please show what gdb outputs when you try it. – ssbssa Jan 06 '22 at 21:06
  • Does it work if you try the same on bash, without gdb? Like `testing3.exe < userinput.txt`. – ssbssa Jan 07 '22 at 12:14
  • @ssbssa @ssbssa yes it works `51009396@NHQ-GF-51009396 /cygdrive/d/Let us C/Practice $ ./testing3.exe < userinput.txt input ths string printing the string God is great 51009396@NHQ-GF-51009396 /cygdrive/d/Let us C/Practice` – Singh Jan 07 '22 at 14:31

0 Answers0