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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9907387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:20:46  来源:igfitidea点击:

How to change variables value while debugging with LLDB in Xcode?

xcodedebugginglldb

提问by Eric

In Xcode, GDB allows you to change local variables while debugging (see how to change NSString value while debugging in XCode?). Does LLDB offer a similar functionality? If so, how can we use it?

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

回答by Matthias Bauch

expr myString = @"Foo"

(lldb) help expr
Evaluate a C/ObjC/C++ expression in the current program context, using variables currently in scope. This command takes 'raw' input (no need to quote stuff).

Syntax: expression --

Command Options Usage: expression [-f ] [-G ] [-d ] [-u ] -- expression [-o] [-d ] [-u ] -- expression

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

Examples:

expr my_struct->a = my_array[3]
expr -f bin -- (index * 8) + 5
expr char c[] = "foo"; c[0]

IMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options you must use ' -- ' between the end of the command options and the beginning of the raw input.

'expr' is an abbreviation for 'expression'

(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 -- (index * 8) + 5
expr char c[] = "foo"; c[0]

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

“expr”是“表达式”的缩写

回答by arango_86

The following stuff works for me. I am using Xcode 8.

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

If you want to set some variable (for example a "dict") to nil and then test the code flow, you can try the following.

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

  1. Put the breakpoint properly after initialised to the desired value.
  2. then execute "expression dict = nil" in lldb command line to change it. (for example "nil")
  3. Step over the break point.
  4. Check the variable "dict" in the next line. It will be nil.
  1. 初始化为所需值后正确放置断点。
  2. 然后在lldb命令行中执行“expression dict = nil”来改变它。(例如“零”)
  3. 跨过断点。
  4. 检查下一行中的变量“dict”。它将为零。

It will look something like in the console.

它看起来像在控制台中。

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

回答by atalayasa

If you are using Xcode 10 or 11 put the breakpoint properly after initialised to the required variable then you can change your variable using po myString = "Hello World"easily.

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