在OC函數或者objc_msgSend的入口處(第一行)加斷點,可用esp指針來探查參數。
以esp為基址,往後的偏移分別是:
0:函數執行完畢後的返回地址(不是返回值的地址哦)
4:對象實例的指針,即self指針
8:selector,實際是一個char數組型的字符串,即char*
12:(如果有)第一個參數
…(前一個參數的基址+前一個參數所占的字節數):(如果有)第n個參數
由此,要調試這樣一個函數
[cpp]
- (void)para1:(id)arg1 para2:(CGRect)p2 para3:(CGPoint)p3 para4:(id)p4
{
NSLog(@"para1:(id)arg1 para2:(CGRect)p2 para3:(CGPoint)p3 para4:(id)p4");
}
- (void)viewDidLoad
{
[self para1:[UIApplication sharedApplication] para2:CGRectMake(10, 20, 30, 40) para3:CGPointMake(50, 60) para4:self];
- (void)para1:(id)arg1 para2:(CGRect)p2 para3:(CGPoint)p3 para4:(id)p4
{
NSLog(@"para1:(id)arg1 para2:(CGRect)p2 para3:(CGPoint)p3 para4:(id)p4");
}
- (void)viewDidLoad
{
[self para1:[UIApplication sharedApplication] para2:CGRectMake(10, 20, 30, 40) para3:CGPointMake(50, 60) para4:self];
時,斷點後可在gdb這樣調試:
po *(id*)($esp+4) // 得到對象實例的描述
p (char*)*(SEL*)($esp+8) // 得到selector的名字
po *(id*)($esp+12) // 得到p1的description
p *(CGRect*)($esp+16) // 得到p2的各個成員值,輸出結果為:“(CGRect) $1 = origin=(x=10, y=20) size=(width=30, height=40)”
p *(CGPoint*)($esp+32) // 因為一個CGRect結構體占16字節(4個float),所以是“+32”,即“+16+16”, 得到p3的各個成員值,輸出結果為:“(CGPoint) $4 = (x=50, y=60)”
po *(id*)($esp+40) // 因為一個CGPoint結構體占8字節(2個float),所以是“+40”,即“+32+8”、得到p4的description
[plain]
(gdb) po *(id*)($esp+4)
<ViewController: 0x757f8c0>
(gdb) p (char*)*(SEL*)($esp+8)
$1 = 0xf37b "para1:para2:para3:para4:"
(gdb) po *(id*)($esp+12)
<UIApplication: 0x9250000>
(gdb) p *(CGRect*)($esp+16)
$2 = {
origin = {
x = 10,
y = 20
},
size = {
width = 30,
height = 40
}
}
(gdb) p *(CGPoint*)($esp+32)
$3 = {
x = 50,
y = 60
}
(gdb) po *(id*)($esp+40)
<ViewController: 0x757f8c0>
(gdb) po *(id*)($esp+4)
<ViewController: 0x757f8c0>
(gdb) p (char*)*(SEL*)($esp+8)
$1 = 0xf37b "para1:para2:para3:para4:"
(gdb) po *(id*)($esp+12)
<UIApplication: 0x9250000>
(gdb) p *(CGRect*)($esp+16)
$2 = {
origin = {
x = 10,
y = 20
},
size = {
width = 30,
height = 40
}
}
(gdb) p *(CGPoint*)($esp+32)
$3 = {
x = 50,
y = 60
}
(gdb) po *(id*)($esp+40)
<ViewController: 0x757f8c0>
下面是如何查看浮點型參數。
浮點型參數會通過SSE寄存器來傳遞,可以在gdb中這樣查看:
[cpp]
p $xmm0
p $xmm0
(lldb不能用上面的命令,暫沒去研究用什麼替代)
例如調試上例中的
[cpp]
CGRectMake(10, 20, 30, 40)
CGRectMake(10, 20, 30, 40)編譯器會把參數反序送入xmm寄存器組,即40傳入xmm0,30傳入xmm1……
在CGRectMake加斷點,執行到下圖中的位置時輸入命令
命令結果如下:[plain]
(gdb) p $xmm0
$1 = {
v4_float = {0, 0, 0, 40},
v2_double = {0, 5.4811317061554153e-315},
v16_int8 = {0 <repeats 12 times>, 66, 32, 0, 0},
v8_int16 = {0, 0, 0, 0, 0, 0, 16928, 0},
v4_int32 = {0, 0, 0, 1109393408},
v2_int64 = {0, 1109393408},
uint128 = 35467839930368
}
(gdb) p $xmm0
$1 = {
v4_float = {0, 0, 0, 40},
v2_double = {0, 5.4811317061554153e-315},
v16_int8 = {0 <repeats 12 times>, 66, 32, 0, 0},
v8_int16 = {0, 0, 0, 0, 0, 0, 16928, 0},
v4_int32 = {0, 0, 0, 1109393408},
v2_int64 = {0, 1109393408},
uint128 = 35467839930368
}因為SSE寄存器是128位的,gdb並不知道哪些bit是有意義的,所以列出了一些猜測的結果。
我們傳入的是CGFloat,即float,故v4_float = {0, 0, 0, 40} 是有