debugging a shared library wrapped by SWIG in perl - c++

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

Related

How do run executable c++ through terminal in debug mode?

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.

How to setup custom breakpoints in the C++ program?

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.

Can I set a breakpoint with gdb as I'm calling it?

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

How to set C++ breakpoint in eclipse when source is compiled with ccache?

Recently, our development team is starting to use ccache to do faster compile (the compile is done from sandbox /usr/x).
Now, when I compile from my sandbox (/usr/y), and try to set a breakpoint in the code in Eclipse (GDB (DSF) process launcher), it fails to find the file.
Further investigation shows that Eclipse gdb uses the complete path of the file to set a breakpoint (e.g. b /usr/y/untouchedFile.cpp:1234), but the actual path (in the gdb debugger) is actually /usr/x/untouchedFile.cpp.
The only thing that works is to set a breakpoint on the console by typing it, and do a source file mapping when the breakpoint is hit.
I would like to set the breakpoint by clicking on the code line (which used to work before ccache).
I was wondering if there is a way to get around this.
Thanks!

Analyzing core dump generated by multiple applications with gdb

I have a core dump generated by 2 applications -> /usr/bin/python and /usr/bin/app1.
I know the dump can be analyzed by
gdb /path/to/app /path/to/core
but is there a way to include both applications in the arguement?
I did try gdb '/usr/bin/python /usr/bin/app1' core.xxx but that doesnt seem right.
Any suggestions?
I think you cannot achieve what you want with a single invocation of gdb. But you could run gdb twice, in different terminal windows. I did that more than once, and it works quite well (except of course that your own brain could be slightly overloaded).
a gdb process can debug only one single program, with one single debugged process or (for post mortem debug) one single core file.
And a given core file is produced by abnormal termination of one single process (not several), so I don't understand your question.
Apparently, you have a crash in some execution of python probably augmented by your faulty C code. I suggest having a debuggable variant of Python, perhaps by installing the python3-all-dbg package or something similar, then use gdb on it. Of course, compile your C code plugged into Python with debugging enabled. Perhaps you violated some invariant of the Python garbage collector.