Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb is jumping over lines

Tags:

c++

gdb

i have some problems on understanding gdb.

i have a main function, i wrote this main function on myself.

Some lines in this main, call some functions in a library, i think library name is not important but it is tesseract-ocr.

my line in main which call a function, a constructor is here:

choiceItr = new tesseract::ChoiceIterator(itr);

i put a break point at gdb on above line, and run, when it stops on that line i use step command to go into the function.

Here is the library function who is called:

ChoiceIterator::ChoiceIterator(const ResultIterator& result_it) {
  ASSERT_HOST(result_it.it_->word() != NULL);
  tesseract_ = result_it.tesseract_;
  PAGE_RES_IT res_it(*result_it.it_);
  WERD_CHOICE* best_choice = res_it.word()->best_choice;
  BLOB_CHOICE_LIST_CLIST* choices = best_choice->blob_choices();
  if (choices != NULL) {
    BLOB_CHOICE_LIST_C_IT blob_choices_it(choices);
    for (int blob = 0; blob < result_it.blob_index_; ++blob)
      blob_choices_it.forward();
    choice_it_ = new BLOB_CHOICE_IT(blob_choices_it.data());
    choice_it_->mark_cycle_pt();
  } else {
    choice_it_ = NULL;
  }
}

then i use "next" command of gdb to walk in function.

here is my gdb console:

Breakpoint 1, pixsOfOneWord (langString=0x8049e7c "klm", 
    imageString=0x8049e71 "paket2.tif", outputData=0x8049c7b, 
    datapathString=0x8049e6f ".") at deneme234.cpp:161
161 choiceItr = new tesseract::ChoiceIterator(itr);
(gdb) step
tesseract::ChoiceIterator::ChoiceIterator (this=0x819e6f0, result_it=...)
    at resultiterator.cpp:234
234     choice_it_ = new BLOB_CHOICE_IT(blob_choices_it.data());
(gdb) next
225   ASSERT_HOST(result_it.it_->word() != NULL);
(gdb) list
220     return it_->word()->box_word->BlobPosition(blob_index_) == SP_DROPCAP;
221   return false;
222 }
223 
224 ChoiceIterator::ChoiceIterator(const ResultIterator& result_it) {
225   ASSERT_HOST(result_it.it_->word() != NULL);
226   tesseract_ = result_it.tesseract_;
227   PAGE_RES_IT res_it(*result_it.it_);
228   WERD_CHOICE* best_choice = res_it.word()->best_choice;
229   BLOB_CHOICE_LIST_CLIST* choices = best_choice->blob_choices();
(gdb) next
278 }  // namespace tesseract.
(gdb) next
226   tesseract_ = result_it.tesseract_;
(gdb) next
278 }  // namespace tesseract.
(gdb) next
226   tesseract_ = result_it.tesseract_;
(gdb) next
230   if (choices != NULL) {
(gdb) 

as you can see,

tesseract_ = result_it.tesseract_;

line has been called two times, why?

PAGE_RES_IT res_it(*result_it.it_);
WERD_CHOICE* best_choice = res_it.word()->best_choice;
BLOB_CHOICE_LIST_CLIST* choices = best_choice->blob_choices();

also when i "next", above lines was not called, why?

Thanks in advnce.

like image 804
merveotesi Avatar asked Nov 09 '11 12:11

merveotesi


People also ask

How do you stop an infinite loop in gdb?

Start calc from within gdb using the run command. It will go into an infinite loop. Press Ctrl-C (like before) to stop your program.

What does jump do in gdb?

Description. This command jumps the program counter to the specified location. The debugger resumes program execution at that point unless it encounters a breakpoint there.

How do I stop gdb debugging?

To exit GDB, use the quit command (abbreviated q ), or type an end-of-file character (usually C-d ). If you do not supply expression , GDB will terminate normally; otherwise it will terminate using the result of expression as the error code.


1 Answers

The library you are stepping into has been built with optimization and debug symbols (most likely -g -O2, which is the default for Linux builds).

Debugging optimized code is somewhat hard, as control flow optimization causes the code to "jump around", some variables become "<optimized out>", etc.

You can rebuild the library with CXXFLAGS = -g -O0, or you can learn to debug with optimization on.

The latter is a very useful skill, as many times your program will only crash in optimized mode, and you'll have to debug it in that mode anyway.

like image 181
Employed Russian Avatar answered Sep 28 '22 00:09

Employed Russian