如何基于对象字符串属性在Xcode中设置条件断点?

时间:2022-11-28 22:50:36

I'm looking to be able to have the debugger break when it reaches a particular string match. As an example, I might have something like this:

我希望调试器在达到特定的字符串匹配时能够中断。举个例子,我可能会有这样的东西:

Foo myObj = [self gimmeObj];

myObj might have a property called name. I want the debugger to stop on the assignment when

myObj可能有一个名为name的属性。我想让调试器在赋值时停止

[myObj.name isEqualToString:@"Bar"];

How can I set my conditional breakpoint in Xcode to do that?

如何在Xcode中设置条件断点呢?

5 个解决方案

#1


162  

You can set a conditional break point in Xcode by setting the breakpoint normally, then control-click on it and select Edit Breakpoint (choose Run -> Show -> Breakpoints).

您可以在Xcode中通过设置断点来设置条件断点,然后控制点击它并选择编辑断点(选择Run -> Show ->断点)。

In the breakpoint entry, there is a Condition column.

在断点条目中,有一个条件列。

Now, there are several issues to keep in mind for the condition. Firstly, gdb does not understand dot syntax, so instead of myObj.name, you must use [myObj name] (unless name is an ivar).

现在,有几个问题需要牢记于心。首先,gdb不理解点语法,因此代替myobject .name,您必须使用[myObj name](除非名称是ivar)。

Next, as with most expressions in gdb, you must tell it the type of return result, namely "BOOL". So set a condition like:

接下来,与gdb中的大多数表达式一样,您必须告诉它返回结果的类型,即“BOOL”。所以设定一个条件:

(BOOL)[[myObj name] isEqualToString:@"Bar"]

Often it is actually easier to just do this in code by temporarily adding code like:

通常,通过临时添加代码(如:

if ( [myObj.name isEqualToString:@"Bar"] ) {
    NSLog( @"here" );
}

and then setting the break point on the NSLog. Then your condition can be arbitrarily complex without having to worry about what gdb can and can't parse.

然后在NSLog上设置断点。然后,您的条件可以是任意复杂的,而不必担心gdb可以解析和不能解析什么。

#2


6  

I'm not sure if this will work, but you can try setting a breakpoint at that line of code, open up the debugger console (Cmd+Shift+R), and type

我不确定这是否可行,但是您可以尝试在该代码行设置一个断点,打开调试器控制台(Cmd+Shift+R)和类型。

condition N (int)[[myObj name] isEqualToString:@"Bar"]

Where N is replaced by the number of the breakpoint (an integer).

其中N被断点的数目(一个整数)所取代。

#3


6  

Here is how you do using XCode lldb conditional breakpoints.

下面介绍如何使用XCode lldb条件断点。

First, double click the break point (or right click edit breakpoint), you can see a dialog popup.

首先,双击断点(或右键单击编辑断点),您可以看到一个对话框弹出。

如何基于对象字符串属性在Xcode中设置条件断点?

Here is what those option means:

以下是这些选项的含义:

  1. Condition: The breakpoint will only fire under this condition.
  2. 条件:在这种情况下,断点只会触发。
  3. Ignore: The amount of times the condition needs to meet before fire the breakpoint
  4. 忽略:在触发断点之前,条件需要满足的次数
  5. Action: Action that runs after the breakpoint breaks.
  6. 操作:断点过后运行的操作。
  7. Options: Automatically continue after evaluating actions
  8. 选项:在评估动作后自动继续

Here is a summary. For the above example in image, it means that when the variable buildingId is equal to 13, break here. If I add ignore time to 1, then it will ignore the first time when buildingId is equal to 13 and break at the second time the condition is met.

这是一个总结。对于上图中的示例,它意味着当变量buildingId等于13时,在这里中断。如果我将ignore time添加到1,那么它将在buildingId第一次等于13时忽略,并在第二次满足条件时中断。

For actions, when you press add actions, there will be a list of choice. Usually what I do is to use the Debugger Command po to print variables that I need to check and I believe that there are better ways using the actions then I do.

对于操作,当您按add actions时,将有一个选项列表。通常我所做的是使用调试器命令po打印需要检查的变量,我相信有更好的方法可以使用这些操作。

It seems that you have to recompile and run the app if you change the conditions at runtime

如果你在运行时改变了条件,你似乎必须重新编译并运行这个应用程序

#4


2  

If you mutate myObj.name using the setter, you can add a symbolic breakpoint on -[MyObjClass setName:] either from the Debugger Console or from the Run->Manage Breakpoints->Add Symbolic Breakpoint menu in Xcode. If not (why not? you probably shouldn't be modifying the instance variable directly except in the designated initializer or dealloc) you can set a watchpoint in gdb (use the Debugger Console in Xcode once the debugger is running). This page explains how. I don't believe Xcode exposes a UI for setting watchpoints without using the Debugger Console.

如果您使用setter修改myobject .name,您可以在-[MyObjClass setName:]上添加一个符号断点,可以从调试器控制台或从Run->管理断点->在Xcode中添加符号断点菜单。如果不是(为什么不呢?您可能不应该直接修改实例变量,除非在指定的初始化器或dealloc中)您可以在gdb中设置一个观察点(在调试器运行之后,使用Xcode中的调试控制台)。本页解释了。我不相信Xcode在不使用调试器控制台的情况下公开了设置观察点的UI。

#5


0  

At times when working with Frameworks (debug builds) and need to put a breakpoint in certain file/location that is hard to navigate or isn't exposed publically in framework under development. One option is to write a helper class to trigger conditional breakpoints & make step-in/step-out easier.

有时在使用框架(调试构建)时,需要在特定的文件/位置上放置一个断点,这很难在开发的框架中公开或公开。一种选择是编写一个帮助类来触发条件断点,并使插入/退出更容易。

- (void)invokeFrameworkMethod {
    ...
    [DebugConditionalBreakPointHelper breakPointCondition:YES comment:@"from invokeFrameworkMethod."];
    ...
}

Header declaration in framework under development.

开发框架中的头声明。

#import <Foundation/Foundation.h>

@interface DebugConditionalBreakPointHelper : NSObject
+ (void)breakPointCondition:(BOOL)enabled comment:(NSString *)comment;
@end

And implementation file:

和实现文件:

#import "DebugConditionalBreakPointHelper.h"

@implementation DebugConditionalBreakPointHelper
+ (void)breakPointCondition:(BOOL)enabled comment:(NSString *)comment {
    if (enabled)
    {
        NSLog(@"Triggerred Conditional Break Point. Comment: %@");
    }
}
@end

#1


162  

You can set a conditional break point in Xcode by setting the breakpoint normally, then control-click on it and select Edit Breakpoint (choose Run -> Show -> Breakpoints).

您可以在Xcode中通过设置断点来设置条件断点,然后控制点击它并选择编辑断点(选择Run -> Show ->断点)。

In the breakpoint entry, there is a Condition column.

在断点条目中,有一个条件列。

Now, there are several issues to keep in mind for the condition. Firstly, gdb does not understand dot syntax, so instead of myObj.name, you must use [myObj name] (unless name is an ivar).

现在,有几个问题需要牢记于心。首先,gdb不理解点语法,因此代替myobject .name,您必须使用[myObj name](除非名称是ivar)。

Next, as with most expressions in gdb, you must tell it the type of return result, namely "BOOL". So set a condition like:

接下来,与gdb中的大多数表达式一样,您必须告诉它返回结果的类型,即“BOOL”。所以设定一个条件:

(BOOL)[[myObj name] isEqualToString:@"Bar"]

Often it is actually easier to just do this in code by temporarily adding code like:

通常,通过临时添加代码(如:

if ( [myObj.name isEqualToString:@"Bar"] ) {
    NSLog( @"here" );
}

and then setting the break point on the NSLog. Then your condition can be arbitrarily complex without having to worry about what gdb can and can't parse.

然后在NSLog上设置断点。然后,您的条件可以是任意复杂的,而不必担心gdb可以解析和不能解析什么。

#2


6  

I'm not sure if this will work, but you can try setting a breakpoint at that line of code, open up the debugger console (Cmd+Shift+R), and type

我不确定这是否可行,但是您可以尝试在该代码行设置一个断点,打开调试器控制台(Cmd+Shift+R)和类型。

condition N (int)[[myObj name] isEqualToString:@"Bar"]

Where N is replaced by the number of the breakpoint (an integer).

其中N被断点的数目(一个整数)所取代。

#3


6  

Here is how you do using XCode lldb conditional breakpoints.

下面介绍如何使用XCode lldb条件断点。

First, double click the break point (or right click edit breakpoint), you can see a dialog popup.

首先,双击断点(或右键单击编辑断点),您可以看到一个对话框弹出。

如何基于对象字符串属性在Xcode中设置条件断点?

Here is what those option means:

以下是这些选项的含义:

  1. Condition: The breakpoint will only fire under this condition.
  2. 条件:在这种情况下,断点只会触发。
  3. Ignore: The amount of times the condition needs to meet before fire the breakpoint
  4. 忽略:在触发断点之前,条件需要满足的次数
  5. Action: Action that runs after the breakpoint breaks.
  6. 操作:断点过后运行的操作。
  7. Options: Automatically continue after evaluating actions
  8. 选项:在评估动作后自动继续

Here is a summary. For the above example in image, it means that when the variable buildingId is equal to 13, break here. If I add ignore time to 1, then it will ignore the first time when buildingId is equal to 13 and break at the second time the condition is met.

这是一个总结。对于上图中的示例,它意味着当变量buildingId等于13时,在这里中断。如果我将ignore time添加到1,那么它将在buildingId第一次等于13时忽略,并在第二次满足条件时中断。

For actions, when you press add actions, there will be a list of choice. Usually what I do is to use the Debugger Command po to print variables that I need to check and I believe that there are better ways using the actions then I do.

对于操作,当您按add actions时,将有一个选项列表。通常我所做的是使用调试器命令po打印需要检查的变量,我相信有更好的方法可以使用这些操作。

It seems that you have to recompile and run the app if you change the conditions at runtime

如果你在运行时改变了条件,你似乎必须重新编译并运行这个应用程序

#4


2  

If you mutate myObj.name using the setter, you can add a symbolic breakpoint on -[MyObjClass setName:] either from the Debugger Console or from the Run->Manage Breakpoints->Add Symbolic Breakpoint menu in Xcode. If not (why not? you probably shouldn't be modifying the instance variable directly except in the designated initializer or dealloc) you can set a watchpoint in gdb (use the Debugger Console in Xcode once the debugger is running). This page explains how. I don't believe Xcode exposes a UI for setting watchpoints without using the Debugger Console.

如果您使用setter修改myobject .name,您可以在-[MyObjClass setName:]上添加一个符号断点,可以从调试器控制台或从Run->管理断点->在Xcode中添加符号断点菜单。如果不是(为什么不呢?您可能不应该直接修改实例变量,除非在指定的初始化器或dealloc中)您可以在gdb中设置一个观察点(在调试器运行之后,使用Xcode中的调试控制台)。本页解释了。我不相信Xcode在不使用调试器控制台的情况下公开了设置观察点的UI。

#5


0  

At times when working with Frameworks (debug builds) and need to put a breakpoint in certain file/location that is hard to navigate or isn't exposed publically in framework under development. One option is to write a helper class to trigger conditional breakpoints & make step-in/step-out easier.

有时在使用框架(调试构建)时,需要在特定的文件/位置上放置一个断点,这很难在开发的框架中公开或公开。一种选择是编写一个帮助类来触发条件断点,并使插入/退出更容易。

- (void)invokeFrameworkMethod {
    ...
    [DebugConditionalBreakPointHelper breakPointCondition:YES comment:@"from invokeFrameworkMethod."];
    ...
}

Header declaration in framework under development.

开发框架中的头声明。

#import <Foundation/Foundation.h>

@interface DebugConditionalBreakPointHelper : NSObject
+ (void)breakPointCondition:(BOOL)enabled comment:(NSString *)comment;
@end

And implementation file:

和实现文件:

#import "DebugConditionalBreakPointHelper.h"

@implementation DebugConditionalBreakPointHelper
+ (void)breakPointCondition:(BOOL)enabled comment:(NSString *)comment {
    if (enabled)
    {
        NSLog(@"Triggerred Conditional Break Point. Comment: %@");
    }
}
@end