4
votes

I change post totally. because I work in south korea army. but south korea army internet computer is forbid upload file. so I really upload my source code. but I can't . so I try debug very very simple program with gdb. but It is still not working.

my system is

Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:        14.04
Codename:       trusty

in cloud IDE called "nitrous"

and g++ , gdb version is

g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
GNU gdb (GDB) 7.8

I write very simple code : simple.cpp

#include <iostream>

int main(){
   std::cout << "Hello World!" << std::endl;
   return 0;
}

compile with "-g" and gdb execute

nitrous@ubuntu-108903:~/code$ g++ -g simple.cpp -o simple
nitrous@ubuntu-108903:~/code$ gdb simple

and set break main and run

Reading symbols from simple...done.
(gdb) break main
Breakpoint 1 at 0x400861: file simple.cpp, line 4.
(gdb) run
Starting program: /home/nitrous/code/simple
Hello World!
During startup program exited normally.

Even very simple helloworld program not work breakpoint. just print out During startup program exited norally.

2
You forgot to show a minimal example (SSCCE)sehe
The question is quite unclear. Maybe this helps, though: The usual way to set a breakpoint at that line would be b main.cpp:21sehe
@sehe add some example. sorry for poor example.. this posting is my first ask.임근영
That's not the correct source, because it's line 7, not 21. Also, you cannot set a breakpoint on a comment line (it will set it at the next line with executable statements I think). Try disabling optimizations (-O0) and make sure you compile and link with -g (make sure you do a full rebuild)sehe
@sehe I explicitly comment "line no.21", because that source code is comment removed version(for Readability. actually, raw source code has many comment. so I remove some comment). and I try also "-g" and "-O0" and link with "-g"임근영

2 Answers

0
votes

I set a break point, but it's not hit. What is problem?

The most likely problem is that your program terminates before reaching main. (A typical dynamically linked program will execute several hundred 1000s of instructions before reaching main.)

Run your program under GDB until GDB stops with SIGSEGV. Execute GDB where command. Observe that main is not on the stack.

Once you've confirmed that main is not on the stack, ask a different question (assuming you still don't understand the reason for crash).

0
votes

I guess that gdb for some reason failed to set breakpoint. Try to run gdb with sudo. Btw, could you run strace on generated elf and grep for ptrace? It should be something like follow strace -f -o syscall.txt gdb ./simple.out.