how to run a cgi program - c++

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)

Related

Remote debugging HiFive Unleashed in QEMU

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

schedBreak(<tick>) gdb debugging function not working

I am trying to create breakpoints and debug gem5 using gdb. I referred to http://www.gem5.org/Debugger_Based_Debugging.
As in the official documentation in the above link, I tried `call schedBreak() but it doesn't work. the following are the full commands:
➜ test-gem5-x86 git:(master) ✗ gdb --args ./build/X86/gem5.opt configs/learning_gem5/part1/simple.py
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 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".
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 ./build/X86/gem5.opt...done.
(gdb) b main
Breakpoint 1 at 0x4b0d20: main. (4 locations)
(gdb) run
Starting program: /home/hari/test-gem5-x86/build/X86/gem5.opt configs/learning_gem5/part1/simple.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 1, main (argc=2, argv=0x7fffffffdef8) at build/X86/sim/main.cc:42
42 {
(gdb) call schedBreak(10000)
warn: need to stop all queues
(gdb) c
Continuing.
gem5 Simulator System. http://gem5.org
gem5 is copyrighted software; use the --copyright option for details.
gem5 compiled Aug 26 2019 20:59:02
gem5 started Sep 8 2019 15:17:55
gem5 executing on nirmal-cadsl2, pid 30158
command line: /home/hari/test-gem5-x86/build/X86/gem5.opt configs/learning_gem5/part1/simple.py
Global frequency set at 1000000000000 ticks per second
warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
0: system.remote_gdb: listening for remote gdb on port 7001
Beginning simulation!
info: Entering event queue # 0. Starting simulation...
Hello world!
Exiting # tick 501393000 because exiting with last active thread context
[Inferior 1 (process 30158) exited normally]
(gdb)
While this other tutorial http://gem5.org/wiki/images/0/0e/ASPLOS2017_gem5_tutorial.pdf (assuming the debug function is not particular to ISA) tells me that the function is actually schedBreakCycle(), it gives me this No symbol "schedBreakCycle" in current context. The full commands shown below.
➜ test-gem5-x86 git:(master) ✗ gdb --args ./build/X86/gem5.opt configs/learning_gem5/part1/simple.py
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 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".
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 ./build/X86/gem5.opt...done.
(gdb) b main
Breakpoint 1 at 0x4b0d20: main. (4 locations)
(gdb) run
Starting program: /home/hari/test-gem5-x86/build/X86/gem5.opt configs/learning_gem5/part1/simple.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 1, main (argc=2, argv=0x7fffffffdef8) at build/X86/sim/main.cc:42
42 {
(gdb) call schedBreakCycle(10000)
No symbol "schedBreakCycle" in current context.
(gdb)
gem5 version: ea8c435b6c6c092d72047eee50f125f5ae7347c3
gdb version: GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Also, if I want to debug ALPHA or ARM full system on my ubuntu 18.04(x86), do I have to use any cross-compiled gdb versions or would native gdb suffice?
The tutorial is correct, you have to pass --debug-break to gem5:
gdb --args gem5.debug --debug-break=1000 ...
and then without breaking at main:
run
call schedBreak(5000)
continue
--debug-break generates a break at time 1000 and makes GDB stop there after main, and from that point, schedBreak does work.
Or alternatively first go to:
tbreak doSimLoop
run
call schedBreak(5000)
continue
schedBreak schedules an event in the event queue, which cannot be ready at main.
The breaks work by raising a signal, which GDB stops at by default.
ALPHA and ARM won't make a difference since schedBreak is a tiny helper to debug the host emulator itself, which is likely an x86 program. To debug the guest code, which is what most people want, see e.g. this tutorial.
Tested gem5 master at e87a293d1ffa6da38ba8fa145e7dc5128138ab77 in an X86 debug build.

GDB in Cygwin is missing sigfe.s?

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

D2 and gdb issue

Short question: Is there any support of D2 in gdb (I have gdb 7.2) ?
Long story: I compiled next little application ...
// file main.d
int glVar = 0xAAAAAAAA;
void main()
{
glVar = 0xBBBBBBBB;
}
... using command "dmd -gc -debug main.d";
then I load it to gdb and trying to debug it:
vnm#vnm:~/proj/d_gdb_test$ gdb main
GNU gdb (GDB) 7.2-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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 "i686-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/vnm/proj/d_gbb_test/main...done.
(gdb) b main
Breakpoint 1 at 0x804b667
(gdb) r
Starting program: /home/vnm/proj/d_gbb_test/main
[Thread debugging using libthread_db enabled]
Breakpoint 1, 0x0804b667 in main ()
(gdb) info line
No line number information available.
(gdb) info variables glVar
All variables matching regular expression "glVar":
File main.d:
int _D4main5glVari;
Why gdb can't show line information and why it shows symbols in mangled form ? Is this software issues or I'm doing something wrong ?
gdb has support for D starting with version 7.2. So, you can debug D programs with gdb 7.2.
Now, that doesn't mean that support is perfect - far from it in fact. For instance, I'm not sure that you can get it to print strings properly. And it's not at all surprising if it doesn't demangle D symbols. So, it works, but it's far from perfect.

Debugging MinGW program with gdb on Windows, not terminating at assert failure

How do I set up gdb on window so that it does not allow a program with assertion failure to terminate? I intend to check the stack trace and variables in the program.
For example, running this test.cpp program compiled with MinGW 'g++ -g test.cpp -o test' in gdb:
#include <cassert>
int main(int argc, char ** argv) { assert(1==2); return 0; }
Gives:
$ gdb test.exe
GNU gdb 6.8
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 "i686-pc-mingw32"...
(gdb) r
Starting program: f:\code/test.exe
[New thread 4616.0x1200]
Error: dll starting at 0x77030000 not found.
Error: dll starting at 0x75f80000 not found.
Error: dll starting at 0x77030000 not found.
Error: dll starting at 0x76f30000 not found.
Assertion failed: 1==2, file test.cpp, line 2
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Program exited with code 03.
(gdb)
I would like to be able to stop the program from terminating immediately, like how Visual Studio's debugger and gdb on Linux does it. I have done a search and found some stuff on trapping signals but I can't seem to find a good post on how to set up gdb to do this.
Found out that the breakpoint can be put in the .gdbinit file with the lines:
set breakpoint pending on
b exit
This removes the need to enter yes for windows.
Just set a breakpoint on exit:
(gdb) b exit
Using recent (March 2017) msys2 with gcc 6.3 and gdb 7.12.1 you should use:
break _exit
i.e. use _exit and not exit. I expect this also to work in other cases as I expect that exit will call _exit to actually exit.