LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-27-2007, 02:46 PM   #1
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Dumb question about gdb


Hello. I can put it off no more, I have to learn to use gdb. So, I have a program which crashes with segmentation fault. I see with gdb that it's in some mysterious strlen ().

PHP Code:
0x40089e33 in strlen () from /lib/libc.so.6 
Ok, so far so good. How do I find how this relates to my sourcecode? I mean, how do I find the line that causes this to happen? Thank you
 
Old 05-27-2007, 02:55 PM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
bt
It will give you call stack, or BackTrace. So you'll see every call that finally led you to the problem, if stack is not corrupted (which is not guaranteed) you'll see even pre-main() initialization code.
 
Old 05-27-2007, 03:32 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
If your just starting out with using gdb I also recommend you download and print out a copy of the GDB Quick Reference PDF.
 
Old 05-27-2007, 04:26 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. GDB is a very powerful tool.
Here's a tutorial that might be a little less intimidating to start off with:
http://oucsace.cs.ohiou.edu/~bhumphre/gdb.html

2. "strlen" is the standard C library function to get "string length".

It's almost certainly *not* the problem.

You're probably calling "strlen" with a bad string.

Or calling some function that calls a function that calls "strlen" with a bad string.

In any case, "bt" (or, equivalently, "where") are your friends.

'Hope that helps .. PSM
 
Old 05-28-2007, 08:42 AM   #5
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Thumbs up

Quote:
Originally Posted by paulsm4
"strlen" is the standard C library function to get "string length".
Thanks dude =)) =)) I only called it mysterious because sometime in the past I had a program with many strlen () calls and one was causing a segfault. It was just an example, and yes, I am quite sure the problem is probably a null pointer or smth like that. Yesterday I had a problem with memcpy () (PLEASE don't explain me what that does ) and there were many memcpy calls in my program. Anyway thank you very much for your help, I really appreciate it. Cheers.

Last edited by nc3b; 05-28-2007 at 08:44 AM.
 
Old 05-28-2007, 04:16 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, again -

Fair enough - I get that you're familiar with "strlen ()".

For whatever it's worth, the two points I had tried to make were:

1) GDB can be formidable (heck, the GDB "cheat sheet" jtshaw referred you to might appear formidable!), but it can also be really easy. In general, you'll only need four or five GDB commands most of the time (including "where" and "quit").

So if you're not already familiar with GDB, please definitely avail yourself of a brief, simple tutorial like this one:
http://oucsace.cs.ohiou.edu/~bhumphre/gdb.html

2) The second point I wanted to make is that, even though "strlen()" might appear to be the culprit, it's very probably two or three (or more!) steps removed from the root cause.

So don't get hung up on "strlen()" itself. You mentioned NULL pointers - that's certainly possible. A string that *doesn't* have a NULL terminator is also a candidate. And a string that *used* to have a NULL terminator ... until somebody else accidentally *overwrote* it ... is even likelier.

Happy hunting!

Your .. PSM

PS:
You might also wish to consider tools like Valgrind or Electric Fence...

Last edited by paulsm4; 05-28-2007 at 04:20 PM.
 
Old 05-29-2007, 08:36 AM   #7
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
I am really beginning to understand that gdb is really something. I really didn't know strlen() crashes if there is no NULL terminator. This could really come in handy in the future. Again, thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 c++ question markhod Programming 3 04-25-2007 02:45 PM
normal gdb and spec gdb for kgdb Igor007 Programming 1 09-23-2005 05:15 PM
normal gdb and spec gdb for kgdb Igor007 Linux - Newbie 1 09-23-2005 02:41 PM
gdb .. looking for 32 bit gdb.. for ia64 suse.. nkshirsagar SUSE / openSUSE 0 12-09-2004 04:02 AM
a question about gdb captainstorm Programming 5 10-07-2003 12:13 PM

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

All times are GMT -5. The time now is 11:06 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration