Problem in debugging GDB

I use GDB to debug a C program, but I find that GDB runs several codes twice.

For instance,

.... stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) ); stream_sys_t *p_sys; if( !s ) return NULL; s->p_input = p_access->p_input; s->psz_path = strdup( p_access->psz_path ); .... 

Debug gdb,

 292 stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) ); Missing separate debuginfos, use: debuginfo-install dbus-libs-1.2.16-9.fc12.i686 libcap-ng-0.6.2-3.fc12.i686 (gdb) next 295 if( !s ) (gdb) 292 stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) ); (gdb) 295 if( !s ) (gdb) 298 s->p_input = p_access->p_input; (gdb) 299 s->psz_path = strdup( p_access->psz_path ); (gdb) 298 s->p_input = p_access->p_input; (gdb) 299 s->psz_path = strdup( p_access->psz_path ); 

I am embarrassed. Could you explain why?

thanks

+4
source share
2 answers

In fact, it does not execute the same code twice. Optimization of the compiler can lead to a reordering of machine instructions, so that some instructions that were generated for the second source line are placed before the last instruction for the first line of the source. The "next" Gdb command stops when the original line corresponding to the command changes, although it can actually just execute the rest of the original line, which has not yet been completed.

+4
source

Try to compile without any optimization (-O0) and run again. Another idea is to set the clock on s-> p_input and see if this structure field is changed twice.

+1
source

All Articles