gdb reports something I dont understand

This is a discussion on gdb reports something I dont understand within the Linux Programming forums, part of the Platform Specific Boards category; could you please explain what has happened here? > Program received signal SIGSEGV, Segmentation fault. > 0x0804885d in add_token () ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    15

    gdb reports something I dont understand

    could you please explain what has happened here?

    > Program received signal SIGSEGV, Segmentation fault.
    > 0x0804885d in add_token ()
    > Current language: auto; currently asm

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Your program accessed memory, while in the add_token function, it did not have the rights to access.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Type bt to get a backtrace of where the segmentation fault happened (i.e., which functions called add_token() before it segfaulted).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    The next step is add a breakpoint at the start of add_token(), then single step your way through the code until you either
    - segfault again
    - realise at some point before it crashes "wait a minute, that can't be good".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    This is what bt gives me...


    > Program received signal SIGSEGV, Segmentation fault.
    > 0x080487be in add_token ()
    > Current language: auto; currently asm
    > (gdb) bt
    > #0 0x080487be in add_token ()
    > #1 0x08048dad in analyze ()
    > #2 0x08048696 in main ()

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Following Salem's advice, I added a breakpoint and stepied....

    This is what happens


    Breakpoint 1, 0x080487a8 in add_token ()
    (gdb) stepi
    0x080487ab in add_token ()
    (gdb) stepi
    0x080487b0 in add_token ()
    (gdb) stepi
    0x080487b3 in add_token ()
    (gdb) stepi
    0x080487b9 in add_token ()
    (gdb) stepi
    0x08048604 in new_token ()
    (gdb) stepi
    0x08048605 in new_token ()
    (gdb) stepi
    0x08048607 in new_token ()
    (gdb) stepi
    0x0804860a in new_token ()
    (gdb) stepi
    0x08048611 in new_token ()
    (gdb) stepi
    0x08048544 in malloc@plt ()
    (gdb) stepi
    0x0804854a in malloc@plt ()
    (gdb) stepi
    0x0804854f in malloc@plt ()
    (gdb) stepi
    0x08048474 in ?? ()
    (gdb) stepi
    0x0804847a in ?? ()
    (gdb) stepi
    0xb7fd6c40 in ?? () from /lib/ld-linux.so.2
    (gdb) stepi
    0xb7fd6c41 in ?? () from /lib/ld-linux.so.2
    (gdb) stepi
    0xb7fd6c42 in ?? () from /lib/ld-linux.so.2
    (gdb) stepi
    0xb7fd6c43 in ?? () from /lib/ld-linux.so.2
    (gdb) stepi
    0xb7fd6c47 in ?? () from /lib/ld-linux.so.2
    (gdb) stepi
    0xb7fd6c4b in ?? () from /lib/ld-linux.so.2
    (gdb) stepi
    0xb7fd1350 in ?? () from /lib/ld-linux.so.2
    (gdb) stepi
    0xb7fd1351 in ?? () from /lib/ld-linux.so.2
    (gdb) stepi
    0xb7fd1353 in ?? () from /lib/ld-linux.so.2
    (gdb) stepi
    0xb7fd1354 in ?? () from /lib/ld-linux.so.2
    This goes on for some time but I think "that '??' cant be good"
    Am I right?

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    At the point the ??s start showing up you're in kernel code, so that's not really important.

    My suggestion, adding to Salem's is to run the program itself within gdb. When it segfaults, check the values of the variables in add_token; it should provide a hint as to what's happening.

    Perhaps if you post the code for add_token we could be of more assistance in getting to the root of the issue.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    The fact that it calls malloc probably suggests that some earlier mis-use of malloc occurred. Here is just where you get to notice there's a problem.

    Problems such as
    - array overrun of allocated memory
    - use after free
    - double free
    - freeing what wasn't allocated

    If you use say valgrind or electric fence, then these usually pinpoint the code which is the cause, not the effect (which is what you see).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Here's a part of the add_token() function. (The part that I've truncated is a few if-else statements that recognize tokens and add them to a linked list)

    Code:
    void add_token() {
    
    	struct lex *ptr;
    	ptr=token;
    	token=token->next_lex=new_token();
    	if(ptr!=NULL) {
    		token->prev_lex=ptr;
    	}
    	else {
    		begin=token;
    	}
    	token->lines=lines;
    ...
    ...
    Here's the new_token() function where I have used malloc function.

    Code:
    struct lex* new_token() {
    	struct lex *a=malloc(sizeof(struct lex));
    	return (a);
    }

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    very strange code

    if ptr is NULL then the previous token was NULL
    so token->next_lex will crash because it is done before the check
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Good catch. The correct form would be:
    Code:
    void add_token() {
    
    	struct lex *ptr;
    	ptr=token;
    	token=new_token();
    	if(ptr!=NULL) {
    		token->prev_lex=ptr;
    		ptr->next_lex=ptr;
    	}
    	else {
    		begin=token;
    	}
    	token->lines=lines;
    ...
    ...
    However, there's also the problem that the memory of the new token is uninitialized. The simplest way to solve this is to use calloc instead of malloc.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using gdb debugger.....
    By manny in forum Tech Board
    Replies: 7
    Last Post: 09-18-2006, 02:26 AM
  2. Example from book, don't understand.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2005, 04:47 PM
  3. using gdb
    By crypto_quixote in forum C Programming
    Replies: 2
    Last Post: 09-15-2005, 11:02 AM
  4. My library is failing and I cannot figure out why...
    By DerelictDream in forum C++ Programming
    Replies: 7
    Last Post: 08-15-2005, 04:47 PM
  5. Some errors and warnings i dont understand
    By lakai02 in forum C Programming
    Replies: 6
    Last Post: 10-18-2002, 12:16 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21