LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2012, 11:55 AM   #1
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Rep: Reputation: 220Reputation: 220Reputation: 220
gdb doesn't show segfault line, Wikipedia example does


I started to learn gdb today, and I came across a problem right away. Here https://en.wikipedia.org/wiki/Gdb#An_example_session gdb is shown to print the line, where the segfault happens, specifically "0x0000000000400527 in foo_len (s=0x0) at example.c:8". But when I do the same on my own computer, output is a bit different:
Code:
Starting program: /home/avg/c/debug 

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7833ac1 in __strlen_sse2 () from /lib64/libc.so.6
Only when I type 'where', I see:

Code:
#0  0x00007ffff7833ac1 in __strlen_sse2 () from /lib64/libc.so.6
#1  0x00000000004005ab in foo_len (argc=<value optimized out>, argv=<value optimized out>)
    at debug.c:8
#2  main (argc=<value optimized out>, argv=<value optimized out>) at debug.c:16
I run Slackware 13.37 x64, GNU gdb (GDB) 7.2
 
Old 05-29-2012, 04:45 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
probably you have forgotten to build in debug mode (that is the -g flag), so you have no debug info.
 
1 members found this post helpful.
Old 05-29-2012, 09:07 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Plus, you may want to reduce optimization-level. Maybe even to 0.
 
1 members found this post helpful.
Old 05-29-2012, 11:04 AM   #4
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Original Poster
Rep: Reputation: 220Reputation: 220Reputation: 220
I compiled with this command, I dropped '-O2':

Code:
gcc debug.c -g -o debug
And this is my entire output:

Code:
~/c $ gdb ./debug 
GNU gdb (GDB) 7.2
Copyright (C) 2010 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-slackware-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/avg/c/debug...done.
(gdb) run
Starting program: /home/avg/c/debug 

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ab8ac1 in __strlen_sse2 () from /lib64/libc.so.6
Could you check please what output do you get? Maybe it has something to do with x64.
 
Old 05-29-2012, 11:41 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Okay, so you passed an invalid string to strlen. Now try 'bt' then 'bt full'
 
Old 05-29-2012, 11:59 AM   #6
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Original Poster
Rep: Reputation: 220Reputation: 220Reputation: 220
Thanks, but this is not exactly what I am asking about. I just want to know why here https://en.wikipedia.org/wiki/Gdb#An_example_session it showed:

Code:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400527 in foo_len (s=0x0) at example.c:8
8         return strlen (s);
while my gdb shows this:
Code:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ab8ac1 in __strlen_sse2 () from /lib64/libc.so.6
The last line is different. It would be nice if the particular line where segfault occurred was shown, as in Wikipedia example, instead of '0x00007ffff7ab8ac1 in __strlen_sse2 () from /lib64/libc.so.6'
 
Old 05-29-2012, 12:34 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
So the segfault occurs in libc.so.6, your result is perfect, there is no problem with it. The confusion is coming from the fact that it is not your code, but an external library. In the example (what you linked) the same error occured, but the source code is displayed instead of __strlen_sse2() from libc. In general when you debug you need to know what was the last "known" line of code, that is the line: return strlen(s). At this moment I cannot tell you how they produced that result (what was the trick to skip libc), but you can use the command "where" to see similar result.
 
Old 05-29-2012, 03:42 PM   #8
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Original Poster
Rep: Reputation: 220Reputation: 220Reputation: 220
I see. I am very curious how they did it at Wikipedia. I hope somebody will put on the right track here. If not, I will may ask at gdb discusion group or even directly at Wikipedia, because I cannot get along with this.
 
Old 05-29-2012, 05:59 PM   #9
Hidden Windshield
Member
 
Registered: Jul 2010
Distribution: Fedora
Posts: 68

Rep: Reputation: 27
I just tried my copy of gdb, and it produced the exact same output as the Wikipedia article.

Maybe gdb (or libc) was compiled differently on my computer than it was on yours.

Did you try the "where" command after you turned on the debugging symbols?
 
Old 05-30-2012, 02:16 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I've just fixed your problem by editing Wikipedia. Click: http://en.wikipedia.org/wiki/GNU_Deb...xample_session
 
Old 05-30-2012, 11:47 AM   #11
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Original Poster
Rep: Reputation: 220Reputation: 220Reputation: 220
Yes but you didn't exactly explain what I need to do to have the same result as at Wikipedia. I will try to install glibc-debug, but it says "To use these libraries, set LD_LIBRARY_PATH when calling the debugger: LD_LIBRARY_PATH=/usr/lib/debug gdb <executable>" in the package description. I didn't see it done at Wikipedia.
 
Old 05-30-2012, 02:57 PM   #12
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
You're getting into a *deep* rabbit-hole if you're wanting to bring in the debug info for glibc. That's what people who work on glibc itself do, not us mere mortals. Just accept that wikipedia isn't always 100% correct about every single detail and go work on getting learning stuff that's.. a bit more practical.
 
Old 05-30-2012, 03:51 PM   #13
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Original Poster
Rep: Reputation: 220Reputation: 220Reputation: 220
But even in this thread some users reported that they got the same output as at Wikipedia. I'd like to know what's the reason and I'm lost.
 
Old 05-31-2012, 01:22 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Function strlen might be 'inlined' on some platforms... (Wikipedia does mention this possibility)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gdb can't access memory of a segfault? Los Frijoles Programming 2 02-14-2012 10:26 PM
[SOLVED] GDB doesn't provide the correct line in the source code napx Programming 3 01-23-2011 07:16 PM
gdb doesn't show correct string data prima8 Linux - Newbie 0 03-11-2009 04:21 PM
C++: use gdb to find where segfault happens Ephracis Programming 10 03-09-2009 07:42 PM
ps doesn't show the full command line OlRoy *BSD 2 01-29-2007 09:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration