Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the condition of conditional breakpoints in Swift with self and enum?

I have a enum of HTTP methods:

enum HTTPMethod: String {
  case GET = "GET"
  case POST = "POST"
}

and I have a request class and a request wrapper class:

class Request {
  let method: HTTPMethod = .GET
}

class RequestWrapper {
  let request: Request

  func compareToRequest(incomingRequest: NSURLRequest) -> Bool {

     // Next line is where the conditional breakpoint set.
     return request.method.rawValue == incomingRequest.HTTPMethod
  }
}

I set a conditional breakpoint on the line:

return request.method.rawValue == incomingRequest.HTTPMethod

with condition:

self.request.method == HTTPMethod.POST

and then the debugger stop at the line with an error message:

Stopped due to an error evaluating condition of breakpoint 1.1:     
"self.request.method == HTTPMethod.POST"
Couldn't parse conditional expression:
<EXPR>:1:1: error: use of unresolved identifier 'self'
self.request.HTTPMethod == HTTPMethod.POST

And if I delete self and change the condition to:

request.method == HTTPMethod.POST

Error message is as the following lines:

Stopped due to an error evaluating condition of breakpoint 1.1:  
"request.method == HTTPMethod.POST"
Couldn't parse conditional expression:
<EXPR>:1:1: error: could not find member 'method'
request.method == HTTPMethod.POST
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Is there a way to solve this question?

Update:

It's able to check the value of self.request.method using LLDB command:

fr v self.request.method

And if I use a local constant to store the value, the debugger can be stopped at the right position:

// Use a local constant to store the HTTP method
let method = request.method

// Condition of breakpoint
method == HTTPMethod.POST

Update 2:

I am using Xcode 6.3.1

like image 495
mrahmiao Avatar asked Apr 25 '15 08:04

mrahmiao


People also ask

How do I set a conditional breakpoint in Xcode?

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). In the breakpoint entry, there is a Condition column. Now, there are several issues to keep in mind for the condition.

How do you set conditional Breakpoints?

To set a conditional breakpoint, activate the context menu in the source pane, on the line where you want the breakpoint, and select “Add Conditional Breakpoint”. You'll then see a textbox where you can enter the expression. Press Return to finish.

How do you add a breakpoint in Swift?

If the thrown error has a helpful error message, that may be enough information to resolve the problem. If not, add a Swift error breakpoint to pause on the line that throws the error. In the Breakpoint navigator, click the Add button (+) in the lower-left corner, and choose Swift Error Breakpoint.

What are conditional Breakpoints?

Conditional breakpoints allow you to break inside a code block when a defined expression evaluates to true. Conditional breakpoints highlight as orange instead of blue. Add a conditional breakpoint by right clicking a line number, selecting Add Conditional Breakpoint , and entering an expression.


1 Answers

This is pretty clearly an lldb bug. You don't mention what version of the tools you are using. If you aren't using 6.3.1 or better, please try again using that. If you are still seeing a problem, please file a bug with http://bugreporter.apple.com.

Note, frame var and expr are pretty different beasts. frame var only prints the values of local variables, using the DWARF debug information directly, but is NOT an expression evaluator. So for instance, it doesn't know enough to do ==. I imagine if you did:

(lldb) self.request.method == HTTPMethod.POST

when stopped at that breakpoint you would see the same effect.

The expression parser has to play the extra trick of posing as a method of your class (to get transparent references through self, etc to work) and, it is a little tricky to get that right. Apparently we aren't doing the job correctly in your case.

like image 60
Jim Ingham Avatar answered Sep 20 '22 21:09

Jim Ingham