You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GDB中如何在$r1等于指定字符串时无条件触发断点?

Great question! Let's break this down so you can set that global conditional breakpoint in GDB exactly how you want.

First, let's clarify why your initial break [addr] if (...) approach has limits: that command only checks the condition when the program hits that specific address. So picking a single addr won't catch cases where $r1 holds your target string elsewhere in the code. To make it truly global—triggering no matter where the program is executing—we need a different strategy.

Option 1: Conditional Watchpoint (Best for Most Scenarios)

Watchpoints are designed to monitor changes to registers or memory. We can set one on $r1 that only triggers when it points to your target string:

watch $r1 if (strcmp((char *)$r1, "hello") == 0)
  • How this works: GDB will pause execution whenever $r1's value changes and the new value is a pointer to the string "hello".
  • A quick note: This won't trigger repeatedly if $r1 stays pointing to "hello" (it only checks when the register's value updates). If you need to catch every single instruction where $r1 holds the string—even if it hasn't changed—use the next option.

Option 2: Instruction-Level Global Check (Full Coverage, But Slow)

If you need to inspect $r1 at every single instruction (regardless of whether its value changed), you can set a breakpoint at the current program counter and use GDB's command system to auto-continue unless your condition is met:

  1. Start the program at its first instruction to set the initial breakpoint:
    starti
    
  2. Set a breakpoint at the current $pc and attach a command sequence:
    break *$pc
    commands
        if (strcmp((char *)$r1, "hello") == 0)
            echo Hit the global condition!\n
            # Leave the program paused here
        else
            # Auto-continue if the condition isn't met
            continue
        end
    end
    
  • Heads up: This will slow down execution a lot, since GDB pauses at every single instruction to check the condition. Only use this for small code snippets or very specific debugging tasks.

Troubleshooting strcmp in GDB

If you get an error about strcmp being undefined, make sure your program's debug symbols are loaded. If that's not an option, you can use a manual string comparison (it's less clean, but works):

watch $r1 if ((char *)$r1)[0] == 'h' && ((char *)$r1)[1] == 'e' && ((char *)$r1)[2] == 'l' && ((char *)$r1)[3] == 'l' && ((char *)$r1)[4] == 'o' && ((char *)$r1)[5] == '\0'

内容的提问来源于stack exchange,提问作者Neolex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:56:19