0

I have just shifted to ubuntu and newly using gdb and g++ . Please forgive me if my question is silly .

This is from Richard Stevens Advanced Linux Programming . Three files were created in the folder names reciprocal main.c:

#include <stdio.h>
#include "reciprocal.hpp"
int main (int argc, char **argv)
{
  int i;
  i = atoi (argv[1]);
  printf ("The reciprocal of %d is %g\n", i, reciprocal (i));
  return 0;
}

reciprocal.cpp:

#include <cassert>
#include "reciprocal.hpp"
double reciprocal (int i) {
  // I should be non-zero.
  assert (i != 0);
  return 1.0/i;
}

reciprocal.hpp:

#ifdef __cplusplus
extern "C" {
#endif
extern double reciprocal (int i);

#ifdef __cplusplus
}
#endif

After compiling , I ran the command (gdb) reciprocal and the (gdb) run . I was expecting something as in the book

Starting program: reciprocal
Program received signal SIGSEGV, Segmentation fault.
__strtol_internal (nptr=0x0, endptr=0x0, base=10, group=0)
at strtol.c:287
287 strtol.c: No such file or directory.
(gdb)

But I got :

Starting program: /home/trafalgar/Desktop/reciprocal/reciprocal
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000

Program received signal SIGSEGV , Segmentation fault.
0x00007ffff7a56ad4 in ?? () from /lib/x86_64-linux-gnu/libc.so.6

What might be happening different . Is this a version problem or anything else ?

Here is the Makefile

reciprocal: main.o reciprocal.o
           g++ $(CFLAGS) -o reciprocal main.o reciprocal.o
main.o: main.c reciprocal.hpp
           gcc $(CFLAGS) -c main.c
reciprocal.o: reciprocal.cpp reciprocal.hpp
           g++ $(CFLAGS) -c reciprocal.cpp
clean:
      rm -f *.o reciprocal
abkds
  • 1,764
  • 7
  • 27
  • 43

3 Answers3

3

How did you compile the program? use g++ -g programname.c

also, when you do gdb reciprocal

note if there is a message like loaded symbols from ... or couldnot find symbols

if you get output similar to 2nd code statement, then the problem is that you did not use -g symbol.

Shehbaz Jaffer
  • 1,944
  • 8
  • 23
  • 30
  • I used the -g symbol but still its the same , any other suggestions – abkds Mar 04 '14 at 11:22
  • could you please paste the exact output when you write gdb reciprocal starting from the point where you execute the program? – Shehbaz Jaffer Mar 04 '14 at 11:25
  • 1
    symbols are not getting generated, hence they are not loaded. We will need your compilation statement. – Shehbaz Jaffer Mar 04 '14 at 11:33
  • I used a Makefile , and then `make CFLAGS=-g` – abkds Mar 04 '14 at 11:38
  • Please have a look at the make file – abkds Mar 04 '14 at 11:40
  • add this line to Makefile on the top CFLAGS=-g I am able to get following output: – Shehbaz Jaffer Mar 04 '14 at 11:47
  • shehbaz@debian:~/stackoverflow$ gdb reciprocal GNU gdb (GDB) 7.4.1-debian Copyright (C) 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later 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-linux-gnu". For bug reporting instructions, please see: ... Reading symbols from /home/shehbaz/stackoverflow/reciprocal...done. – Shehbaz Jaffer Mar 04 '14 at 11:48
  • @shehbaz Yes till this part its the same but when i run the `run` command then it is showing no information – abkds Mar 04 '14 at 11:56
2

You should compile with all warnings and debug info, i.e.

 gcc -Wall -g -c main.c
 g++ -Wall -g -c reciprocal.cpp

then link with

 g++ -g main.o reciprocal.o -o reciprocal

So add

CFLAGS= -Wall -g

in your Makefile. See also this.

Then run the debugger with

 gdb reciprocal

then set a program argument with set args 12 command to (gdb) prompt at last start the debugged program with run when having the (gdb) prompt

Of course, if you don't have any program arguments, argc is 1 and argv[1] is NULL, which you should not pass to atoi(3).

The debugger works quite well. The bug is in your code. You should handle correctly the case when argc is 1 and argv[1] is NULL.

If you encounter a segmentation fault inside a C library function, use the bt or backtrace gdb command to understand how you get there.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Everything is working properly, with expected output.

Compare:

1. Starting program: reciprocal
2. Program received signal SIGSEGV, Segmentation fault.
3. __strtol_internal (nptr=0x0, endptr=0x0, base=10, group=0)
4. at strtol.c:287
5. 287 strtol.c: No such file or directory.

A. Starting program: /home/trafalgar/Desktop/reciprocal/reciprocal
B. warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
C. Program received signal SIGSEGV , Segmentation fault.
D. 0x00007ffff7a56ad4 in ?? () from /lib/x86_64-linux-gnu/libc.so.6

Lines 1-5 are your expected output (from Mr Stevens' text), lines A-D are your actual output.

Lines 1 & A, are essentially identical, they both specify the filename of the executable, (1) is relative pathed, (A) has full path. No worries.

Line B... this is NORMAL, this is gdb telling you that you don't have the debugging information installed for your library functions (NOT YOUR CODE, the dynamically linked libraries on your system).

Line C... Same as (2), easy enough.

Line D... Well, since we don't have debug info for the library functions, it can only point out where the error was as best it can: libc.so.6 (standard library functions, of which strtol is one such)

Essentially, line D is similar to lines 3-5. Without the debug information installed/available, you're not going to get much more information than this.

But everything is working as expected. You're fine.

For help on how to install debug symbols, see here: how-to-use-debug-libraries-on-ubuntu

Fear not! You're doing great. (Technically, the error is on line 6 of your main.cpp, since argv[1] is pretty much undefined because you didn't supply an argument, perhaps confusing since atoi() is often replaced with strtol() behind the scenes.)

Try:

gdb --args ./reciprocal 15

or similar to test with arguments.

Community
  • 1
  • 1
lornix
  • 1,946
  • 17
  • 14