I have a question about debugging a running C++ program in Linux. If a programming is already running and can't be interrupted, how to do that.
I can find three ways, but I don't know too much about details, I am grateful if any one can elaborate it deeper.
1) we can use GDB by specifying the process ID
gdb -p PID
In this case, what's the difference between this and attach PID?
2) We can use pstat, however, I am using Ubuntu, no pstat, but only mpstat
it seems that mpstat does not provide too much information, and not so many options.
3) check the details information under directory ./proc
In this case, just go to the directory with the PID. However, should this be done mannually?
I can't find -p option in gdb man or their documentation, but it does work! I've tried it many times with older versions on RedHat and 7.0.1 on Debian.
I'm not sure how exactly it finds the exe by PID (maybe /proc/<PID>/exe), but it does. Since it's not described in their documentation, perhaps it not the most recommended way, but I haven't had any problems with it.
There's no noticeable difference between gdb -p <PID> and running gdb and in the their shell typing attach <PID>.
I personally prefer ps xa| grep myprogram for getting the PID
In regards to technique 1, there is no -p flag and you still need the name of the program:
gdb prog PID
There is no difference between doing that vs running gdb prog and then telling gdb attach pid.
Use ps -ef | grep <your program> to get the PID. Then run gdb <your program> <PID>.
pstat is not a standard command. I've only used it with Solaris.
e.g.
gayan#gayan:~/FE/bin> ./fe&
[1] 5866
gayan#gayan:~/FE/bin> ps -ef | grep fe
gayan 5866 5836 2 10:19 pts/3 00:00:00 ./fe
gayan 5871 5836 0 10:19 pts/3 00:00:00 grep fe
gayan#gayan:~/FE/bin> gdb fe 5866
GNU gdb (GDB; openSUSE 11.1) 6.8.50.20081120-cvs
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i586-suse-linux".
For bug reporting instructions, please see:
<http://bugs.opensuse.org/>...
Attaching to program: /home/gayan/FE/bin/fe, process 5866
The above was run on openSuse but should work on Ubuntu.
Related
I'm trying to get remote debugging working in QEMU for the sifive_u machine. All tools are from the Arch Linux repositories:
➜ qemu-system-riscv64 --version
QEMU emulator version 4.2.0
Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers
➜ riscv64-linux-gnu-gdb --version
GNU gdb (GDB) 8.3.1
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
I'm starting the machine as follows:
qemu-system-riscv64 -M sifive_u -m 256M -bios default -nographic -S -s
When I connect the debugger, I attempt to continue execution, but nothing happens; if I detach the debugger, the OpenSBI splash prints to the serial console. A typical gdb session looks something like this:
GNU gdb (GDB) 8.3.1
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=x86_64-pc-linux-gnu --target=riscv64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) target remote :1234
Remote debugging using :1234
warning: No executable has been specified and target does not support
determining executable automatically. Try using the "file" command.
0x0000000000001000 in ?? ()
(gdb) info thread
Id Target Id Frame
* 1 Thread 1.1 (sifive-e51-riscv-cpu harts[0] [running]) 0x0000000000001000 in ?? ()
(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x0000000080005a52 in ?? ()
(gdb) info thread
Id Target Id Frame
* 1 Thread 1.1 (sifive-e51-riscv-cpu harts[0] [halted ]) 0x0000000080005a52 in ?? ()
(gdb) detach
Detaching from program: , process 1
Ending remote debugging.
[Inferior 1 (process 1) detached]
It seems odd that I can only see a single thread in info thread; I would expect to see one thread per hart.
My hunch is that I end up attached to a hart which loses the lottery and goes to sleep, and for some none of the other harts are allowed to continue execution. If I use the virt machine, the execution starts as expected when I run continue and I see the OpenSBI splash immediately, so it seems to be linked to the use of the sifive_u in some way.
Does anyone have any idea what I'm doing wrong?
https://www.qemu.org/docs/master/system/gdb.html#Debugging%20multicore%20machines
See "Debugging multicore machines" in this page.
(gdb)target extended-remote :1234
(gdb)add-inferior
(gdb)inferior 2
(gdb)attach 2
(gdb)i threads
See this picture
I want to use eclipse IDE as front-end GUI to debug a core file of another OS that is docker'ed in my host. With 'gdbserver' in the docker i can only debug a running process. But i need post-mortem debugging of core files.
I tried some /home/me/bin/remote_gdb wrapper that uses
docker exec -it $DOCKER_CONTAINER bash -c "gdb /usr/bin/executable /opt/crash/executable.core
This works using in a shell. But it fails using in 'eclipse' because of
Could not determine GDB version using command: remote_gdb --version
even though 'remote_gdb --version' prints out exactly the same as the original 'gdb --version'.
I could make something work with 'ddd' as a front-end - let me exercise:
> cd $HOME/SRC
> ls -l
total 4
-rw-rw-r-- 1 frank frank 96 Aug 24 18:04 test.c
> cat test.c
#include <stdio.h>
int main(int argc, char** argv)
{
printf("hello, world!\n");
return 0;
}
> docker run -v $HOME/SRC:/SRC -t fedora /bin/bash
// IN DOCKER NOW:
>> yum install gcc
[...]
>> yum install gdb
[...]
>> cd /SRC
>> gcc -g3 -O0 -o test test.c
>> ls -l
total 40
-rwxr-xr-x 1 root root 34864 Aug 24 16:26 test
-rw-rw-r-- 1 1000 1000 96 Aug 24 16:04 test.c
Back in Host:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e501d8384060 fedora "/bin/bash" 4 minutes ago Up 4 minutes stupefied_murdock
> cat $HOME/bin/remote_gdb
#!/bin/bash
docker exec -it e501d8384060 gdb /SRC/test
> remote_gdb # $HOME/bin/ is in $PATH
GNU gdb (GDB) Fedora 8.0-20.fc26
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /SRC/test...done.
(gdb) list
1 #include <stdio.h>
2
3 int main(int argc, char** argv)
4 {
5 printf("hello, world!\n");
6 return 0;
7 }
8
(gdb)
Switching back to the shell, where i am in the docker - see 'gdb' is running:
>> yum install procps-ng
[...]
>> ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 16:22 ? 00:00:00 /bin/bash
root 16 0 0 16:23 ? 00:00:00 bash
root 116 0 0 16:28 ? 00:00:00 gdb /SRC/test
root 131 16 0 16:30 ? 00:00:00 ps -ef
Back again on the host shell (after quit gdb session in docker):
> ddd --debugger remote_gdb
The 'ddd' plays nicely with gdb started in docker.
And it also works with core-files (add a "sleep(100);" and kill the process).
But 'ddd' doesn't have a really good GUI.
So what's the way to make such work with 'eclipse'?
I also couldn't make it work with other options: 'qtcreator', 'kdevelop', 'nemiver', 'kdbg', etc
Addendum:
It also doesn't work with 'ssh' into container.
But when i execute the same 'remote_gdb' wrapper script as 'External Tool' it can be executed(?!)
I partially could solve the issue.
The main problem was: my docker command does not provide a tty to eclipse.
As a workaround i switched over to 'ssh' into the docker image.
Moreover i had to align the file locations (executable, core-file and source-files) between host and docker image. Because using a 'gdb' wrapper script, 'eclipse' is not aware that i am debugging remotely and verifies all files for local availability.
This works for post-mortem debugging (core file analysis) and attaching a running process in docker (in general on the remote system).
However, it does not work for starting up a process in gdb via eclipse.
So used 'strace' to check for the activities between 'eclipse' and 'gdb' and found out, that for starting up a new process in 'gdb', 'eclipse' tries to apply some '--interpreter mi2 -nx' command:
inferior-tty-set --thread-group i1 /dev/pts/27
Does it mean it tries to bind a local tty to gdb?
That of course does not work remotely.
What is the reason to use "inferior-tty-set" by 'eclipse'?
It doesn't use this command for doing post-mortem debugging or attaching a running process - while for both i even have a gdb console operable in 'eclipse'
?
After doing some programming assignments for school, I decided to start working on some side projects I had in mind. I decided to install vim on Windows and link it properly using Cygwin. I installed gcc and gdb at first, to try to see if everything worked. I made a quick C file that had a few printf's so I could step into them using gdb.
I can set a breakpoint in main, run the program, get the first printf, but the moment I try to step into the next line, an error appears.
(gdb) step
_sigfe_puts () at sigfe.s:7019
7019 sigfe.s: No such file or directory.
I thought I didn't install gdb properly, so I reinstalled to no luck. I tried using UltraGDB to see if it would help, but the same thing happened, this time giving me a much clearer error.
Can't find a source file at "C:/cygwin64/cygwin/src/cygwin/cygwin-2.8.0/cygwin-2.8.0-1.x86_64/build/x86_64-pc-cygwin/winsup/cygwin/sigfe.s"
Locate the file or edit the source lookup path to include its location.
I had a suspicion it had to do with permissions, so I tried to look up said directory and turns out it really does not exist. C:/cygwin64/ does not have a cygwin folder, much less a subfolder called src and so on. What I did find is that there is a folder called x86_64-pc-cygwin in the cygwin64/usr but it does not have a winsup folder at all.
I'm lost as to what I can do now to fix this error, it's been annoying me for the past few days and have not found any fixes regarding this. I assume GDB is looking for this file in the wrong place, but if I have gdb installed using the Cygwin setup executable then shouldn't this be working anyway? Any help would be appreciated, thank you.
Complete test run w/error:
$ gdb ./test
GNU gdb (GDB) (Cygwin 7.10.1-1) 7.10.1
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test...done.
(gdb) break main
Breakpoint 1 at 0x1004010ed: file test.c, line 6.
(gdb) run
Starting program: /home/User/test
[New Thread 13236.0x4c0]
[New Thread 13236.0x824]
[New Thread 13236.0x3078]
[New Thread 13236.0x374]
Breakpoint 1, main () at test.c:6
6 printf("Hello\n");
(gdb) step
_sigfe_puts () at sigfe.s:7019
7019 sigfe.s: No such file or directory.
(gdb)
7020 in sigfe.s
(gdb)
7023 in sigfe.s
(gdb)
_sigfe () at sigfe.s:19
19 in sigfe.s
(gdb)
20 in sigfe.s
(gdb)
21 in sigfe.s
.
.
.
(stops responding after various steps)
sigfe.s is a source file dynamically created during cygwin build.
It is present in the build tree:
$ find . -iname "*sigfe*"
./x86_64-unknown-cygwin/winsup/cygwin/sigfe.o
./x86_64-unknown-cygwin/winsup/cygwin/sigfe.s
and it seems the only file with such characteristics
$ find . -iname "*.s"
./x86_64-unknown-cygwin/winsup/cygwin/sigfe.s
In theory it should be included in the cygwin-debuginfo package
$ cygcheck -l cygwin-debuginfo | grep -i "s$"
/usr/src/debug/cygwin-2.8.1-0.1/newlib/libc/machine/x86_64/memcpy.S
/usr/src/debug/cygwin-2.8.1-0.1/newlib/libc/machine/x86_64/memset.S
/usr/src/debug/cygwin-2.8.1-0.1/newlib/libc/machine/x86_64/setjmp.S
You can highlight the issue on the cygwin mailing list.
https://cygwin.com/cygwin/lists.html
This is really a general question.
And I've tried looking around for help but it's about the gdp debugger.
I compile a program, it takes two strings as arguments.
I run it with r and the two parameters following it, but when I insert a break like ' break 33 ' and break at line 33 and then run it again, I cannot step into it and it doesn't stop at my break.
I can't step break continue etc.
gcc -g -o debugme debugme.c
Directory: /home/
Tue Oct 6 20:03:14 EDT 2015
[]$ debugme "hi there" "bye bye"
String '0' - 'debugme'
String '1' - 'hi there'
String '2' - 'bye bye'
Total number of command-line arguments: 2
(gdb)
Tue Oct 6 20:06:56 EDT 2015
[]$ cd cosc220
[]$ gdb debugme
GNU gdb (GDB; openSUSE 13.1) 7.6.50.20130731-cvs
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-suse-linux".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://bugs.opensuse.org/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
..
Reading symbols from /home/...done.
(gdb) break 33
Breakpoint 1 at 0x4005c7: file debugme.c, line 33.
(gdb) delete 1
(gdb)
(gdb) break 33
Breakpoint 2 at 0x4005c7: file debugme.c, line 33.
(gdb)
What do I need to type for this to work?
It tells me my program isn't running, I try viewing my call stack and it's empty
I type r for run later (I just didn't include it here)
You need to hit r at the gdb so that your program again start running.
What do I need to type for this to work?
You should probably read an introduction, such as this one.
The command you are looking for is run, but once you run your program to the breakpoint, it's likely that you'll have more questions, so starting at introduction may still be a good idea.
It turns out that we didn't have it installed on the server at my school...
Face-palm, it's working now.
I am new to cgi and with an example I wrote a small program in c++ which I have compiled to a .cgi file.
My question is: do I need an separate web server? I have lighttpd as my default web server ... If i can run thought lighttpd please explain how can I do it...
Make sure your .cgi file is executable, and put it under your web root.
Turn on cgi http://redmine.lighttpd.net/wiki/1/Docs:ModCGI
go to the page. :)
Strictly speaking you don't need a server. If you just want to see your CGI running, you can use my tiny runCGI project.
All you need is to set a yaml file which looks something like this
_exec: /var/cgi-bin/myfile.cgi
method: GET
query_string:
q: s
and then run
./runCGI myyamlfile.yaml
You will see the output on the console's standard output.
You can even debug it with gdb, debug runCGI gdb runCGI, run with the correct parameters (run someyaml.yaml), issue tcatch exec (tcatch catches it only once) and then set breakpoints to your CGI file:
$ g++ a.cc -o a.out
$ cat a.yaml
method: GET
_exec: a.out
$ gdb runCGI
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu"...
(gdb) tcatch exec
Catchpoint 1 (exec)
(gdb) run a.yaml
Starting program: /home/elazar/runCGI/runCGI a.yaml
Executing new program: /home/elazar/runCGI/a.out
0x00007fc3a24a6a60 in ?? () from /lib64/ld-linux-x86-64.so.2
(gdb) tbreak main
Breakpoint 2 at 0x400577: file a.cc, line 2.
(gdb) c
Continuing.
main (argc=1, argv=0x7fff14891408) at a.cc:2
2 int a =0;
(gdb)