objective-c - 用于跟踪 iOS 设备上的 Objective-C 调用的 GDB 脚本 - 问题

标签 objective-c ios gdb objective-c-runtime

我有一个正在处理的 gdb 脚本,用于跟踪所有通过 objc_msgSend 的 Objective-C 方法调用,但我遇到了一个我似乎无法处理的问题。在查看 Objective-C 运行时源代码后,我开发了以下脚本以在 objc_msgSend 的每个中断处打印 [ ]。问题是有些情况下 data_NEVER_USE 不是有效指针但也不为空。我能找到的关于一个类是否被初始化的唯一指标是在 id->data_NEVER_USE->flags & RW_REALIZED 中。我在这里缺少类初始化的哪个方面可以让我跳过这种情况?

b objc_msgSend
c
commands
silent

if (*$r0 == 0)
    continue
end

set $id = (class_t *)$r0
set $sel = $r1      
print *$id  
if($id->data_NEVER_USE != 0)
    set $data = (class_ro_t *) ($id->data_NEVER_USE)
    if (($data->flags & 0x80000000) && ($data->name))
        set $classname = $data->name
        printf "[%s ", $classname
    else
        continue
    end
end

if ($sel != 0)
    printf "%s", $sel
else
    printf "null"
end

printf "]\n"
continue
end

我很感激这方面的任何帮助。谢谢。

最佳答案

这两种方法对我来说效果相当好。请注意,在我的示例中,我手动启动“SomeApp”,以便在它启动后立即对其进行监控。

gdb
(gdb) attach --waitfor 'SomeApp'

**this is where you manually start SomeApp on your device**

call (void)instrumentObjcMessageSends(YES)

“instrumentObjcMessageSends”在运行时启用/禁用消息记录。这是一些 more information on this method.

另一个选项,仍然在你的 iDevice 上使用 GDB,是写一个像这样的小命令:

FooPad:~ root# gdb
(gdb) attach SBSettings
Attaching to process 440.
Reading symbols for shared libraries . done
Reading symbols for shared libraries ............................. done
0x35686004 in mach_msg_trap ()

(gdb) break objc_msgSend
Breakpoint 1 at 0x3323ef72

(gdb) commands
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".

>printf "-[%s %s]\n", (char *)class_getName(*(long *)$r0,$r1),$r1
>c
>end

(gdb) c
Continuing.
// a ton of information will follow

只要您按下 “c”(就在“Continuing.”行的正上方,您的屏幕就会充满函数名称和参数。

最后关注these instructions在您的 iDevice 上获得一个工作的 GDB。为了后代,我将在此处发布简短说明:

GNU Debugger (gdb) is used to analyze the run time behavior of an iOS application. In recent iOS versions, GNU Debugger directly downloaded from the Cydia is broken and not functioning properly. Following the Pod 2g blog post also did not help me.

To get rid of this problem, add http://cydia.radare.org to cydia source and download the latest GNU Debugger (build 1708). GDB build 1708 is working for iOS 5.x.

关于objective-c - 用于跟踪 iOS 设备上的 Objective-C 调用的 GDB 脚本 - 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12733471/

相关文章:

objective-c - 可拖动的 NSView

ios - 在不旋转 View 本身的情况下围绕中心点旋转 UIView

iphone - 在 Xcode 4 中将静态库链接到 iOS 项目

ios - 自定义 UITableViewCell - 我设置了背景图像,但如何设置边框颜色?

c++ - 如何在 GDB 中漂亮地打印 STL 容器?

iphone - 了解 NSDictionary 版本的内存管理

ios - TableView 单元格中的 NSAttributedString - 有时显示为纯文本

IOS 流套接字

python - 调试 Cython 时打印变量时出错

c++ - 如何确定哪些库是动态加载的?