简体   繁体   English

gdb 不会与 #include 指令排成一行

[英]gdb doesn't stop in a line with #include directive

The problem is that, when I set a breakpoint at the line of the #include, gdb just ignore the line and stop at the next instruction in the main (I compiled the main.cpp with g++ -g -O2 -std=c++11 ).问题是,当我在 #include 行设置断点时,gdb 只是忽略该行并在主程序中的下一条指令处停止(我用g++ -g -O2 -std=c++11编译了 main.cpp g++ -g -O2 -std=c++11 )。

The program works perfect (-O2 doesn't affect the result at all), but I want to check what exactly does something inside that file, but I can't because gdb doesn't let me enter the code inside the file.该程序运行良好(-O2 根本不影响结果),但我想检查该文件中到底做了什么,但我不能,因为 gdb 不允许我在文件中输入代码。

How can I debug code inside other file?如何调试其他文件中的代码? Is it even possible?甚至有可能吗?

Edit : Here is the code编辑:这是代码

main.cpp主程序

#include <iostream>
#include <cstdlib>
#include <chrono>
#include "inc/includes.h"

template <class T>
void PrintVector(T* vector, int size){
  for (int i=0; i<size; ++i){
    std::cout << vector[i] << " ";
  }
  std::cout << std::endl;
}

template <class T>
void CheckTime(void (*f)(T*&, int), T* &vector, int size){
  std::chrono::high_resolution_clock::time_point tantes, tdespues;
  std::chrono::duration<double> transcurrido;

  tantes = std::chrono::high_resolution_clock::now();
  (*f)(vector, size);
  tdespues = std::chrono::high_resolution_clock::now();

  transcurrido = std::chrono::duration_cast<std::chrono::duration<double>(tdespues - tantes);

  std::cout << size << " " << transcurrido.count() << std::endl;
}

int main(int argc, char * argv[]){
  if (argc != 2){
    std::cerr << "Formato " << argv[0] << " <num_elem>" << std::endl;
    return -1;
  }

  int n = atoi(argv[1]);
  int range;

#if defined RADIXSORTLSD || defined RADIXSORTMSD
  unsigned short * array = new unsigned short[n];
  range = (n<65536)?n:65536;
#else
  unsigned int * array = new unsigned int[n];
  range = n;
#endif
  srand(time(0));

  for (int i = 0; i < n; i++){
    array[i] = rand()%range;
  }

#ifdef PRINT
  PrintVector(array, n);
#endif

#include "inc/select.h"  //Here is the problem for debugging

#ifdef PRINT
  PrintVector(array, n);
#endif
}

includes.h包括.h

#include "../src/radixsortlsd.cpp"
#include "../src/radixsortmsd.cpp"
#include "../src/mergesort.cpp"
#include "../src/bitonicsort.cpp"
#include "../src/insertion.cpp"
#include "../src/slowsort.cpp"
#include "../src/selection.cpp"

select.h This is the code I want to debug. select.h这是我要调试的代码。 I decided to separate it from the main because it will grow a lot.我决定将它与主要分开,因为它会增长很多。

// The calls to CheckTime takes the first parameter as the direction to a function, previously defined inside the cpps of includes.h
#ifdef RADIXSORTLSD
CheckTime(&RadixSortLSD, array, n);
#endif
#ifdef RADIXSORTMSD
CheckTime(&RadixSortMSD, array, n);
#endif
#ifdef MERGESORT
CheckTime(&MergeSort, array, n);
#endif
#ifdef INSERTION
CheckTime(&Insertion, array, n);
#endif
#ifdef SLOWSORT
CheckTime(&SlowSort, array, n);
#endif
#ifdef SELECTION
CheckTime(&Selection, array, n);
#endif
#ifdef BITONICSORT
CheckTime(&BitonicSort, array, n);
#endif

I hope this help.我希望这会有所帮助。 Note that everything compiles great and works great (I made sure that the macros I defined when compiling are the correct ones)请注意,一切都编译得很好并且工作得很好(我确保编译时定义的宏是正确的)

Note : By debugging (not the right word) I meant checking how a function works (a function I don't fully understand).注意:通过调试(不是正确的词)我的意思是检查函数的工作方式(我不完全理解的函数)。

Possibly you could break at MyFunction(), then run 'bt' command to see the stack.可能您可以在 MyFunction() 处中断,然后运行 ​​'bt' 命令以查看堆栈。 Then you see, is there any additional stack frame or what stack frames consist of in terms of source files, it might help然后你会看到,是否有任何额外的堆栈框架或堆栈框架由源文件组成,它可能会有所帮助

First of all:首先:

A include is a preprocessor directive which never generates code. include是一个从不生成代码的预处理器指令。 Debuggers can stop only on things which can be executed.调试器只能在可以执行的事情上停止。 Including a file works during compilation, not during runtime.包含文件在编译期间有效,而不是在运行时。

The next:下一个:

Your included files will define some values, functions, classes and a lot other.您包含的文件将定义一些值、函数、类和许多其他内容。 So you must give the debugger an idea where to stop.所以你必须让调试器知道在哪里停止。

And at all: Including 'cpp' files is really trash!总而言之:包括“cpp”文件真的很垃圾! There are only very seldom reasons to do this.很少有理由这样做。

But ok, how to proceed:但是好的,如何进行:

If your header file ( or included cpp file ) provides a function, you simply can do a break Func and run your program.如果您的头文件(或包含的 cpp 文件)提供了一个函数,您只需执行一个break Func并运行您的程序即可。 No need to open any file in a gui for gdb before.之前不需要在 gdb 的 gui 中打开任何文件。

If you want to look inside the included files, you also can list myheader.h:1 .如果您想查看包含的文件,您还可以list myheader.h:1 The 1 one is the line of code you want to start looking into the file.第一个是您要开始查看文件的代码行。

And a hint: Please provide much smaller code examples which persons can compile for themselves to give you more detailed help.还有一个提示:请提供更小的代码示例,人们可以自己编译这些示例,以便为您提供更详细的帮助。 You example is really bad to understand!你这个例子真的不好理解!

Example session:示例会话:

Header: fh标题:fh

#include <stdlib.h>

void g(void)
{
    malloc(4000);
}

void f(void)
{
    malloc(2000);

}

main.cpp:主.cpp:

#include "f.h"
 int main(void)
{
    int i;
    int* a[10];

    for (i = 0; i < 10; i++) {
        a[i] = (int*)malloc(1000);
    }

    f();

    g();

    for (i = 0; i < 10; i++) {
        free(a[i]);
        return 0;
    }


}

Example session:示例会话:

> gdb prog
gdb) break f
Breakpoint 1 at 0x40061a: file main.cpp, line 10.
(gdb) run
Starting program: /home/xxx/go 

Breakpoint 1, f () at f.h:10
 10     malloc(2000);
(gdb) list
 5      malloc(4000);
 6  }
 7  
 gdb ) 

Now you can walk through your subroutine with step .现在您可以使用step遍历您的子程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM