I have a c++ executable file named test .. To execute it in my terminal I run.....
./test
Although I want to run it in debug mode wherein it shows the exact command being used immediately after being executed
If you are wondering what exactly I mean by debug mode..
Just like how we use -x for shell scripts
sh -x test.sh
OR
bash -x test.sh
This shows every command immediately after its executed .
I want same thing for this test c++ executable file.
I hope there would be some way.
To debug a c++ program you need to:
1. Compile the program with debug information.
You need to tell the compiler to include information about symbols in the executable to be able to debug it later (at least to debug it in an easy way). For example if you use g++, add the -g option)
2. Run the program with attached debugger
Since your question is tagged with linux, you may want to use gdb. There also exist tools that provide a gui.
You cannot execute C++ source files. You have to first compile them into executables. Then you run the executable. C++ is not an interpreted scripting language.
Related
I want to know how can I trace the execution of a C/C++ program during the running time? I am working on a new already existing code base, which is a large project. I want to run the project in a common use case, and I want to see which functions or methods get called during the running time. I want to run the C/C++ program, and be able to trace which functions, and which statements get executed in this specific use case.
I am familiar with strace and ltrace, which trace the execution of the program's system calls and library functions respectively. I am looking for something similar, but instead I want to see which functions of the project itself are executed during the running time of the program. Is there a specialized tool for that?
Alternatively, can I run the program in gdb without setting any breakpoints that would block the program so that it could run through in real time, and configure gdb to step into all the functions of the project, but not step into any library or system calls, and have gdb echo out all the functions that it steps into and all the statements that it executes into a log file?
I have the source code, and I can compile it with -g -O0.
-gp switch to gcc and gprof will help, but you'll need the source of your program to compile.
I'm working on a project, where I cannot disclose the details of the code. So, the application is all written in C and C++. Since, a particular file which wanted to debug has a lot of dependencies and exports, I need to debug the whole project. How do I set breakpoints in the code itself so that the debugging would stop at that particular point? I'm using Ubuntu 14.04 (since the project is compatible with this environment) and gdb debugger.
I've tried using
#include <csignal>
// Generate an interrupt
std::raise(SIGINT);
But I keep getting error
error: ‘raise’ is not a member of ‘std’
Even this also didn't work
#include <signal.h>
raise(SIGINT);
Plus the debugging wont stop at that point, so that I could foresee the function at that point. I only want to debug it from console, rather using any IDE.
Since the programfile I want to debug has lot many header files which it imports, I'm unable to make a executable to use gdb. So, while make clean build of my MakeFile I want to debug the particular program file at a particular function. So, for that I want to add breakpoints in the program. I cannot use any GUI for debugging since I should not use.
Have you tried to use GDB Commands?
b lineno - set a break point at line 'lineno'
b srcfile:lineno - set a break point in source file 'srcfile' at line 'lineno'
Read more about debugging with gdb. Be sure to compile all your code with DWARF debug information (so use g++ -Wall -Wextra -g to compile it with GCC).
GDB is extensible and you can define your own gdb commands at startup in your init file, probably .gdbinit and put some initial commands there.
BTW, on Linux, debugging (so the gdb debugger) is using ptrace(2) facilities. And you can use gdb non-interactively on the command line, using scripts.
How do I set breakpoints in the code itself
I don't recommend adding specific C code for breakpoints. So don't do that in your C code. But see also this.
Perhaps you want some backtrace library, like Ian Taylor's libbacktrace ?
I cannot use any GUI for debugging
You don't need to. You'll use gdb on the command line. With an appropriate gdb script, you can even use it non-interactively (e.g. in a Makefile)
I only want to debug it from console, rather using any IDE.
Please realize that IDEs are only glorified source code editors capable of running other external tools (including the GCC compiler and the gdb debugger). You certainly don't need -on Linux- any IDE to run a compiler or a debugger (but IDEs could be convenient, but not necessary, for that), because you can (and should) run your compiler, your debugger, your build automation tool, on the command line.
Since the program file I want to debug has lot many header files which it imports, I'm unable to make a executable
You should fix that first. You need to make an executable. BTW, there is no "import" involved at run time, since header files are relevant only at compile time. Read more about the cpp preprocessor. You probably should invoke GCC (e.g. the g++ compiler, since you have C++ code) with appropriate preprocessor options (sometimes, tools like pkg-config are useful for that). You probably should use some build automation tool such as GNU make (with your Makefile; see this for inspiration) or ninja. You could add ad hoc gdb commands to your build procedure (e.g. with some additional and specific rules and/or recipes in your Makefile).
First, make sure you have compiled with -g. There are other gdb specific flags in gcc. You could add them in too.
Try using ddd, the graphical version of gdb. Great tool if you don't know the gdb command line. Just open up the relevant source file, select the line then click on breakpoint on the toolbar. It will tell you on the console section, what command was actually executed (good way to learn). There is a floating button list with run, next etc. for stepping through your code.
ddd will work on most of the gcc toolchain.
EDIT:
Say your code is made up of 2 files main.cpp and child.cpp. main.cpp contains main(). The executable is called a.out.
To start
ddd a.out &
It will open in main.cpp. To put a breakpoint in child.cpp, click on File/Open Source... and select child.cpp. Then scroll to where you want a breakpoint. Put your cursor on the line, then click on break in the toolbar.
To run, either type run in the gdb window below or click on Run in the floating button dialog.
I'm in the middle of a large debugging project, and every time I start running gdb I have to type b 253.
It would be really nice if I could set my run script so that gdb loads with that breakpoint already set.
To be more explicit: Here are the contents of run.csh:
gdb --args path/to/program arg1 arg2
Can I modify this so that, once I run it, I can just type r and the program breaks on line 253?
Yes. Read documentation of gdb.
You can extend GDB. You can have Canned Sequences of Commands.
You can define or use extensions in Python, in Guile. See also this.
(you might need to recompile GDB itself from source, since sadly not all usual gdb are configured with Guile support)
You can have your .gdbinit file (read about startup files and command files). Btw you might prefer to break in function names, not in line numbers there. Read more about specifying locations.
Actually, many large projects have some .gdbinit (perhaps generated) in their source repository.
Be sure to use a recent version of GDB. The latest one (in March 2018) is GDB 8.1
Compiled a C++ program using gcc -pg -g (at least, those are the args I gave in the Makefile; don't have any hard evidence of what command was executed). Program ran to normal completion with CWD set to my home directory. No gmon.out file written.
gcc is 4.4.7. OS is centos 6.
My program was launched by a hand-rolled Perl daemon using fork/exec. I've verified that the CWD is my home directory, and that it's writeable, by having the daemon execute touch foo just before exec'ing my target program. As far as I've been able to research, this shouldn't have affected the program's profiling or writing gmon.out when it terminated (normally).
Ran into this same issue, g++ 4.8.2 on CentOS 7. -pg was present for both compiling and linking, run the process & it exits normally, no gmon.out generated.
I fixed this by replacing a call to _exit(status) with exit(status). Note that the former is _exit(3), a system call, and the latter is exit(2), a standard library method.
Why does this work? From the gprof man page:
The profiled program must call "exit"(2) or return normally for the profiling information to be saved in the gmon.out file.
Apparently the writing of gmon.out is dependent on (the higher-level) exit(2). So, check to be sure the code is using exit(2) (from stdlib) and not _exit(3) (system call).
This is really late, but for those of you who are struggling, after you compile your code with -pg, you need to run the executable for it to generate gmon.out
Maybe you have solved this months ago but I encountered the effect today so I can answer for future visitors:
No error message is shown, gmon.out just does not get created (and the analysis text-file will be empty).
One reason why this might be is if you have no main method or in the case of -mwindows a WinMain. E.g. if you use the compiler arguments (gcc) -e or (vc) /entry or using __main.
I looked over the gprof manual but did not find info about how to tell it an entry point so I changed the code.
In my case, the issue was that the executable was chdir'ing (changing the current working directory) to elsewhere, and that's where gmon.out ended up.
I ran my program with strace to see system calls and could see that it was writing the gmon output at the end of the trace.
I have wrapped my C/C++ code using SWIG in Perl. I have few segmentation fault because of the wrapped code. I am trying to use ddd with the Perl script but unfortunately even if I set a breakpoint on a line of the script ( the one calling C/C++ code ), ddd is not able to step in down to the C/C++ code.
Is there any way to set breakpoint into my C lib when I am debugging Perl code or do you know a good way/tool to debug the C lib when I am running this Perl script?
I am using Linux/gcc.
I did a simple thing. I called the gdb directly on perl interpreter.
gdb /usr/bin/perl
(gdb) r myscript
#block the script someway or rerun it
(gdb) b whatever_my_function
It seems that once the scipt is running shared memory is also loaded in memory. Once this happened I have available all information, functions and breakpoint for debugging.
I've only used SWIG for calling C++ from TCL, and debugged it using Visual Studio, but the same ideas should apply for your case as well. I'll describe what I've done to debug, hopefully you can figure out how to apply it to your situation.
Build a debug version of the C++ module
Make sure the TCL script is including the debug version (the path in the TCL load command points to the debug version of the module)
Place breakpoints in the C++ code
Invoke the TCL script through the Visual Studio debugger; for instance the command used is tclsh85.exe MyScript.tcl
HTH