can print from command line, but cannot get it from the program - gdb

I use gdb to debug my program, when I unpack a message and wanna print it, I got a problem. It seems that i can print from command line in the terminal, but when program goes to the printf("%d has received msg: ", msg->connid);, I got the problem,
Program received signal SIGSEGV, Segmentation fault.
0xb7ff6301 in ?? () from /lib/ld-linux.so.2
(gdb)n
154 LSPMessage* msg = lspmessage__unpack(NULL, msg_len, buf);
(gdb) n
156 memcpy(pld, msg->payload.data, msg->payload.len);
(gdb) p msg->payload.data
$1 = (uint8_t *) 0x804c038 "Connectedrt,\031"
(gdb) p msg->connid
$2 = 1
(gdb) p msg->payload.len
$3 = 9
174 printf("%d has received msg: ", msg->connid); // required field
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0xb7ff6301 in ?? () from /lib/ld-linux.so.2

You did a remarkably poor job of explaining what it is you are actually asking.
I think your question is: "how come my printf call crashes?".
There could be several reasons
You have corrupted stdout earlier, or
msg->connid is not int (and so it is wrong to printf it with "%d", or
you have corrupted something else in the runtime loader state, and it now crashes while doing lazy PLT symbol resolution
Since you crash is inside the runtime loader, that last cause appears to be most likely. You can confirm this hypothesis by forcing the loader to perform symol resolution non-lazily:
(gdb) set env LD_BIND_NOW 1
(gdb) run
Did it still crash? If not, run your program under Valgrind, and be sure to fix all the problems it reports.

Related

Why does GDB show a segfault when using atoi(), not when running the code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I get a segfault related to the following line:
int testenum = atoi(argv[1]);
The following is the whole error:
/build/bin/wrapper' has changed; re-reading symbols.
Temporary breakpoint 5 at 0x13f5: file /examples/wrapper.cpp, line 166.
Starting program: /build/bin/wrapper
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
Temporary breakpoint 5, main (argc=1, argv=0x7fffffffe288) at /examples/wrapper.cpp:166
166 testenum = atoi(argv[1]);
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6fffa3b in __GI_____strtol_l_internal (nptr=0x0, endptr=0x0, base=10, group=<optimized out>, loc=0x7ffff71934a0 <_nl_global_locale>) at ../stdlib/strtol_l.c:291
291 while (ISSPACE (*s))
(gdb) backtrace
#0 0x00007ffff6fffa3b in __GI_____strtol_l_internal (nptr=0x0,
endptr=0x0, base=10, group=<optimized out>,
loc=0x7ffff71934a0 <_nl_global_locale>) at ../stdlib/strtol_l.c:291
#1 0x0000555555555405 in atoi (
__nptr=0xa <error: Cannot access memory at address 0xa>)
at /usr/include/stdlib.h:364
#2 main (argc=<optimized out>, argv=0x7fffffffe288)
at /examples/wrapper.cpp:166
(gdb)
Why does this seemingly simple line trigger this error in GDB, when it runs seemingly fine when ran traditionally?
The message tells you exactly what the error is:
0x00007ffff6fffa3b in __GI_____strtol_l_internal (nptr=0x0, endptr=0x0, base=10, group=<optimized out>, loc=0x7ffff71934a0 <_nl_global_locale>) at ../stdlib/strtol_l.c:291
Translation: The error happened while converting a string to long, the input string pointer (nptr) is NULL.
Since this line of code attempts to convert a string to long
int testenum = atoi(argv[1]);
This means argv[1] is NULL. You failed to check before passing command line arguments to atoi() that the user actually passed arguments to the program when launching it from the command line.
Use main function parameter argc to check if the user passed arguments to the program. To use argv[1], then you should have argc > 1, otherwise your program will crash and will print the error message above.

GDB backtrace not showing correct informations

I'm learning how to debug with gdb on my mac and after finding a segmentation fault I wanted to use it to learn.
I'm using gdb 8.0.1 and gcc 7.2.0 both from homebrew, I'm compiling with -ggdb and running gdb directly from my makefile through gdb -ex run ./main.
I open the game, I open a menu inside it, and when I try close it it crashes because I do this in WindowsObject.cpp :
WindowObject_CraftingGrid::~WindowObject_CraftingGrid(){
for (unsigned i = 0; i < gridSlots_.size(); i++) {
for (unsigned j = 0; j < gridSlots_[0].size(); i++) { //i++ instead of j++, this leads to the crash
delete gridSlots_[i][j];
}
}
}
Gdb says:
(gdb) bt
#0 0x0000000100023a80 in WindowObject_Image::Draw (this=0x300000000) at src/WindowObjects.cpp:620
#1 0x0000000100023ae2 in WindowObject_Image::setImage (this=0x100a9e980, img=0x0) at src/WindowObjects.cpp:629
#2 0x000000010001d5f7 in WindowMain::AddSection (this=0x100a04ce0, n=28672) at src/Window.cpp:263
#3 0x0000000100033765 in LoadLibrary () at src/main.cpp:781
#4 0x0000000100030b25 in DrawGUI () at src/main.cpp:465
#5 0x0000000100031534 in DrawGUI () at src/main.cpp:501
#6 0x00000001006eae27 in ?? ()
#7 0x0000700001875ef0 in ?? ()
#8 0x00007fff40b796d8 in ?? ()
#9 0x0000000000000000 in ?? ()
And this is totally wrong because it leads to nothing useful to solve the bug because it does not point to the right objects and lines.
I discovered this bug from visual studio on my windows machine because the call stack there was quite clear:
project.exe!std::vector<std::vector>WindowObjects_Slot * //Other stuff
project.exe!WindowObject_CraftingGrid::~WindowObject_CraftingGrid() Line 348
project.exe!WindowMain::~WindowMain() Line 234
project.exe!KeyPressed(int KeyCode) Line 566
project.exe!gameloop() Line 181
project.exe!main(int argc, char ** argv) Line 321)
And this is totally wrong
No, it's not: it's where your application actually crashes on this platform.
because it leads to nothing useful to solve the bug
You have a heap corruption bug. Heap corruption bugs are like that: your application may crash some time after heap corruption, in an arbitrary place.
In addition, the stack trace is not useless: it tells you that this == 0x300000000, which is not a reasonable value for this, and therefore you are looking at some kind of heap corruption.
There are many ways to debug similar problems: debug malloc, Address Sanitizer and Valgrind among them.
Building with -D_GLIBCXX_DEBUG enables debugging mode in GCC STL, and would likely also point you straight at the bug.

STL type/function used in gdb conditional break, will crash the program?

I wish to test in my program below: when s="abc", break inside "f()" and see the value if "i".
#include<string>
using namespace std;
int i=0;
void f(const string& s1)
{
++i; // line 6
}
int main()
{
string s="a";
s+="b";
s+="c";
s+="d";
s+="e";
s+="f";
return 0;
}
Compile and run a.out, no problem. I then debug it
g++ 1.cpp -g
gdb a.out
...
(gdb) b main if strcmp(s.c_str(),"abc")==0
Breakpoint 1 at 0x400979: file 1.cpp, line 9.
(gdb) r
Starting program: /home/dev/a.out
Program received signal SIGSEGV, Segmentation fault.
__strcmp_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S:31
31 ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S: No such file or directory.
Error in testing breakpoint condition:
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(__strcmp_sse2_unaligned) will be abandoned.
When the function is done executing, GDB will silently stop.
Program received signal SIGSEGV, Segmentation fault.
Breakpoint 1, __strcmp_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S:31
31 in ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S
If I change the break point declaration into:
(gdb) b main:6 if s.compare("abc")==0
Breakpoint 1 at 0x400979: file 1.cpp, line 9.
Then I get another kind of crash, seems:
(gdb) r
Starting program: /home/dev/a.out
Program received signal SIGSEGV, Segmentation fault.
__memcmp_sse4_1 () at ../sysdeps/x86_64/multiarch/memcmp-sse4.S:1024
1024 ../sysdeps/x86_64/multiarch/memcmp-sse4.S: No such file or directory.
Error in testing breakpoint condition:
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(char const*) const) will be abandoned.
When the function is done executing, GDB will silently stop.
Program received signal SIGSEGV, Segmentation fault.
Breakpoint 1, __memcmp_sse4_1 () at ../sysdeps/x86_64/multiarch/memcmp-sse4.S:1024
1024 in ../sysdeps/x86_64/multiarch/memcmp-sse4.S
Is this crash caused by gdb, or my command? If my command has runtime problem, why gdb doesn't simply report an error, by rather crash the program?
Hope to get some explanations, as I didn't get this error cause.
What is going on here is that your command:
(gdb) break main:6
... is interpreted by gdb as the same as break main. You can see this by typing the latter as well:
(gdb) b main:6
Breakpoint 1 at 0x400919: file q.cc, line 10.
(gdb) b main
Note: breakpoint 1 also set at pc 0x400919.
Breakpoint 2 at 0x400919: file q.cc, line 10.
Now, this is peculiar because gdb presumably ought to warn you that the trailing :6 is ignored. (I'd recommend filing a bug asking that this be made a syntax error.)
If you want to break at a certain line in a file you must use the source file name. Presumably you meant to type:
(gdb) break main.cc:6

Issue while using GDB to find the reason of a segmentationf fault

I've been trying to use GDB to debug a palindrome recognizing program. I entered into gdb and ran it by doing a command:
run aca
It then printed out
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 1 (LWP 1)]
0x00010e38 in findEnd ()
So I'm assuming there's something wrong in my findEnd().
I then did:
bt
and it printed out
0x00010e38 in findEnd ()
Is there any way I can figure out what line exactly in findEnd is causing this seg fault error? I'm assuming that the 38 at the end of the line doesn't indicate line number, does it?
Please let me know if I need to add more details!

gdb doesn't print debugging info

when i run the core file in gdb, gdb doesn't show where the error is coming from or what line
in the application that causes the problem.
i'm using the compiler options -g -DDEBUG -D_DEBUG, but it doesnt seem help.
Any help would be appreciated, thanks.
You could be blowing your stack. For example, after running the following program
#include <stdio.h>
#include <string.h>
int main(void)
{
int a[10];
memset(a, 0, 100 * sizeof a[0]);
return 0;
}
and then running gdb on the resulting core yields
$ gdb oflow core
[...]
Core was generated by `./oflow'.
Program terminated with signal 11, Segmentation fault.
#0 0x0000000000000000 in ?? ()
The output of the where and bt commands isn't terribly useful:
(gdb) where
#0 0x0000000000000000 in ?? ()
#1 0x0000000000000000 in ?? ()
(gdb) bt
#0 0x0000000000000000 in ?? ()
#1 0x0000000000000000 in ?? ()
ok, problem solved. i had a recursive function that returned a string, but the problem was there was nothing being returned, but i still don't understand why debugging info wasn't generated, when i step through the code it shows the line numbers i'm stepping through, but i guess because the line it was getting an error was missing? so there was no breakpoint for where it went wrong? when it tried to concatenate itself by recursing into the function, using "+=" it would go into the second call but then crash at the end of the function because nothing was being returned. but shouldn't that have generated an error on the first function call on the line where it didn't return?
thanks.