GDB and C loops

hello every one …

i’m having a problem debugging a c program using GDB

the problem is i can’t set a break point in source code after a for loop for example the following code




  1. #include <stdio.h>
  1. 
  1. void main()
  1. {
  1. 	int i = 0;
  1. 
  1. 	printf("Count from 1 to 3
");
  1. 
  1. 	for(i = 1 ; i <= 3 ; i++)
  1. 	{
  1. 		printf("%i
",i);
  1. 	}
  1. 
  1. 	printf("Finished
");
  1. 	printf("Now to Exit
");
  1. }
  1. 



when i try to set a break point at line 14 : printf("Finished
");
i get the following message

No line 14 in file "loops_and_gdb.c".

It’s line 10 you want.

no no it’s my fault the topic is wrongly formatted

here’s the code listing

   │1       #include <stdio.h>                                                                                                                      │
   │2                                                                                                                                               │
   │3       void main()                                                                                                                             │
   │4       {                                                                                                                                       │
   │5               int i = 0;                                                                                                                      │
   │6                                                                                                                                               │
   │7               printf("Count from 1 to 3
");                                                                                                  │
   │8                                                                                                                                               │
   │9               for(i = 1 ; i <= 3 ; i++)                                                                                                       │
   │10              {                                                                                                                               │
   │11                      printf("%i
",i);                                                                                                       │
   │12              }                                                                                                                               │
   │13                                                                                                                                              │
   │14              printf("Finished
");                                                                                                           │
   │15              printf("Now to Exit
");                                                                                                        │
   │16      }

Works for me.

$ gdb a.out
GNU gdb (GDB) SUSE (7.1-3.12)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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-suse-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /tmp/a.out...done.(gdb) break 14
Breakpoint 1 at 0x4005c7: file x.c, line 14.
(gdb) run
Starting program: /tmp/a.out 
... (some warning messages)
Count from 1 to 3
1
2
3

Breakpoint 1, main () at x.c:14
14              printf("Finished
");
(gdb) quit

On Tue, 12 Jul 2011 23:06:03 +0000, mostafaxxx wrote:

> the problem is i can’t set a break point in source code after a for loop
> for example the following code

Check to make sure you don’t have any extra blank lines in the actual
code - if it were me, I’d be inclined to try different line numbers and
see what works - and when you find something that works, look and see
what line of the code is and if it matches up.

Jim

Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

RMS’s gdb Tutorial: How do I use breakpoints?

well that’s strange i’ve removed all blank lines from the source code recompile and link but nothing changed

mostafa@linux-45gd:~/Documents/problem> gcc -g loops_and_gdb.c -o loops_and_gdb
mostafa@linux-45gd:~/Documents/problem> gdb -q loops_and_gdb
(gdb) list
1	#include <stdio.h>
2	void main()
3	{
4		int i = 0;
5		printf("Count from 1 to 3
");
6		for(i = 1 ; i <= 3 ; i++)
7		{
8			printf("%i
",i);
9		}
10		printf("Finished
");
(gdb) 
11		printf("Now to Exit
");
12	}
(gdb) b 10
No line 10 in file "loops_and_gdb.c".

On Thu, 14 Jul 2011 00:06:03 +0000, mostafaxxx wrote:

> well that’s strange i’ve removed all blank lines from the source code
> recompile and link but nothing changed

So, again, I’d try using different values and see what it comes back
with. Try b 9 and b 11, see what the results you get are.

Jim

Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

using the gdb gui Insight

http://i53.tinypic.com/2l89c8g.jpg

as you can see from the screenshot i can’t set breakpoints on any thing after line 7

On Thu, 14 Jul 2011 21:06:02 +0000, mostafaxxx wrote:

> using the gdb gui Insight
>
> [image: http://i53.tinypic.com/2l89c8g.jpg]
>
> as you can see from the screenshot i can’t set breakpoints on any thing
> after line 7

Try doing a verification on the gdb package - it sounds like something in
the package might be messed up.

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

Try doing a verification on the gdb package - it sounds like something in
the package might be messed up.

that sounds very mush possible
see ,i have built the Insight Debugger from source so it might indeed missed up the GDB pakages…

any way how can verify the gdb package ??

On Thu, 14 Jul 2011 22:06:02 +0000, mostafaxxx wrote:

>> Try doing a verification on the gdb package - it sounds like something
>> in
>> the package might be messed up.
>
> that sounds very mush possible
> see ,i have built the Insight Debugger from source so it might indeed
> missed up the GDB pakages…
>
>
> any way how can verify the gdb package ??

The ‘rpm’ command has a verify option:

rpm -V <packagename>

will get you started. See

man rpm

For details on what the output means.

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

well i got no output i guess that mean the package is fine
(RPM produces output only if there is a verification failure.)


# rpm -q gdb
gdb-7.2-3.3.i586

===============================================================================================================================

# rpm -qi gdb
Name        : gdb                          Relocations: (not relocatable)
Version     : 7.2                               Vendor: openSUSE
Release     : 3.3                           Build Date: Wed 23 Feb 2011 12:40:36 AM EET
Install Date: Sat 30 Apr 2011 09:08:11 AM EEST      Build Host: build21
Group       : Development/Debuggers         Source RPM: gdb-7.2-3.3.src.rpm
Size        : 5189817                          License: GPLv3+ and GPLv3+ with exceptions and GPLv2+ and GPLv2+ with exceptions and GPL+ and LGPLv2+ and GFDL and BSD and Public Domain
Signature   : RSA/8, Wed 23 Feb 2011 12:42:28 AM EET, Key ID b88b2fd43dbdc284
Packager    : http://bugs.opensuse.org
URL         : http://gnu.org/software/gdb/
Summary     : A GNU source-level debugger for C, C++, Java and other languages
Description :
GDB, the GNU debugger, allows you to debug programs written in C, C++,
Java, and other languages, by executing them in a controlled fashion
and printing their data.
Distribution: openSUSE 11.4

================================================================================================================================

# rpm -qs gdb
normal        /usr/bin/gcore
normal        /usr/bin/gdb
normal        /usr/bin/gdb-add-index
normal        /usr/bin/gdbtui
normal        /usr/bin/gstack
normal        /usr/share/doc/packages/gdb
normal        /usr/share/doc/packages/gdb/COPYING
normal        /usr/share/doc/packages/gdb/COPYING.LIB
normal        /usr/share/doc/packages/gdb/COPYING3
normal        /usr/share/doc/packages/gdb/NEWS
normal        /usr/share/doc/packages/gdb/README
normal        /usr/share/gdb
normal        /usr/share/gdb/python
normal        /usr/share/gdb/python/gdb
normal        /usr/share/gdb/python/gdb/FrameIterator.py
normal        /usr/share/gdb/python/gdb/FrameWrapper.py
normal        /usr/share/gdb/python/gdb/__init__.py
normal        /usr/share/gdb/python/gdb/backtrace.py
normal        /usr/share/gdb/python/gdb/command
normal        /usr/share/gdb/python/gdb/command/__init__.py
normal        /usr/share/gdb/python/gdb/command/alias.py
normal        /usr/share/gdb/python/gdb/command/backtrace.py
normal        /usr/share/gdb/python/gdb/command/ignore_errors.py
normal        /usr/share/gdb/python/gdb/command/pahole.py
normal        /usr/share/gdb/python/gdb/command/require.py
normal        /usr/share/gdb/python/gdb/command/save_breakpoints.py
normal        /usr/share/gdb/python/gdb/command/upto.py
normal        /usr/share/gdb/python/gdb/function
normal        /usr/share/gdb/python/gdb/function/__init__.py
normal        /usr/share/gdb/python/gdb/function/caller_is.py
normal        /usr/share/gdb/python/gdb/function/in_scope.py
normal        /usr/share/gdb/syscalls
normal        /usr/share/gdb/syscalls/amd64-linux.xml
normal        /usr/share/gdb/syscalls/gdb-syscalls.dtd
normal        /usr/share/gdb/syscalls/i386-linux.xml
normal        /usr/share/gdb/syscalls/ppc-linux.xml
normal        /usr/share/gdb/syscalls/ppc64-linux.xml
normal        /usr/share/gdb/syscalls/sparc-linux.xml
normal        /usr/share/gdb/syscalls/sparc64-linux.xml
normal        /usr/share/info/annotate.info.gz
normal        /usr/share/info/gdb.info.gz
normal        /usr/share/info/gdbint.info.gz
normal        /usr/share/info/stabs.info.gz
normal        /usr/share/man/man1/gdb.1.gz
normal        /usr/share/man/man1/gdbtui.1.gz
normal        /usr/share/man/man1/gstack.1.gz

================================================================================================================================

# rpm -V gdb


again

i built Insight debugger from source http://www.tech.dmu.ac.uk/~glapworth/src/insight/insight-6.8-1-gl.tar.gz