I am working on mirtCAT package in R. I need to debug inside a function in this package called mirtCAT(). There are many function inside this one which are written in c++. I need to see which function is taking which value. That is why I need to debug inside the c++ functions which I am trying to do using gdb. I am referring this document for the same:
http://r-pkgs.had.co.nz/src.html#src-debugging
When I am using the command R --debugger=gdb to start R on the command prompt
It is starting R but I think it is not starting the gcc compiler.
It is throwing the warning :
unknown option '--debugger=gdb'
I have changed my environment path variables for gcc compiler.
Any suggestions anyone have?
P.S. I also referred to this thread: Debugging (line by line) of Rcpp-generated DLL under Windows
Where #Dirk suggests to start R by using command R -d gdb
which is also not working it says::
unknown option '-d'
ARGUEMENT 'gdb' is _ignored_
Try :
gdb Rgui.exe
(gdb) break WinMain
Related
Background info: The C++ program(LAMMPS - an open source) takes in a input script that has all the commands to be executed. The executable is named "lmp_fedora", input script named "in.hit". The program's run command "./lmp_fedora < in.hit"
My Problem: I am trying to debug one of the .cpp files in LAMMPS.
My Attempts: 1. I tried "gdb lmp_fedora < in.hit", but it failed. 2. Also tried to find the pid of the running program using ps aux, but wasn't sure which id it was.
My Question: How do you debug a input script(that has commands linked to c++ project) using gdb?
You use the gdb run command:
$ gdb lmp_fedora
(gdb) run <in.hit
From the help:
(gdb) help run
Start debugged program. You may specify arguments to give it.
Args may include "*", or "[...]"; they are expanded using "sh".
Input and output redirection with ">", "<", or ">>" are also allowed.
With no arguments, uses arguments last specified (with "run" or "set args").
To cancel previous arguments and run with no arguments,
use "set args" without arguments.
When you say gdb foo < bar that means bar is input to gdb, not to foo.
I think what you want to use is the gdb command set args.
I have this C function in a huge code:
void test() {
char *arg[] = {"/bin/sh", 0};
execve("/bin/sh", arg, 0);
}
I am trying to debug this code using gdb
(gdb) call test()
process 1948 is executing new program: /bin/dash
warning: Selected architecture i386:x86-64 is not compatible with reported target architecture i386
Architecture of file not recognized.
An error occurred while in a function called from GDB.
Evaluation of the expression containing the function
(test) will be abandoned.
When the function is done executing, GDB will silently stop.
Hence the shell is not spawning. How to go about it?
gdb isn't allowing you to exec a binary with a different architecture, even though it's compatible on your platform. The same occurs if you try to exec a 32bit executable from a 64bit one. This occurs on the latest version (7.5.1) of gdb as well.
If you can compile your code as 32bit without it causing other problems, it would be a workaround.
As per Hasturkun's comment, there is a patch available here.
I am using gdb to debug NS-2 which is a simulator for network protocols. It takes an .tcl file as input and interpret it. [I think it is an interpreter.]
Some of the code is written in tcl (events and creation of network components) and some in C++ (especially Packet Formats, Agents etc.).
I have created an Agent in C++ and i want to stop it at some function call so that i can see the stack trace and find which other classes have been called before it.
This is what i have done:
There was some error in one of my MyAgent::function and it was giving Segmentation Fault and gdb was stopping there automatically. I could then see the stack trace. I rectified the error.
Now when i run
gdb ./ns
b MyAgent::function()
/*
When i press TAB after writing "b MyA" it gives me all functions
of my class :). when i press enter after above command --
it asks me "Breakpoint on future shared library load" and i say Yes.
I hope this is ok ??
*/
r myfiles/myWireless.tcl
Now it runs and do not stop anywhere. :(
I am sure that this function is being called, because when that Segmentation fault was occuring, it was stopping at that function.
Thanks
You can add a breakpoint in that function:
(gdb) break MyAgent::function()
You must make sure to compile with whatever options are necessary to get debug symbols. On GCC, use the -g or -ggdb options.
You need the -args option to specify the tcl script that will be executed.
Run gdb like this:
gdb -args ./ns path/to/tcl/script.tcl
To enable debug flag to c++ code, if have not done it already, re-configure your ns2 instalation with:
./configure --enable-debug ;# plus any other flags you use for configuring
make clean
make -j 3 ;# -j for faster compiling
make install ;# optional
You can also use the --with-tcldebug=..., for debugging tcl code (You need to install tcldebug first for this option)
I am a beginner and got some trouble in RE.
I have an ELF 'bomb' and an unknown file 'model.abc'.
The correct way to run bomb is:
bomb model.abc
Now I want to use gdb to see the value of some addresses when running it. Can any one help me?
First start gdb from a shell prompt:
$ gdb bomb
Then run your program from the (gdb) prompt with the command line you want:
(gdb) run model.abc
You need to launch your program this way because gdb doesn't allow you to specify command line arguments for your program on the gdb command line.
Another, more convenient way of debugging a program with arguments:
gdb --args program <arguments>
If you don't have symbols, you'll have to start from the entry point. To figure our where it is, use:
(gdb) info file
Symbols from "/.../tesprog".
Local exec file:
`/.../tesprog', file type elf32-i386.
Entry point: 0x804abc0
Then you can set breakpoint on it before running:
break *0x804abc0
Note that the entry will be most often the library startup code (ctr0.s), it might take a while to get to the actual code written by the programmer.
I'm trying to get gdb to run programs with input redirection to stdin. For example, without gdb I would run a program like this:
prog < input.txt
Now in gdb, the usual way to do this is run < input.txt. However, it doesn't work for me and when doing this nothing gets redirected into stdin.
I'm using Windows with MinGW. What could be the problem?
As far back as the late '90s, broken command line redirection was a known and assumed limitation. My suspicion is that it remains that way, since the mingw32 port of gdb still gleefully passes on verbatim all run arguments (including redirects) to the debugee.
Several possible workarounds:
if you have the option to alter the command line interface, then implement bbadour's suggestion
otherwise, if you can easily suspend the process before the point you want to debug at, invoke the debugee (with redirection) from a shell and attach to it while it is already running
otherwise, if you have symbols for the debugee (gcc -g) or you know the address of main() (gcc -Wl,-Map,mapfile) and can set a breakpoint there, proceed in the following manner (tested with mingw gdb 6.8.0):
# gdb debugee.exe
(gdb) b main
(gdb) run non-redirect-arguments-if-any
(gdb) p dup2(open("/tmp/input.txt", 0), 0)
(gdb) c
I ran into the same issue here, and I just got into the habit of adding a command-line argument to allow grabbing input from a file.
e.g. Parsing a "-i ifile" argument using argc and argv to get input from ifile instead of stdin and parsing a "-o ofile" to write output to ofile instead of stdout.
Then I just use those arguments instead of redirects.
The tools that come with MinGW often are not the latest versions and often have features omitted. ::shrug::
Input redirection is supported starting with GDB 8.0. From the NEWS file:
Native debugging on MS-Windows supports command-line redirection
Command-line arguments used for starting programs on MS-Windows
can now include redirection symbols supported by native Windows
shells, such as '<', '>', '>>', '2>&1', etc. This affects GDB
commands such as "run", "start", and "set args", as well as the
corresponding MI features.