0%

如何利用 LLDB 来 Debug

介绍

鉴于现在调试的频繁性和重要性,此文记录了一些基本的 LLDB 调试命令,方便日后查阅。

常用命令 p、po、call、print

1
2
3
4
5
6
7
8
9
10
11
12
p         -- ('expression --')  Evaluate an expression (ObjC++ or Swift) in
the current program context, using user defined variables and
variables currently in scope.
po -- ('expression -O -- ') Evaluate an expression (ObjC++ or Swift)
in the current program context, using user defined variables and
variables currently in scope.
print -- ('expression --') Evaluate an expression (ObjC++ or Swift) in
the current program context, using user defined variables and
variables currently in scope.
call -- ('expression --') Evaluate an expression (ObjC++ or Swift) in
the current program context, using user defined variables and
variables currently in scope.

从官方的描述来看, pprintcall 是一样的,但是 po 就不太一样了,输入一样但是输出不一样。 po 的输出的是具体对象的内容。

如果想要按照特定的格式来打印,如下:

1
2
3
4
5
6
7
8
9
10
(lldb) p/s self.title
(__NSCFString *) $0 = @"目的地"
(lldb) p self.title
(__NSCFString *) $1 = 0x00007fe0c0d4d230 @"目的地"
(lldb) p/x self.title
(__NSCFString *) $2 = 0x00007fe0c0d4d230 @"目的地"
(lldb) p/t self.title
(__NSCFString *) $3 = 0b0000000000000000011111111110000011000000110101001101001000110000 @"目的地"
(lldb) p/a self.title
(__NSCFString *) $4 = 0x00007fe0c0d4d230 -> 0x000000010d2fd2c8 (void *)0x000000010d2fd278: __NSCFString @"目的地"

打印输出格式化

name description
x Regard the bits of the value as an integer, and print the integer in hexadecimal.
d Print as integer in signed decimal.
u Print as integer in unsigned decimal.
o Print as integer in octal.
t Print as integer in binary. The letter ‘t’ stands for “two”.
a Print as an address, both absolute in hexadecimal and as an offset from the nearest preceding symbol. You can use this format used to discover where (in what function) an unknown address is located:(gdb) p/a 0x54320 $3 = 0x54320 <_initialize_vx+396>
c Regard as an integer and print it as a character constant. This prints both the numerical value and its character representation. The character representation is replaced with the octal escape ‘\nnn’ for characters outside the 7-bit ascii range.Without this format, gdb displays char, unsigned char, and signed char data as character constants. Single-byte members of vectors are displayed as integer data.
f Regard the bits of the value as a floating point number and print using typical floating point syntax.
s Regard as a string, if possible. With this format, pointers to single-byte data are displayed as null-terminated strings and arrays of single-byte data are displayed as fixed-length strings. Other values are displayed in their natural types.Without this format, gdb displays pointers to and arrays of char, unsigned char, and signed char as strings. Single-byte members of a vector are displayed as an integer array.
z Like ‘x’ formatting, the value is treated as an integer and printed as hexadecimal, but leading zeros are printed to pad the value to the size of the integer type.
r Print using the ‘raw’ formatting. By default, gdb will use a Python-based pretty-printer, if one is available (see Pretty Printing). This typically results in a higher-level display of the value’s contents. The ‘r’ format bypasses any Python pretty-printer which might exist.

格式: p/x $pc 参考链接:打印输出格式化

lldb 声明变量

1
2
3
(lldb) e NSString *$str = @"http://www.baidu.com"
(lldb) po $str
http://www.baidu.com

我们使用 e 开头声明了变量

调用变量的 API

1
(lldb) po [self.title uppercaseString]

强转返回值类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(lldb) po [self.title characterAtIndex:0]
U+76ee u'目'

(lldb) po (unsigned int)[self.title characterAtIndex:0]
30446

(lldb) po (char)[self.title characterAtIndex:0]
'\xee'

(lldb) po (NSString *)[self.title characterAtIndex:0]
0x00000000000076ee

(lldb) po (unichar)[self.title characterAtIndex:0]
U+76ee u'目'

添加断点

1
2
b 120
b [KLNewDestinationVC setupUI]

b 命令后面加行号,或者指定某个对象调用的方法

设置断点触发条件

右键断点处,在 condition 处编辑,如果设置了条件,只要当条件满足时,才会进入断点,也可以设置条件满足时发出声音和打印提示语。

常用打印视图层次结构

1
(lldb) po [self.view recursiveDescription]

临时刷新界面 UI

1
2
3
(lldb) e ((UIButton *)sender).backgroundColor = [UIColor redColor]
(UICachedDeviceRGBColor *) $41 = 0x00007fdd10715b00
(lldb) e (void)[CATransaction flush]