4

I use GDB to debug a C program, but I find GDB execute some codes twice.

For example,

 ....
    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 );
  ....

GDB Debugging,

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 confused. Could you explain why?

Thanks

2
  • 2
    Do you have compiler optimisations enabled? Commented Apr 29, 2011 at 8:20
  • @Oli Charlesworth: yes, I think it is enabled. Commented May 3, 2011 at 4:01

2 Answers 2

4

It is not actually executing the same code twice. Compiler optimizations can cause machine instructions to be reordered, such that some instructions that were generated for the second source line are placed before the last instruction for the first source line. Gdb's "next" command stops when the source line corresponding to the instruction changes, even though it may actually just be executing the rest of a source line that was not finished yet.

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

2 Comments

I see. If it is, how could I know or trace the program`s running?
If it bothers you, you can compile without optimization as Pih suggested. If you are interested in the actual optimizations you can always disassemble the code and step by machine instructions instead of by source line (nexti, stepi).
1

Try to compile without any optimization (-O0) and run again. Another idea, it is to put a watch on s->p_input and see if this structure field is modified twice.

2 Comments

Worked? What was the problem?
I think the reason is compiler optimization. So I just turn it off by using 'g++ -o0'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.