next command in gdb is not working correctly

c, gdb

Solution

Anyone know what is wrong?

The symptoms you've described are very common when debugging programs built with optimization: the compiler re-arranges code in such a way that instructions from different lines become intermixed.

Make sure you compile without any `-O` flags, or add `-O0` at the end of your compilation lines.

here is part of the make file: `CC = gcc` `CFLAGS = -g -O2`

That would do it: remove `-O2` and your debugging would go much easier.

Problem

I am simply using gdb to step through a code line by line to learn how it is working and what it is doing. It worked fine the first time I did this, but now the next command is not working right. Sometimes it goes forward, sometimes it goes backwards. It doesn't make sense. Every time I do this it seems to be the same pattern. Below is an example. Anyone know what is wrong? Note: This is an old Mac with old program versions (That I do not have the power to update) Please be explicit. I am fairly new to linux and programming in general. Thanks! ``` Reading symbols for shared libraries ... done (gdb) b main Breakpoint 1 at 0x2730: file ../../../../gcc-3.4.6/libf2c/libF77/pow_zi.c, line 14. (gdb) r Starting program: /Users/kevin/project/ConstU/main nReading symbols for shared libraries +. done Error in re-setting breakpoint 1: Function "main" not defined. Re-enabling shared library breakpoints: 1 Breakpoint 1, 0x00002730 in main () at main.c:34 34 { (gdb) n main () at main.c:42 42 for (i=0;i<DpDIM;i++) { (gdb) n 34 { (gdb) n 35 runstart=clock(); (gdb) n 39 Init=ReadInit(&CaseDim);/*reads in initial valies from initfile*/ (gdb) n 35 runstart=clock(); (gdb) n 39 Init=ReadInit(&CaseDim);/*reads in initial valies from initfile*/ (gdb) ```

Original source