繁体   English   中英

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

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

问题是,当我在 #include 行设置断点时,gdb 只是忽略该行并在主程序中的下一条指令处停止(我用g++ -g -O2 -std=c++11编译了 main.cpp g++ -g -O2 -std=c++11 )。

该程序运行良好(-O2 根本不影响结果),但我想检查该文件中到底做了什么,但我不能,因为 gdb 不允许我在文件中输入代码。

如何调试其他文件中的代码? 甚至有可能吗?

编辑:这是代码

主程序

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

包括.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这是我要调试的代码。 我决定将它与主要分开,因为它会增长很多。

// 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

我希望这会有所帮助。 请注意,一切都编译得很好并且工作得很好(我确保编译时定义的宏是正确的)

注意:通过调试(不是正确的词)我的意思是检查函数的工作方式(我不完全理解的函数)。

可能您可以在 MyFunction() 处中断,然后运行 ​​'bt' 命令以查看堆栈。 然后你会看到,是否有任何额外的堆栈框架或堆栈框架由源文件组成,它可能会有所帮助

首先:

include是一个从不生成代码的预处理器指令。 调试器只能在可以执行的事情上停止。 包含文件在编译期间有效,而不是在运行时。

下一个:

您包含的文件将定义一些值、函数、类和许多其他内容。 所以你必须让调试器知道在哪里停止。

总而言之:包括“cpp”文件真的很垃圾! 很少有理由这样做。

但是好的,如何进行:

如果您的头文件(或包含的 cpp 文件)提供了一个函数,您只需执行一个break Func并运行您的程序即可。 之前不需要在 gdb 的 gui 中打开任何文件。

如果您想查看包含的文件,您还可以list myheader.h:1 第一个是您要开始查看文件的代码行。

还有一个提示:请提供更小的代码示例,人们可以自己编译这些示例,以便为您提供更详细的帮助。 你这个例子真的不好理解!

示例会话:

标题:fh

#include <stdlib.h>

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

void f(void)
{
    malloc(2000);

}

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


}

示例会话:

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

现在您可以使用step遍历您的子程序。

暂无
暂无

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

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