将类转储信息导入GDB [英] Import class-dump info into GDB
问题描述
有没有办法将输出从 class-dump
导入到GDB中?
示例代码:
$ cat> test.m
#include< stdio.h>
#import< Foundation / Foundation.h>
@interface TestClass:NSObject
+(int)randomNum;
@end
@implementation TestClass
+(int)randomNum {
return 4; //由公平的掷骰子选择。
//保证是随机的。
}
@end
int main(void){
printf(num:%d \,[TestClass randomNum]) ;
返回0;
}
^ D
$ gcc test.m -lobjc -o test
$ ./test
num:4
$ gdb test
...
(gdb)b + [TestClass randomNum]
在0x100000e5c处的断点1
(gdb)^ D
$ strip test
$ gdb test
...
(gdb)b + [TestClass randomNum]
函数+ [TestClass randomNum]未定义。
(gdb)^ d
$ class-dump -A test
...
@interface TestClass:NSObject
{
}
+(int)randomNum; // IMP = 0x0000000100000e50
@end
我知道我现在可以使用 b * 0x0000000100000e50
在 gdb
中,但是有没有办法修改GDB的符号表使其接受 b + [TestClass randomNum]
?
编辑: v6,而不仅仅是GDB v7,因为GDB v6是带有Apple补丁程序的最新版本。
可以加载符号文件在gdb中使用添加符号文件
命令。最难的部分是产生这个符号文件。 借助libMachObjC(它是 class-dump ),所以很容易转储所有地址及其对应的Objective-C方法。我已经写了一个小工具, objc-symbols 正是这样做的。
我们以Calendar.app为例。如果您尝试使用 nm
工具列出符号,您会注意到日历应用已被剥离:
$ nm -U /Applications/Calendar.app/Contents/MacOS/Calendar
0000000100000000 T __mh_execute_header
0000000005614542 - 00 0000 OPT radr:// 5614542
但是使用 objc-symbols
,您可以轻松地检索地址所有缺少的Objective-C方法:
$ objc-symbols /Applications/Calendar.app
00000001000c774c + [ CALCanvasAttributedText textWithPosition:size:text:]
00000001000c8936 - [CALCanvasAttributedText createTextureIfNeeded]
00000001000c8886 - [CALCanvasAttributedText bounds]
00000001000c883b - [CALCanvasAttributedText updateBezierRepresentation]
...
00000001000309eb - [CALApplication applicationDidFinishLaunching:]
...
然后,使用 SymTabCreator ,你可以创建一个符号文件,它实际上是一个空的dylib和所有符号。
使用 objc-symbols
和 SymTabCreator
一起很简单:
$ objc-symbols /Applications/Calendar.app | SymTabCreator -o Calendar.stabs
您可以检查 Calendar.stabs
包含所有符号:
$ nm Calendar.stabs
000000010014a58b T + [APLCALSource printingCachedTextSize]
000000010013e7c5 T + [APLColorSource alternateGenerator]
000000010013e780 T + [APLColorSource defaultColorSource]
000000010013e7bd T + [APLColorSource defaultGenerator]
000000010011eb12 T + [APLConstraint constraintOfClass:withProperties:]
...
00000001000309eb T - [CALApplication applicationDidFinishLaunching:]
...
<现在让我们来看看gdb会发生什么:
$ gdb --silent /Applications/Calendar.app
阅读共享库的符号.................................完成
没有符号文件:
(gdb)b - [CALApplication applicationDidFinishLaunching:]
函数 - [CALApplication applica tionDidFinishLaunching:]未定义。
在将来的共享库加载时使断点处于待处理状态? (y或[n])n
加载符号文件后:
(gdb)add-symbol-file Calendar.stabs
从文件Calendar.stabs添加符号表? (y或n)y
从/Users/0xced/Calendar.stabs...done中读取符号。
(gdb)b - [CALApplication applicationDidFinishLaunching:]
0x1000309f2的断点1
您会注意到,断点地址与符号地址(0x1000309f2与0x1000309eb,7个字节的差异)并不完全匹配,这是因为gdb会自动识别函数序言并在后面设置断点。
GDB脚本
您可以使用此GDB脚本自动运行这是因为剥离的可执行文件是当前的目标。
将脚本从下面添加到 .gdbinit
中,在gdb中运行命令 objc_symbols
:
$ $ $ $ $ gdb test
...
(gdb)b + [TestClass randomNum]
函数[TestClass randomNum]未定义。
(gdb)objc_symbols
(gdb)b + [TestClass randomNum]
0x100000ee1处的断点1
(gdb)^ D
define objc_symbols
shell rm -f / tmp / gdb-objc_symbols
在$ b $上设置记录重定向设置记录文件/ tmp / gdb-objc_symbols
设置记录在
信息目标
set注销
shell target =$(head -1 / tmp / gdb-objc_symbols | head -1 | awk -F'''{print $ 2}'); objc -symbols$ target| SymTabCreator -o / tmp / gdb-symtab
设置登录
添加符号文件/ tmp / gdb-symtab
设置记录关闭
结束
Is there a way to import the output from class-dump
into GDB?
Example code:
$ cat > test.m
#include <stdio.h>
#import <Foundation/Foundation.h>
@interface TestClass : NSObject
+ (int)randomNum;
@end
@implementation TestClass
+ (int)randomNum {
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
@end
int main(void) {
printf("num: %d\n", [TestClass randomNum]);
return 0;
}
^D
$ gcc test.m -lobjc -o test
$ ./test
num: 4
$ gdb test
...
(gdb) b +[TestClass randomNum]
Breakpoint 1 at 0x100000e5c
(gdb) ^D
$ strip test
$ gdb test
...
(gdb) b +[TestClass randomNum]
Function "+[TestClass randomNum]" not defined.
(gdb) ^D
$ class-dump -A test
...
@interface TestClass : NSObject
{
}
+ (int)randomNum; // IMP=0x0000000100000e50
@end
I know I can now use b *0x0000000100000e50
in gdb
, but is there a way of modifying GDB's symbol table to make it accept b +[TestClass randomNum]
?
Edit: It would be preferably if it would work with GDB v6 and not only GDB v7, as GDB v6 is the latest version with Apple's patches.
It’s possible to load a symbol file in gdb with the add-symbol-file
command. The hardest part is to produce this symbol file.
With the help of libMachObjC (which is part of class-dump), it’s very easy to dump all addresses and their corresponding Objective-C methods. I have written a small tool, objc-symbols which does exactly this.
Let’s use Calendar.app as an example. If you try to list the symbols with the nm
tool, you will notice that the Calendar app has been stripped:
$ nm -U /Applications/Calendar.app/Contents/MacOS/Calendar
0000000100000000 T __mh_execute_header
0000000005614542 - 00 0000 OPT radr://5614542
But with objc-symbols
you can easily retrieve the addresses of all the missing Objective-C methods:
$ objc-symbols /Applications/Calendar.app
00000001000c774c +[CALCanvasAttributedText textWithPosition:size:text:]
00000001000c8936 -[CALCanvasAttributedText createTextureIfNeeded]
00000001000c8886 -[CALCanvasAttributedText bounds]
00000001000c883b -[CALCanvasAttributedText updateBezierRepresentation]
...
00000001000309eb -[CALApplication applicationDidFinishLaunching:]
...
Then, with SymTabCreator you can create a symbol file, which is just actually an empty dylib with all the symbols.
Using objc-symbols
and SymTabCreator
together is straightforward:
$ objc-symbols /Applications/Calendar.app | SymTabCreator -o Calendar.stabs
You can check that Calendar.stabs
contains all the symbols:
$ nm Calendar.stabs
000000010014a58b T +[APLCALSource printingCachedTextSize]
000000010013e7c5 T +[APLColorSource alternateGenerator]
000000010013e780 T +[APLColorSource defaultColorSource]
000000010013e7bd T +[APLColorSource defaultGenerator]
000000010011eb12 T +[APLConstraint constraintOfClass:withProperties:]
...
00000001000309eb T -[CALApplication applicationDidFinishLaunching:]
...
Now let’s see what happens in gdb:
$ gdb --silent /Applications/Calendar.app
Reading symbols for shared libraries ................................. done
Without the symbol file:
(gdb) b -[CALApplication applicationDidFinishLaunching:]
Function "-[CALApplication applicationDidFinishLaunching:]" not defined.
Make breakpoint pending on future shared library load? (y or [n]) n
And after loading the symbol file:
(gdb) add-symbol-file Calendar.stabs
add symbol table from file "Calendar.stabs"? (y or n) y
Reading symbols from /Users/0xced/Calendar.stabs...done.
(gdb) b -[CALApplication applicationDidFinishLaunching:]
Breakpoint 1 at 0x1000309f2
You will notice that the breakpoint address does not exactly match the symbol address (0x1000309f2 vs 0x1000309eb, 7 bytes of difference), this is because gdb automatically recognizes the function prologue and sets the breakpoint just after.
GDB script
You can use this GDB script to automate this, given that the stripped executable is the current target.
Add the script from below to your .gdbinit
, target the stripped executable and run the command objc_symbols
in gdb:
$ gdb test
...
(gdb) b +[TestClass randomNum]
Function "+[TestClass randomNum]" not defined.
(gdb) objc_symbols
(gdb) b +[TestClass randomNum]
Breakpoint 1 at 0x100000ee1
(gdb) ^D
define objc_symbols
shell rm -f /tmp/gdb-objc_symbols
set logging redirect on
set logging file /tmp/gdb-objc_symbols
set logging on
info target
set logging off
shell target="$(head -1 /tmp/gdb-objc_symbols | head -1 | awk -F '"' '{ print $2 }')"; objc-symbols "$target" | SymTabCreator -o /tmp/gdb-symtab
set logging on
add-symbol-file /tmp/gdb-symtab
set logging off
end
这篇关于将类转储信息导入GDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!