gdb 正在跳过行

2022-01-20 00:00:00 gdb c++

我对 gdb 的理解有些问题.

i have some problems on understanding gdb.

我有一个main函数,这个main函数是我自己写的.

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

main 中的一些行,调用库中的一些函数,我认为库名并不重要,但它是 tesseract-ocr.

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

我在 main 中调用函数的行,构造函数在这里:

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

choiceItr = new tesseract::ChoiceIterator(itr);

我在上面一行的 gdb 处放了一个断点,然后运行,当它在那一行停止时,我使用 step 命令进入函数.

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.

这是被调用的库函数:

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;
  }
}

然后我使用 gdb 的next"命令进入函数.

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

这是我的 gdb 控制台:

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) 

如你所见,

tesseract_ = result_it.tesseract_;

line 被调用了两次,为什么?

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?

谢谢.

推荐答案

您要进入的库已经构建了优化和调试符号(很可能是 -g -O2,这是默认的对于 Linux 版本).

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

调试优化代码有点困难,因为控制流优化会导致代码跳来跳去",一些变量变成<optimized out>"等等.

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

您可以使用 CXXFLAGS = -g -O0 重新构建库,或者您可以学习使用优化进行调试.

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.

相关文章