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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <libproc.h> #include <mach/mach_init.h> #include <mach/mach_types.h> #include <System/sys/proc.h> #include <uuid/uuid.h> #include <mach-o/dyld_introspection.h> static void usage() { fprintf(stderr, "Usage: dyld_inspect <options>* [ -p pid | -all | -all_installed_caches ]\n" "\t-shared_cache_uuid print shared cache UUID\n" "\t-shared_cache_address print shared cache base address\n" //"\t-shared_cache_path print shared cache path\n" "\t-shared_cache print all shared cache options\n" //FIXME: Keep hidden for now, make public once we settle on the output format // "\t-images print all images loaded in the process\n" ); } static void printAllInstalledCaches(bool printSharedCacheUUID, bool printSharedCacheAddress, bool printImages) { dyld_for_each_installed_shared_cache(^(dyld_shared_cache_t cache) { // Get the path from the first file __block const char* cachePath = nullptr; dyld_shared_cache_for_each_file(cache, ^(const char *file_path) { if ( !cachePath ) cachePath = file_path; }); // Get the shared cache data uint64_t cacheBaseAddress = dyld_shared_cache_get_base_address(cache); uuid_t cacheUUID; dyld_shared_cache_copy_uuid(cache, &cacheUUID); bool printSeparator = false; { if ( printSeparator ) printf(" "); printSeparator = true; printf("%s", cachePath); } if ( printSharedCacheUUID ) { if ( printSeparator ) printf(" "); printSeparator = true; uuid_string_t uuidString; uuid_unparse_upper(cacheUUID, uuidString); printf("%s", uuidString); } if ( printSharedCacheAddress ) { if ( printSeparator ) printf(" "); printSeparator = true; printf("0x%08llx", cacheBaseAddress); } printf("\n"); if (printImages) { dyld_shared_cache_for_each_image(cache, ^(dyld_image_t image) { printf(" "); uuid_t imageUUID; uuid_clear(imageUUID); dyld_image_copy_uuid(image, &imageUUID); uuid_string_t uuidString; uuid_unparse_upper(imageUUID, uuidString); printf("%s", uuidString); const char* installname = dyld_image_get_installname(image); const char* file_path = dyld_image_get_file_path(image); if (file_path) { printf("%s\n", file_path); } else if (installname) { printf("%s\n", installname); } dyld_image_for_each_segment_info(image, ^(const char *segmentName, uint64_t vmAddr, uint64_t vmSize, int perm) { printf(" %16s 0x%08llx-0x%08llx\n", segmentName, vmAddr, vmAddr+vmSize); }); }); } }); } int main(int argc, const char* argv[], const char* envp[], const char* apple[]) { if ( argc == 1 ) { usage(); return 1; } bool allProcesses = false; int specificProcessPID = 0; bool allInstalledCaches = false; bool printSharedCacheUUID = false; bool printSharedCacheAddress = false; bool printImages = false; //bool printSharedCachePath = false; bool gotOption = false; for (int i=1; i < argc; ++i) { const char* arg = argv[i]; if ( strcmp(arg, "-shared_cache_uuid") == 0 ) { printSharedCacheUUID = true; gotOption = true; } else if ( strcmp(arg, "-shared_cache_address") == 0 ) { printSharedCacheAddress = true; gotOption = true; } // else if ( strcmp(arg, "-shared-cache-path") == 0 ) { // printSharedCachePath = true; // gotOption = true; // } else if ( strcmp(arg, "-shared_cache") == 0 ) { printSharedCacheUUID = true; printSharedCacheAddress = true; // printSharedCachePath = true; gotOption = true; } else if ( strcmp(arg, "-images") == 0 ) { printImages = true; // printSharedCachePath = true; gotOption = true; } else if ( strcmp(arg, "-p") == 0 ) { if ( ++i < argc ) { specificProcessPID = atoi(argv[i]); } else { fprintf(stderr, "-p missing process PID"); return 1; } } else if ( strcmp(arg, "-all") == 0 ) { allProcesses = true; } else if ( strcmp(arg, "-all_installed_caches") == 0 ) { allInstalledCaches = true; } else if ( strcmp(arg, "-help") == 0 ) { usage(); return 0; } else { fprintf(stderr, "unknown option: %s\n", argv[i]); usage(); return 1; } } if ( !allProcesses && (specificProcessPID == 0) && !allInstalledCaches ) { fprintf(stderr, "expected -p PID, -all, or -all_installed_caches flag\n"); return 1; } if ( !gotOption ) { fprintf(stderr, "expected print option\n"); usage(); return 1; } auto handleProcess = ^(int pid, bool exitOnError) { task_t task; if (task_read_for_pid(mach_task_self(), pid, &task) != KERN_SUCCESS) { if ( exitOnError ) { fprintf(stderr, "task_read_for_pid(%d) failed due to: %s\n", pid, strerror(errno)); if ( geteuid() != 0 ) { fprintf(stderr, "note: you may want try again as root\n"); } exit(1); } return; } kern_return_t kr = KERN_SUCCESS; dyld_process_t dyldProcess = dyld_process_create_for_task(task, &kr); if ( !dyldProcess ) { if ( exitOnError ) { fprintf(stderr, "dyld_process_create_for_task(pid = %d) failed due to: %d\n", pid, kr); exit(1); } return; } dyld_process_snapshot_t dyldSnapshot = dyld_process_snapshot_create_for_process(dyldProcess, &kr); if ( !dyldSnapshot ) { if ( exitOnError ) { fprintf(stderr, "dyld_process_snapshot_create_for_process(pid = %d) failed due to: %d\n", pid, kr); exit(1); } return; } uint64_t cacheBaseAddress = 0; uuid_t cacheUUID; uuid_clear(cacheUUID); // Get the shared cache data dyld_shared_cache_t dyldSharedCache = dyld_process_snapshot_get_shared_cache(dyldSnapshot); if ( dyldSharedCache ) { cacheBaseAddress = dyld_shared_cache_get_base_address(dyldSharedCache); dyld_shared_cache_copy_uuid(dyldSharedCache, &cacheUUID); } bool printSeparator = false; if ( allProcesses ) { if ( printSeparator ) printf(" "); printSeparator = true; printf("% 6d", pid); } if ( printSharedCacheUUID ) { if ( printSeparator ) printf(" "); printSeparator = true; uuid_string_t uuidString; uuid_unparse_upper(cacheUUID, uuidString); printf("%s", uuidString); } if ( printSharedCacheAddress ) { if ( printSeparator ) printf(" "); printSeparator = true; printf("0x%08llx", cacheBaseAddress); } printf("\n"); if (printImages) { dyld_process_snapshot_for_each_image(dyldSnapshot, ^(dyld_image_t image) { if ( allProcesses ) { printf(" "); } uuid_t imageUUID; uuid_clear(imageUUID); dyld_image_copy_uuid(image, &imageUUID); uuid_string_t uuidString; uuid_unparse_upper(imageUUID, uuidString); printf("%s", uuidString); const char* installname = dyld_image_get_installname(image); const char* file_path = dyld_image_get_file_path(image); if (file_path) { printf("%s\n", file_path); } else if (installname) { printf("%s\n", installname); } dyld_image_for_each_segment_info(image, ^(const char *segmentName, uint64_t vmAddr, uint64_t vmSize, int perm) { if ( allProcesses ) { printf(" %16s 0x%08llx-0x%08llx\n", segmentName, vmAddr, vmAddr+vmSize); } else { printf(" %16s 0x%08llx-0x%08llx\n", segmentName, vmAddr, vmAddr+vmSize); } }); }); } // All done. Free the data structures dyld_process_snapshot_dispose(dyldSnapshot); dyld_process_dispose(dyldProcess); }; if ( allProcesses ) { pid_t pids[2048]; int pidcount = proc_listallpids(pids, sizeof(pids)); if ( pidcount == -1 ) { fprintf(stderr, "failed to get list of processes due to: %s\n", strerror(errno)); return 1; } for ( int i=0; i < pidcount; ++i ) { handleProcess(pids[i], false); } } else if ( specificProcessPID != 0 ) { handleProcess(specificProcessPID, true); } else { // printing all installed caches printAllInstalledCaches(printSharedCacheUUID, printSharedCacheAddress, printImages); } return 0; } |