1

I'm just trying to learn to use gdb at this point. The program I'm using it on works perfectly fine; I'm not trying to debug it or anything; I'm just testing the functionality of gdb. Here's the source code:

#include <stdio.h> #include <ctype.h> #include <string.h> int main( int argc, char **argv ){ int wordcount = 0; int len = strlen( argv[1] ); for( int i = 0; i < len; i++ ){ if( isspace( argv[1][i] ) && !isspace( argv[1][i-1] ) ) wordcount++; } if( !isspace( argv[1][len-1] ) ) wordcount++; if( wordcount == 0 && len > 0 ) // if all characters were non-whitespace, wordcount = 1; // then there was exactly one word printf( "%d\n", wordcount ); return 0; } 

I started gdb and ran the program, setting breakpoints at lines 7 and 9. I used the backtrace and step commands, and I don't understand their output at all. When I typed "backtrace full" gdb gave me this:

#0 _start () at ../sysdeps/i386/elf/start.S:65 No locals. 

What exactly does this mean? What is _start? What is ../sysdeps/i386/elf/start.S:65? And how can there be no locals, when clearly I have declared wordcount and len? I have tried Google, but every tutorial I can find on gdb shows it producing completely different (more detailed) output from what I got. When I Google the string I got, I get a bunch of results on the ARM architecture.

7
  • 2
    Make sure you compile with debug symbols — on GCC, that's the -g flag to each compile command. Commented Nov 13, 2015 at 19:03
  • 2
    Or even better, use -ggdb. Commented Nov 13, 2015 at 19:27
  • 1
    The error is at the beginning of the for-loop. If i is 0, what could i-1 be? That index is out of bounds. Commented Nov 13, 2015 at 19:31
  • Okay, the -g option seems to provide somewhat more explicit output. Haven't tried -ggdb yet. And yeah, I noticed the error in the program when you pointed it out, ott. That should be fairly easy to fix. I only wrote the program to test gdb though, so the error doesn't really matter. Commented Nov 13, 2015 at 20:29
  • @ott: True, but OP wasn't asking about code correctness. It's a subtle bug; it will trip only if the first character of the command-line argument is whitespace, and you have to work at it to make that happen. It could be triggered on purpose; not likely by accident. Could be a security issue, depending on the use case. Commented Nov 13, 2015 at 20:31

1 Answer 1

2

The program I'm using it on works perfectly fine;

For some definition of works. Your program has at least 2 bugs.

When I typed "backtrace full" gdb gave me this: ... What exactly does this mean?

It's hard to tell without knowing which commands you used before reaching this point. Most likely you did next until you returned from main, and thus landed in _start (which is the routine that calls main, and which is where the execution of any program usually starts).

What is ../sysdeps/i386/elf/start.S:65?

The _start routine is part of GLIBC, and is defined in sysdeps/i386/elf/start.S source file.

how can there be no locals, when clearly I have declared wordcount and len?

You are not inside main, so whatever locals are present in main is irrelevant: they are only active while main is executing, and it's not (either not yet, or not any longer).

Sign up to request clarification or add additional context in comments.

5 Comments

"For some definitions of works. Your program has at least 2 bugs." I can see the bounds checking bug. Where is the other one?
@ZenHacker argv[1] may be NULL, in which case strlen will crash. argv[1] may start with a space, in which case argv[1][-1] will be accessed.
Okay, thanks for pointing that out. I tend to be a somewhat sloppy programmer.
@ZenHacker : Here is someone who appears to have helped. If the answer they gave you helped solve your issue, you may wish to consider accepting it.
@ZenHacker Great. Thanks, it helps SO keep track of solved/unsolved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.