Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ int dummy_dyld_symbol = 1; #include <stdio.h> #include <stdlib.h> // The following API's are deprecated. // In Mac OS X 10.4 we only do a minimal implementation of these API's // to keep from breaking existing clients (Omni and MS Crash Reporters). // // This minmal implementation only allows inspection of one process and // only reveals the current images in that process (no notification of // later image changes). It assumes both processes use the same dyld // and dyld has not slid in either process. // #if __ppc__ #include "mach-o/dyld_debug.h" #include "mach-o/dyld_gdb.h" // global state set up by _dyld_debug_subscribe_to_events() and accessed by _dyld_debug_module_name() static const struct dyld_image_info* sImages = NULL; static uint32_t sImagesCount = 0; static task_port_t sImagesTaskPort = 0; // reads an address range out of another process // returns a malloc'ed block that caller should free static void* xprocess_read(task_port_t target_task, const void* address, size_t len) { void* result = NULL; mach_vm_address_t page_address = (uint32_t)address & (-4096); mach_vm_address_t last_page_address = ((uint32_t)address + len + 4095) & (-4096); mach_vm_size_t page_size = last_page_address - page_address; uint8_t* local_start; uint32_t local_len; kern_return_t r = vm_read( target_task, page_address, page_size, (vm_offset_t*)&local_start, &local_len); if ( r == KERN_SUCCESS ) { result = malloc(len); if ( result != NULL ) memcpy(result, &local_start[(uint32_t)address - page_address], len); vm_deallocate(mach_task_self(), (uintptr_t)local_start, local_len); } return result; } // reads a c-string out of another process. The returned string should be vm_deallocated. // All strings must be less than 1 to 2 pages long static const char* xprocess_read_string(task_port_t target_task, const void* address) { char* result = NULL; mach_vm_address_t page_address = (uint32_t)address & (-4096); mach_vm_size_t page_size = 0x2000; // always read two pages uint8_t* local_start; uint32_t local_len; kern_return_t r = vm_read( target_task, page_address, page_size, (vm_offset_t*)&local_start, &local_len); if ( r == KERN_SUCCESS ) { const char* str = (char*)&local_start[(uint32_t)address - page_address]; return str; } return result; } // SPI into dyld to get address of _dyld_get_all_image_infos data structure static const struct dyld_all_image_infos* dyld_get_all_image_infos() { static const struct dyld_all_image_infos* (*p)() = NULL; if ( p == NULL ) _dyld_func_lookup("__dyld_get_all_image_infos", (void**)&p); if ( p != NULL ) return p(); else return NULL; } /* * _dyld_debug_module_name() is passed a dyld_debug_module struct and * sets image_name and module_name as well as the nameCnts. If the module * does not refer to a valid module DYLD_INVALID_ARGUMENTS is returned. */ enum dyld_debug_return _dyld_debug_module_name( task_port_t target_task, unsigned long send_timeout, unsigned long rcv_timeout, boolean_t inconsistent_data_ok, struct dyld_debug_module module, char **image_name, unsigned long *image_nameCnt, char **module_name, unsigned long *module_nameCnt) { // examine sImage* info set up by _dyld_debug_subscribe_to_events() if ( sImagesTaskPort == target_task ) { unsigned int i; for (i=0; i < sImagesCount; ++i) { if ( module.header == sImages[i].imageLoadAddress ) { // copy requested string const char* path = xprocess_read_string(sImagesTaskPort, sImages[i].imageFilePath); if ( path != NULL ) { *image_name = (char*)path; *image_nameCnt = strlen(path); *module_name = NULL; *module_nameCnt = 0; return DYLD_SUCCESS; } } } } // not supported return DYLD_INVALID_ARGUMENTS; } /* * set_dyld_debug_error_func() is called to register a function to be called * when error occurs in the dyld debug API's. */ void _dyld_debug_set_error_func( void (*func)(struct dyld_debug_error_data *e)) { // do nothing } // Examine a mach_header in another process and determine its slid static ptrdiff_t slideForHeader(task_port_t target_task, const struct mach_header* otherAddressHeader) { const struct mach_header* mh = xprocess_read(target_task, otherAddressHeader, 0x2000); if ( mh != NULL ) { int i; const struct segment_command *sgp = (const struct segment_command *)((char*)mh + sizeof(mh)); for (i = 0; i < mh->ncmds; i++){ if (sgp->cmd == LC_SEGMENT) { if (sgp->fileoff == 0 && sgp->filesize != 0) { return (uintptr_t)mh - (uintptr_t)sgp->vmaddr; } } sgp = (const struct segment_command *)((char *)sgp + sgp->cmdsize); } free((void*)mh); } return 0; } /* * _dyld_debug_subscribe_to_events creates a new thread that is will call the * specified dyld_event_routine when dynamic link events occur in the target * task. This uses _dyld_debug_add_event_subscriber() and is just a different * interface to get events. */ enum dyld_debug_return _dyld_debug_subscribe_to_events( task_port_t target_task, unsigned long send_timeout, unsigned long rcv_timeout, boolean_t inconsistent_data_ok, void (*dyld_event_routine)(struct dyld_event event)) { sImages = NULL; sImagesCount = 0; sImagesTaskPort = 0; // get location of dyld_get_all_image_infos in this process // It is possible that dyld slid in one of the processes, in which case this fails const struct dyld_all_image_infos* infoOtherAddressSpace = dyld_get_all_image_infos(); if ( infoOtherAddressSpace != NULL ) { const struct dyld_all_image_infos* infos; infos = (const struct dyld_all_image_infos*)xprocess_read(target_task, infoOtherAddressSpace, sizeof(struct dyld_all_image_infos)); if ( infos != NULL ) { // sanity check version if ( infos->version == 1 ) { sImages = xprocess_read(target_task, infos->infoArray, infos->infoArrayCount * sizeof(struct dyld_image_info)); if ( sImages != NULL ) { int i; // save info info into sImage* globals for use by later calls to _dyld_debug_module_name() sImagesCount = infos->infoArrayCount; sImagesTaskPort = target_task; // tell caller about every image for (i=0; i < infos->infoArrayCount; ++i) { struct dyld_event addEvent; bzero(&addEvent, sizeof(struct dyld_event)); const struct mach_header* mh = sImages[i].imageLoadAddress; addEvent.type = DYLD_IMAGE_ADDED; addEvent.arg[0].header = (struct mach_header*)mh; addEvent.arg[0].vmaddr_slide = slideForHeader(target_task, mh); addEvent.arg[0].module_index = 0; (*dyld_event_routine)(addEvent); } } } // we free the dyld_all_image_infos struct, but not the array we copied // The array is left in sImage* for use by later calls to _dyld_debug_module_name() free((void*)infos); } } // tell client event handler no more images struct dyld_event event; event.type = DYLD_PAST_EVENTS_END; (*dyld_event_routine)(event); return DYLD_SUCCESS; } #endif |