检索由 MacOS 上的进程加载的动态库的文件路径

Retrieving the file path of a dynamic library loaded by a process on MacOS

目标:

我想获取进程加载的动态库的文件路径。

我的代码:

struct task_dyld_info dyld_info;
mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
struct dyld_image_info dyld_image_info;
char path[PATH_MAX];

if (task_info(task, TASK_DYLD_INFO, (task_info_t) & dyld_info, & count) == KERN_SUCCESS) {
  printf("dyld_all_image_infos: %p\n", dyld_info.all_image_info_addr);
  printf("%d\n", dyld_info.all_image_info_size);

  struct dyld_all_image_infos * dyld_all_image_infos = (struct dyld_all_image_infos * ) dyld_info.all_image_info_addr;

  for (int i = 0; i < dyld_all_image_infos -> infoArrayCount; i++) {
    dyld_image_info = dyld_all_image_infos -> infoArray[i];
    printf("%s\n", dyld_image_info.imageFilePath);
    if (dyld_image_info.imageFilePath != NULL) {
      if (strstr(dyld_image_info.imageFilePath, "GameCore_XP2.dll") != NULL) {
        printf("Found GameCore_XP2.dll\n");
        printf("%s\n", dyld_image_info.imageFilePath);
        printf("%p\n", dyld_image_info.imageLoadAddress);
      }
    }
  }
}

不幸的是 Xcode 在运行时给我以下错误: “线程 1:EXC_BAD_ACCESS(代码=1,地址=0x4002e2804002d54)”

根据 https://gist.github.com/xcxcxc/989018646b1f0f2f31f0873a32c4a658,您需要使用 vm_read 来获取该数据。