I need to debug my program, the problem is that this program takes couple of parameters. How Can I debug program which takes a parameters ?? Can I somehow modif开发者_StackOverflow社区y argc and argv parameters in runtime ??
The best way is not to modify the arguments at runtime but to debug an instance that has the arguments you want.
For Windows you can do this in Visual Studio as follows:
- Right click on project in Solution Explorer.
- Set the arguments you want in Configuration Properties -> Debugging -> Command Arguments.
- Hit F5 to start the program (after setting breakpoints where you want to stop).
Alternatively start the program up as normal from the command line, and attach the debugger afterwards.
If you're invoking the debugger from the command line you can just add your command line arguments and the debugger will pass them on to your program.
If you're using an IDE, there should be a way to set the arguments that will be passed to your program (for example, in Visual Studio it's in the project properties under "Debugging/Command Arguments").
However, if I'm in a debug session and I want to debug using a variety of different command line arguments, I find it painful to have to edit the project properties continually. For that reason, I'll often make sure to have my argc
/argv
parsing in a function that takes parameters instead of acting on argc
/argv
directly, and have conditionally compiled in debugging code that passes in a hard coded command line string (I find it easier to modify the string in the source file than to edit the IDE's project properties) or I have the debugging code prompt for command line arguments.
I have a handy little routine that'll parse a string into an argv
-style array, then I can pass that to the routine that normally parses argc
/argv
.
So things might look something like:
int main( int argc, char** argv)
{
if (debugging) {
char** dbg_argv;
int dbg_argc = argcargv( &dbg_argv, "dummyarg0 my debugging command --line");
parse_options( dbg_argc, dbg_argv);
}
else {
parse_options( argc, argv);
}
// etc...
}
It's not exactly pretty, but I find it more convenient than messing with project properties over and over.
If you are using GDB:
gdb ./a.exe
> break main
> run arg1 arg2 arg3 etc..
精彩评论