How to pass arguments and redirect stdin from a file to program run in gdb?

前端 未结 5 1302
青春惊慌失措
青春惊慌失措 2020-12-04 05:55

I usually run a program as :

./a.out arg1 arg2 

I would like to debug it using gdb.

I am aware of the set args

5条回答
  •  爱一瞬间的悲伤
    2020-12-04 06:17

    Start GDB on your project.

    1. Go to project directory, where you've already compiled the project executable. Issue the command gdb and the name of the executable as below:

      gdb projectExecutablename

    This starts up gdb, prints the following: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1 Copyright (C) 2016 Free Software Foundation, Inc. ................................................. Type "apropos word" to search for commands related to "word"... Reading symbols from projectExecutablename...done. (gdb)

    1. Before you start your program running, you want to set up your breakpoints. The break command allows you to do so. To set a breakpoint at the beginning of the function named main:

      (gdb) b main

    2. Once you've have the (gdb) prompt, the run command starts the executable running. If the program you are debugging requires any command-line arguments, you specify them to the run command. If you wanted to run my program on the "xfiles" file (which is in a folder "mulder" in the project directory), you'd do the following:

      (gdb) r mulder/xfiles

    Hope this helps.

    Disclaimer: This solution is not mine, it is adapted from https://web.stanford.edu/class/cs107/guide_gdb.html This short guide to gdb was, most probably, developed at Stanford University.

提交回复
热议问题