I'm dynamically compiling kernels for cuda. Is there some way to add debugging information to the binary that is dynamically generated from the run-time generated assembly string?
I want to use cuda-gdb on it.
Thanks.
PS Can someone please create a tag cuda-gdb?
I am assuming that you are creating a .cubin file from a .cu file using nvcc, and later using cuModuleLoad, cuLaunchGrid and friends to actually launch your kernel. In that case, you can pass "-G" to nvcc when converting .cu to .cubin. Then, you can run your program under cuda-gdb and place a breakpoint in the kernel and that breakpoint should be triggered.
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 created a simple cpp file and compiled it using the cygwin g++ compiler in Win7. I am now trying to debug the resulting executable in gdb, but I can't get it to behave the way I expect it to. I cannot place breakpoints because when I try to execute b file.cpp:25 I get back
No source file named file.cpp.
Make breakpoint pending on future shared library load? (y or [n])
I select y and it still does not break at the expected point. I did compile from this source.
I am getting a segfault at a certain point and whe also does not actually show line numbers. It seems to show memory addresses, which is obviously not useful to me.
Is gdb is misbehaving or am i just expecting it to do things it can't do? If it doesn't have this capability (though I've done this kind of thing before), is there another tool I can use?
In order to add debug information during compilation you should use the -g flag for g++.
I am currently developing a small grading application (for a C++ course) that will compile students' .cpp files and compare the output to a master. I'm using QT Creator to develop the UI as well as the back-end component (including some SQLite integration), and I have struggled with finding a way to compile the files automatically and discretely.
I originally developed a small application in VS2010 that utilized the VS Command Line Compiler - it works, but only in debug mode (when the program is executed from within cmd.exe). When I moved to Release, the compile command ("cl") was not recognized. Here is a small code segment:
int main()
{
system("vcvarsall.bat"); // To set up VS Command Prompt environment
_putenv("INCLUDE=<my include paths>"); // Include paths for compilation
_putenv("LIB=<my lib paths>"); // Include paths for linking
system("cl /EHsc \"Student1.cpp"); // Compilation code
return 0;
}
When executed in Release, both "cl" and "cl.exe" are not recognized as commands. I am executing a variant of this program as a .exe file from within the QT application, passing a file name to this function, so it needs to be in Release form. What can I do to utilize the Command Line Compiler from within this application?
Thanks!
I have no teaching background, but my first suggestion is to have your students provide a makefile. NMake is system for VC, so you could try that.
If you still would like to compile using your own code I would recommend SCons instead of a console application. SCons and Python are free and very easy to setup. Your example becomes:
Program(target='Student1App', source=['Student1.cpp'])
You can call scons from within a C++ application if you choose, and after the call, assuming no build errors, the executable will be available.
I recently wrote an implementation of the Canonical Huffman compression algorithm. I have a 500kb test file that can be compressed to about 250kb when running the debug and release builds from within Visual Studio 2008. However when I run the release build straight from the executeable the test file only compresses to about 330kb.
I am assuming that something is going wrong when the file is written using fwrite(). I have tested the program and confirmed that uncompressing the files always produces the correct uncompressed file.
Does anyone know why this could possibly be? How could the same executeable file be producing different sized outputs based on where it is launched from?
Sounds like an uninitialized value somewhere. See also:
Program crashes when run outside IDE
Running in IDE will initialize values to global defaults, running outside IDE doesn't, so any uninit'd variables will have different values.
Check out the the /RTCu compiler option to help detect use of uninitialized variables.
Try running it through App Verifier and see what it finds.