如何在循环后立即断点?

发布于 2024-12-21 05:39:03 字数 1163 浏览 1 评论 0原文

#include <iostream>
#include <vector>

using namespace std; 
int in;

bool isPrime(int n) {
    for (int i = 3; i <= n; i ++) {
        if (n%i != 0) {
            return false;
        }
    }
    return true;
}

vector<int>* generateVector(int n) {
    vector<int> v;
    for (int i = 2; i < 20; i ++) {
        if (i == n) {
            continue;
        }

        if (isPrime(i+n)) {
            v.push_back(i);
        }
    }
}

int main()
{
    while(1) {
        cin >> in;
        vector<int>* nVectors[21];
        for (int i = 1; i <= 20; i ++) {
            nVectors[i] = generateVector(i);
        } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    }
}

这是一些 C++ 代码。我想在 for 循环之后设置一个断点(箭头显示位置)。 我找到了一种方法来解决这个问题,方法是在它后面添加一条语句,然后在该语句中设置断点。但添加没有意义的陈述会让感觉不太好。那么有没有更好的解决方案呢?

我正在使用 GDB 进行调试。

PS:我已经知道如何在gdb中设置断点。我的目的是在 for 循环结束后中断,并显示 nVectors 中的内容。谢谢。

对大家抱歉。这不是gdb或调试技巧的问题,而是我的代码中有一个错误。因此,当我打印 nVectors 时,没有打印任何内容。修复后,您提供的每种方法都可以正常工作。谢谢。

#include <iostream>
#include <vector>

using namespace std; 
int in;

bool isPrime(int n) {
    for (int i = 3; i <= n; i ++) {
        if (n%i != 0) {
            return false;
        }
    }
    return true;
}

vector<int>* generateVector(int n) {
    vector<int> v;
    for (int i = 2; i < 20; i ++) {
        if (i == n) {
            continue;
        }

        if (isPrime(i+n)) {
            v.push_back(i);
        }
    }
}

int main()
{
    while(1) {
        cin >> in;
        vector<int>* nVectors[21];
        for (int i = 1; i <= 20; i ++) {
            nVectors[i] = generateVector(i);
        } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    }
}

This some c++ code. And i would like to make a break point just after the for loop(the arrow show the position).
I have found one way to solve it through adding a statement after it then make break point in this statement. But adding a statement without a meaning make not feeling good. So is there a better solution?

I am using GDB for debugging.

PS: I have already known how to set breakpoint in gdb. My intent is to break after the for loop ends, and display what in nVectors. Thanks.

Sorry for all. It's not the issue about the gdb or debugging skill, but there is a bug in my code. So when i print nVectors, nothing was printed. After fixing it, every method you provides works fine. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

荒芜了季节 2024-12-28 05:39:03

您可以使用 程序集 断点,只需要了解程序集for循环的代码。

gdb <your_bin>
layout split
break *0x80488ad              #assmebly code will look like 
                              #0x80488ad     jmp    0x8048874 <main+18>     

You can use a assembly break point, just need to understand the assembly code of for loop.

gdb <your_bin>
layout split
break *0x80488ad              #assmebly code will look like 
                              #0x80488ad     jmp    0x8048874 <main+18>     
╄→承喏 2024-12-28 05:39:03

gdb 有一个添加断点的命令

,有几种方法,但我认为可能对您有帮助的是:

(gdb) break filename:linenumber

例如我想在 main.c 中的第 10 行

中断 main.c 中断。 c:10

你可能想尝试一下教程 http://www.yolinux.com/TUTORIALS/GDB-Commands.html

gdb has a command to add break points

there are a couple of ways, but I think the one that might help you is :

(gdb) break filename:linenumber

so for example I want to break at line 10 in main.c

break main.c:10

you might want try a tutorial http://www.yolinux.com/TUTORIALS/GDB-Commands.html

ゃ懵逼小萝莉 2024-12-28 05:39:03

不,调试器必须有一些语句才能设置断点。

Nope there has to be some statement for the debugger to be able to set breakpoint.

梦巷 2024-12-28 05:39:03

我认为没有办法直接做你想做的事,但你仍然可以做到。在循环的最后一条语句上设置断点。当调试器中断时,切换反汇编视图并向下滚动,直到找到要放置真正断点的位置。这将有效地在特定地址而不是源文件中的行号上设置断点。

I don't think there is a way to directly do what you want but you can still do it. Set a breakpoint on the last statement of the loop. When the debugger breaks switch the disassembly view and scroll down until you find where you want to place the real breakpoint. This will effective set a breakpoint at a specific address rather than on a line number in a source file.

您可以在 gdb 命令行上指定源文件和循环结束的行,如下所示:

b file.c:123

如果您想在 for 循环之后立即执行,则需要实际设置结束行的中断我相信 while() 循环的括号。但您可以同时尝试两种方法,看看哪种方法能达到所需的结果。

You can specify the source file and line of the end of the loop, like so, on the gdb command line:

b file.c:123

If you want to go right after the for loop, you'll want to actually set the break for the line of the closing bracket of the while() loop, I believe. But you can try both and see what gives the desired result.

心如狂蝶 2024-12-28 05:39:03

尝试使用 until 命令。避免单步执行循环很有用。同样来自 gdb 手册

直到如果程序试图退出当前的程序,它总是会停止
堆栈帧。

Try to use until command. It is useful to avoid single stepping through a loop. Also from gdb manual:

until always stops your program if it attempts to exit the current
stack frame.

物价感观 2024-12-28 05:39:03

如果是我,我会像其他人所说的那样设置一个汇编断点。但如果你不想进入这个,有一个更简单的方法:因为你知道循环计数(20),而且它不是很大,你可以在循环内的最后一个语句上设置一个断点(在你的例子中) ,循环内的唯一语句),带有条件

if (i == 19)

或者,您可以设置忽略计数:

ignore bnum 19

其中 bnum 是断点编号。这可能会更快,但在您的情况下,差异可以忽略不计。

有关详细信息,请参阅 gdb 文档中的断点、观察点和捕获点

If it was me, I'd set an assembly breakpoint, like the others said. But if you don't want to get into that, there's a simpler way: Since you know the loop count (20), and it is not very big, you can set a breakpoint on the last statement inside the loop (in your case, the only statement inside the loop), with the condition

if (i == 19)

Alternatively, you can set an ignore count:

ignore bnum 19

where bnum is the breakpoint number. This is probably faster, but in your case the difference will be negligible.

See Breakpoints, watchpoints, and catchpoints in the gdb documentation for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文