在Xcode中使用LLDB调试时如何更改变量值?

问题描述 投票:169回答:3

在Xcode中,GDB允许您在调试时更改局部变量(请参见how to change NSString value while debugging in XCode?)。 LLDB是否提供类似的功能?如果是这样,我们如何使用它?

xcode debugging lldb
3个回答
261
投票
expr myString = @"Foo"

((lldb)help expr计算当前的C / ObjC / C ++表达式程序上下文,使用当前作用域中的变量。这个命令接受“原始”输入(无需引用内容)。

语法:表达式-

命令选项的用法:表达式[-f] [-G][-d] [-u]-表达式[-o] [-d] [-u]-表达式

   -G <gdb-format>  ( --gdb-format <gdb-format> )
        Specify a format using a GDB format specifier string.

   -d <boolean>  ( --dynamic-value <boolean> )
        Upcast the value resulting from the expression to its dynamic type
        if available.

   -f <format>  ( --format <format> )
        Specify a format to be used for display.

   -o  ( --object-description )
        Print the object description of the value resulting from the
        expression.

   -u <boolean>  ( --unwind-on-error <boolean> )
        Clean up program state if the expression causes a crash, breakpoint
        hit or signal.

示例:

expr my_struct-> a = my_array [3]expr -f bin-(索引* 8)+ 5expr char c [] =“ foo”; c [0]

重要说明:由于此命令采用'原始'输入,因此,如果您使用任何命令选项,则在末尾之间必须使用'-'。命令选项和原始输入的开头。

'expr'是'expression'的缩写


18
投票

以下内容对我有用。我正在使用Xcode 8。

如果要将某些变量(例如“ dict”)设置为nil,然后测试代码流,则可以尝试以下操作。

  1. 初始化为所需值后正确放置断点。
  2. 然后在lldb命令行中执行“ expression dict = nil”进行更改。 (例如“ nil”)
  3. 越过断点。
  4. 在下一行中检查变量“ dict”。将为零。

它将在控制台中看起来像。

(lldb) expression dict = nil
(NSDictionary *) $5 = nil

3
投票

如果使用的是Xcode 10或11,则在将断点初始化为所需的变量后正确地放置断点,然后可以使用po myString = "Hello World"轻松更改变量。

© www.soinside.com 2019 - 2024. All rights reserved.