2014-08-28 65 views
0

我目前正在嘗試從外部進程獲得基地址 在Xcode中使用C++!
這是我走到這一步:all_image_infos通過在OS X上的pid C++

if (task_info(this->_pmach_port, TASK_DYLD_INFO, (task_info_t)&dyld_info, &count) == KERN_SUCCESS) 
{ 
    this->Read(this->dyld_info.all_image_info_addr, sizeof(dyld_all_image_infos), &this->all_image_infos); 
    printf("Got Task info!\nall_image offset: 0x%llx\ninfo array count: %i",this->dyld_info.all_image_info_addr,this->all_image_infos.infoArrayCount); 
    printf("Version: %i\n",this->all_image_infos.version); 

    for(int i=0;i< this->all_image_infos.infoArrayCount;i++) { 
     printf("image: %s %d\n", 
       this->all_image_infos.infoArray[i].imageFilePath, 
       this->all_image_infos.infoArray[i].imageLoadAddress 
       ); 
     } 
} 

是沒有問題的,我的輸出如下:

Process To open: hl2_osx 
    Got Task info! 
    all_image offset: 0x8feb052c 
    info array count: 303 Version: 14 

我的主要問題是,Xcode的停在那裏我要輸出的線我的模塊的相關信息與 原因:

EXC_BAD_ACCESS(代碼= EXC_I368_GPFLT)

我做錯了什麼?

因爲我剛剛從Windows上使用WINAPI功能上寫上一個Mac程序切換,

我希望有人能幫幫我!

+0

我認爲'imageFilePath'不是空結尾的字符串。 – pqnet 2014-08-28 18:06:56

+0

@pqnet但是,我的信息數組也是空的。 – 2014-08-28 18:47:09

回答

1

對於結構中的任何指針,不能直接訪問指向的數據。您必須從另一個進程讀取它,就像讀取all_image_infos結構一樣。 info_array指針有這個問題。其中的imageFilePath也是如此。等


struct dyld_image_info *infoArray; 
size_t size = sizeof(*infoArray) * this->all_image_infos.infoArrayCount; 
infoArray = malloc(size); 
this->Read(this->all_image_infos.infoArray, size, infoArray); 
for(int i=0;i< this->all_image_infos.infoArrayCount;i++) { 
    char path[PATH_MAX]; 
    this->Read(infoArray[i].imageFilePath, sizeof(path), path); 
    path[sizeof(path) - 1] = 0; 
    // Alternatively, you could use memchr() to see if path is null-terminated. If not, print what you have and read more, in a loop. 
    printf("image: %s %d\n", 
      path, 
      infoArray[i].imageLoadAddress 
      ); 
} 
+0

是的,所以我需要像all_image_infos結構一樣閱讀它。 但是如何? 什麼是我的信息數組的抵消? 我很困惑。 – 2014-08-28 19:22:10

+0

檢查我更新的答案。基本上,從另一個進程複製的結構體中的任何「指針」都只是一個用於讀取該進程的「地址」。 – 2014-08-28 22:41:13

+0

好,謝謝你的回答!無論如何,這不是它應該的工作。因爲我需要得到 模塊名稱和基地址,我的輸出有點令人毛骨悚然。 好吧,它是這樣的: image:\ 330 \ 345 \ 303_ \ 377 0 image:\ 330 \ 345 \ 303_ \ 377 0 image:\ 330 \ 345 \ 303_ \ 377 0 image:\ 330 \ 345 \ 303_ \ 377 0 image:\ 330 \ 345 \ 303_ \ 377 0 image:\ 330 \ 345 \ 303_ \ 377 0 image:\ 330 \ 345 \ 303_ \ 377 0 – 2014-08-29 12:02:04