Find the answer to your Linux question:
Results 1 to 10 of 10
I just wrote a simple Hello world program in C (my Linux distro is Fedora 5) and it ran well when built: gcc -g hello.c -o hello . But when ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    6

    Error while debugging in Fedora

    I just wrote a simple Hello world program in C (my Linux distro is Fedora 5) and it ran well when built: gcc -g hello.c -o hello .
    But when I try to debug it as follow
    Code:
    debug hello
    set args a b c
    b 4 
    show args // a b c
    r
    p argc // Error !!!???
    Is that an error of Fedora 5 's gdb ?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm having a difficult time making sense of some of your gdb commands.
    1. If your hello world program is small, please post the complete program source.
    2. Please post the command you used to compile the program.
    3. Please post the exact error message you got from gdb.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    6
    Sorry,here is the source
    Code:
    #include<stdio.h>
    int main()
    {
           printf("Hello world");
           return 0;
    }
    That's all.
    And the error message is
    Cannot access memory at address 0x0

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm using Slackware 9.1, not Fedora, but when I run this script:
    Code:
    #!/bin/bash
    
    cat > hello.c <<EOD
    #include<stdio.h>
    int main()
    {
           printf("Hello world");
           return 0;
    }
    EOD
    gcc -Wall -g hello.c -o hello
    cat > gdb_commands <<EOD
    set args a b c
    b 4
    r
    p argc
    EOD
    gdb -n -x gdb_commands hello
    I get this as output:
    Code:
    GNU gdb 5.3
    Copyright 2002 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-slackware-linux"...
    Breakpoint 1 at 0x804834c: file hello.c, line 4.
    
    Breakpoint 1, main () at hello.c:4
    4              printf("Hello world");
    gdb_commands:4: Error in sourced command file:
    No symbol "argc" in current context.
    (gdb)
    but when I run this script:
    Code:
    #include<stdio.h>
    int main(int argc, char **argv)
    {
           printf("Hello world");
           return 0;
    }
    EOD
    gcc -Wall -g hello.c -o hello
    cat > gdb_commands <<EOD
    set args a b c
    b 4
    r
    p argc
    EOD
    gdb -n -x gdb_commands hello
    I get this output:
    Code:
    GNU gdb 5.3
    Copyright 2002 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-slackware-linux"...
    Breakpoint 1 at 0x804834c: file hello.c, line 4.
    
    Breakpoint 1, main (argc=4, argv=0xbffff9a4) at hello.c:4
    4              printf("Hello world");
    $1 = 4
    (gdb)
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Nov 2007
    Posts
    6
    Omg,I forgot to type the int argc,char ** argv . In my source code , I did have those !!! I just forgot when posting here !
    And besides,the error message is "cannot access memory at 0x0" (not "no .. in current context").
    I must emphasize that I use Fedora 5 , and it leads to this error .
    At my school,the version is FC 2 and I debugged(exactly these commands) well (just like you !)
    I think the problem is FC 5 . It may seem weird to blame for the distro , but if someone is using FC 5,could you test it for me ?

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    So what happens when you copy and paste the following into a file, chmod that file to 700, and run it? Can you take the exact output from that and paste it into a reply to this thread?
    Code:
    #!/bin/bash
    
    cat > hello.c <<EOD
    #include<stdio.h>
    int main(int argc, char **argv)
    {
           printf("Hello world");
           return 0;
    }
    EOD
    gcc -Wall -g hello.c -o hello
    cat > gdb_commands <<EOD
    set args a b c
    b 4
    r
    p argc
    EOD
    gdb -n -x gdb_commands hello
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Just Joined!
    Join Date
    Nov 2007
    Posts
    6
    Here is the output
    Code:
     ./bame
    GNU gdb Red Hat Linux (6.3.0.0-1.134.fc5rh)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".
    
    Breakpoint 1 at 0x8048395: file hello.c, line 4.
    
    Breakpoint 1, main () at hello.c:4
    4              printf("Hello world");
    gdb_commands:4: Error in sourced command file:
    Cannot access memory at address 0x0
    (gdb)

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    That's funny. If I wanted to see output like that, I'd have to run a script something like this:
    Code:
    #!/bin/bash
    
    cat > hello.c <<EOD
    #include<stdio.h>
    int main(int argc, char **argv)
    {
           printf("Hello world");
           return 0;
    }
    EOD
    gcc -Wall -g hello.c -o hello
    cat > gdb_commands <<EOD
    set args a b c
    b 4
    r
    p *argv[argc]
    EOD
    gdb -n -x gdb_commands hello
    which for obvious reasons gives me:
    Code:
    GNU gdb 5.3
    Copyright 2002 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-slackware-linux"...
    Breakpoint 1 at 0x804834c: file hello.c, line 4.
    
    Breakpoint 1, main (argc=4, argv=0xbffff9a4) at hello.c:4
    4              printf("Hello world");
    gdb_commands:4: Error in sourced command file:
    Cannot access memory at address 0x0
    (gdb)
    I don't have the foggiest notion why you got the error you got with my previous script. I'm hoping someone else can jump in here.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  9. #9
    Just Joined!
    Join Date
    Nov 2007
    Posts
    6
    Can someone help me plz

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Can someone help me plz
    If you ran the script I offered in post 6, and that script was running a stock (unmodified) compiler and assembler and linker and debugger, and you got the output you describe in post 7, then I'm going to go out on a limb here and say that there is something seriously wrong with your system.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...