0

Ive tried multiple variations of this, but none of them seem to work. Any ideas?

in ViewController.m

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 300, 300)];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];

Another file UIView+Frame.h I defined a color macro

#define RGBACOLOR(r,g,b,a) [UIColor colorWithRGB:r Green:g Blue:b Alpha:a]

Actual operation When the program executes [self.view addSubview: view1];I am going to use LLDB to use the defined macros to modify the color, but it prompts an error and I do n’t know how to modify it. The wrong result is as follows

(lldb) e view1.backgroundColor = (UIColor *)RGBACOLOR(0,0,0,1)
error: use of undeclared identifier 'RGBACOLOR'
(lldb) 
Zuqiu
  • 9
  • 1

1 Answers1

1

Macros are not traditionally well supported by debug info. The DWARF standard (which is the most common one on macOS & other Unixen) has a way to store the information, but it is so verbose very few toolchains support it. So there's no natural way for the debugger to know about defines.

If you have a small number of fairly independent macros you want to use in debug expressions, you can put them in a .h file, and set that file as lldb's "expression prefix". This will get included into the source of every expression you run subsequently. Do this by:

(lldb) settings set target.expr-prefix ~/my-common-defines.h

You can't get too ambitious here (e.g. #include <unistd.h> won't work). The problem is that most system header files are conditioned by a set of other #defines. lldb doesn't know what their values are, so at some point preprocessing the expr-prefix will fail.

Alternatively, clang also has a concept called "modules" which is an attempt to make the collection of headers from some package more shareable for repeated compilation. It actually captures some of the info that would cause the expr-prefix parsing to fail. So if the macro you want to access is in a set of headers that are built into a Clang module, then you can import the module into lldb's expression context, and that will make the defines from the modules available as well. So for instance:

(lldb) expr -l objc -- @import Foundation

will make all the Foundation macro definitions available.

Similarly, if your headers are modular (this page goes into depth on what this means:

https://clang.llvm.org/docs/Modules.html

) then you can import the module you've created, and its defines will be available to the expression parser.

Jim Ingham
  • 25,260
  • 2
  • 55
  • 63
  • hello, I don't know what went wrong with these steps: 1.Created a file `lldb-defines.h` 2.Add the following code `#define RGBACOLOR (r, g, b, a) [UIColor colorWithRGB: r Green: g Blue: b Alpha: a] ` To `lldb-defines.h` 3.Add the code `settings set target.expr-prefix ~ / lldb-defines.h` under the path` ~ / .lldbinit` 4.Restart Xocode, When I give the view device color in the console, I cannot use the macros in the `lldb-prefix.h file` – Zuqiu Feb 25 '20 at 16:01
  • Did you make sure you were either stopped in an ObjC stack-frame when you ran the expression, or use `-l objc --` as shown above? – Jim Ingham Feb 25 '20 at 18:10
  • I confirm that I am using oc project, and the breakpoint is using `e view.background = (UIColor *) RGBACOLOR (1, 1, 1, 1)`,but there are still problems and failures, you can reproduce me See if it works – Zuqiu Feb 26 '20 at 17:20
  • Please file a bug with a little test case that shows your issue either with http://bugs.llvm.org or http://bugreporter.apple.com and somebody will take a look. It's better to start with exactly the same code & build - which is easier to do with a bug. – Jim Ingham Feb 26 '20 at 18:52