1 //===-- DNB.cpp -------------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Created by Greg Clayton on 3/23/07. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "DNB.h" 15 #include <inttypes.h> 16 #include <signal.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <sys/resource.h> 20 #include <sys/stat.h> 21 #include <sys/types.h> 22 #include <sys/wait.h> 23 #include <unistd.h> 24 #include <sys/sysctl.h> 25 #include <map> 26 #include <vector> 27 #include <libproc.h> 28 29 #define TRY_KQUEUE 1 30 31 #ifdef TRY_KQUEUE 32 #include <sys/event.h> 33 #include <sys/time.h> 34 #ifdef NOTE_EXIT_DETAIL 35 #define USE_KQUEUE 36 #endif 37 #endif 38 39 #include "MacOSX/MachProcess.h" 40 #include "MacOSX/MachTask.h" 41 #include "CFString.h" 42 #include "DNBLog.h" 43 #include "DNBDataRef.h" 44 #include "DNBThreadResumeActions.h" 45 #include "DNBTimer.h" 46 #include "CFBundle.h" 47 48 49 typedef std::shared_ptr<MachProcess> MachProcessSP; 50 typedef std::map<nub_process_t, MachProcessSP> ProcessMap; 51 typedef ProcessMap::iterator ProcessMapIter; 52 typedef ProcessMap::const_iterator ProcessMapConstIter; 53 54 size_t GetAllInfos (std::vector<struct kinfo_proc>& proc_infos); 55 static size_t GetAllInfosMatchingName (const char *process_name, std::vector<struct kinfo_proc>& matching_proc_infos); 56 57 //---------------------------------------------------------------------- 58 // A Thread safe singleton to get a process map pointer. 59 // 60 // Returns a pointer to the existing process map, or a pointer to a 61 // newly created process map if CAN_CREATE is non-zero. 62 //---------------------------------------------------------------------- 63 static ProcessMap* 64 GetProcessMap(bool can_create) 65 { 66 static ProcessMap* g_process_map_ptr = NULL; 67 68 if (can_create && g_process_map_ptr == NULL) 69 { 70 static pthread_mutex_t g_process_map_mutex = PTHREAD_MUTEX_INITIALIZER; 71 PTHREAD_MUTEX_LOCKER (locker, &g_process_map_mutex); 72 if (g_process_map_ptr == NULL) 73 g_process_map_ptr = new ProcessMap; 74 } 75 return g_process_map_ptr; 76 } 77 78 //---------------------------------------------------------------------- 79 // Add PID to the shared process pointer map. 80 // 81 // Return non-zero value if we succeed in adding the process to the map. 82 // The only time this should fail is if we run out of memory and can't 83 // allocate a ProcessMap. 84 //---------------------------------------------------------------------- 85 static nub_bool_t 86 AddProcessToMap (nub_process_t pid, MachProcessSP& procSP) 87 { 88 ProcessMap* process_map = GetProcessMap(true); 89 if (process_map) 90 { 91 process_map->insert(std::make_pair(pid, procSP)); 92 return true; 93 } 94 return false; 95 } 96 97 //---------------------------------------------------------------------- 98 // Remove the shared pointer for PID from the process map. 99 // 100 // Returns the number of items removed from the process map. 101 //---------------------------------------------------------------------- 102 static size_t 103 RemoveProcessFromMap (nub_process_t pid) 104 { 105 ProcessMap* process_map = GetProcessMap(false); 106 if (process_map) 107 { 108 return process_map->erase(pid); 109 } 110 return 0; 111 } 112 113 //---------------------------------------------------------------------- 114 // Get the shared pointer for PID from the existing process map. 115 // 116 // Returns true if we successfully find a shared pointer to a 117 // MachProcess object. 118 //---------------------------------------------------------------------- 119 static nub_bool_t 120 GetProcessSP (nub_process_t pid, MachProcessSP& procSP) 121 { 122 ProcessMap* process_map = GetProcessMap(false); 123 if (process_map != NULL) 124 { 125 ProcessMapIter pos = process_map->find(pid); 126 if (pos != process_map->end()) 127 { 128 procSP = pos->second; 129 return true; 130 } 131 } 132 procSP.reset(); 133 return false; 134 } 135 136 #ifdef USE_KQUEUE 137 void * 138 kqueue_thread (void *arg) 139 { 140 int kq_id = (int) (intptr_t) arg; 141 142 struct kevent death_event; 143 while (1) 144 { 145 int n_events = kevent (kq_id, NULL, 0, &death_event, 1, NULL); 146 if (n_events == -1) 147 { 148 if (errno == EINTR) 149 continue; 150 else 151 { 152 DNBLogError ("kqueue failed with error: (%d): %s", errno, strerror(errno)); 153 return NULL; 154 } 155 } 156 else if (death_event.flags & EV_ERROR) 157 { 158 int error_no = death_event.data; 159 const char *error_str = strerror(death_event.data); 160 if (error_str == NULL) 161 error_str = "Unknown error"; 162 DNBLogError ("Failed to initialize kqueue event: (%d): %s", error_no, error_str ); 163 return NULL; 164 } 165 else 166 { 167 int status; 168 pid_t child_pid = waitpid ((pid_t) death_event.ident, &status, 0); 169 if (death_event.data & NOTE_EXIT_MEMORY) 170 { 171 if (death_event.data & NOTE_VM_PRESSURE) 172 DNBProcessSetExitInfo (child_pid, "Terminated due to Memory Pressure"); 173 else if (death_event.data & NOTE_VM_ERROR) 174 DNBProcessSetExitInfo (child_pid, "Terminated due to Memory Error"); 175 else 176 DNBProcessSetExitInfo (child_pid, "Terminated due to unknown Memory condition"); 177 } 178 else if (death_event.data & NOTE_EXIT_DECRYPTFAIL) 179 DNBProcessSetExitInfo (child_pid, "Terminated due to decrypt failure"); 180 else if (death_event.data & NOTE_EXIT_CSERROR) 181 DNBProcessSetExitInfo (child_pid, "Terminated due to code signing error"); 182 183 DNBLogThreadedIf(LOG_PROCESS, "waitpid_process_thread (): setting exit status for pid = %i to %i", child_pid, status); 184 DNBProcessSetExitStatus (child_pid, status); 185 return NULL; 186 } 187 } 188 } 189 190 static bool 191 spawn_kqueue_thread (pid_t pid) 192 { 193 pthread_t thread; 194 int kq_id; 195 196 kq_id = kqueue(); 197 if (kq_id == -1) 198 { 199 DNBLogError ("Could not get kqueue for pid = %i.", pid); 200 return false; 201 } 202 203 struct kevent reg_event; 204 205 EV_SET(®_event, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT|NOTE_EXIT_DETAIL, 0, NULL); 206 // Register the event: 207 int result = kevent (kq_id, ®_event, 1, NULL, 0, NULL); 208 if (result != 0) 209 { 210 DNBLogError ("Failed to register kqueue NOTE_EXIT event for pid %i, error: %d.", pid, result); 211 return false; 212 } 213 214 int ret = ::pthread_create (&thread, NULL, kqueue_thread, (void *)(intptr_t)kq_id); 215 216 // pthread_create returns 0 if successful 217 if (ret == 0) 218 { 219 ::pthread_detach (thread); 220 return true; 221 } 222 return false; 223 } 224 #endif // #if USE_KQUEUE 225 226 static void * 227 waitpid_thread (void *arg) 228 { 229 const pid_t pid = (pid_t)(intptr_t)arg; 230 int status; 231 while (1) 232 { 233 pid_t child_pid = waitpid(pid, &status, 0); 234 DNBLogThreadedIf(LOG_PROCESS, "waitpid_thread (): waitpid (pid = %i, &status, 0) => %i, status = %i, errno = %i", pid, child_pid, status, errno); 235 236 if (child_pid < 0) 237 { 238 if (errno == EINTR) 239 continue; 240 break; 241 } 242 else 243 { 244 if (WIFSTOPPED(status)) 245 { 246 continue; 247 } 248 else// if (WIFEXITED(status) || WIFSIGNALED(status)) 249 { 250 DNBLogThreadedIf(LOG_PROCESS, "waitpid_thread (): setting exit status for pid = %i to %i", child_pid, status); 251 DNBProcessSetExitStatus (child_pid, status); 252 return NULL; 253 } 254 } 255 } 256 257 // We should never exit as long as our child process is alive, so if we 258 // do something else went wrong and we should exit... 259 DNBLogThreadedIf(LOG_PROCESS, "waitpid_thread (): main loop exited, setting exit status to an invalid value (-1) for pid %i", pid); 260 DNBProcessSetExitStatus (pid, -1); 261 return NULL; 262 } 263 static bool 264 spawn_waitpid_thread (pid_t pid) 265 { 266 #ifdef USE_KQUEUE 267 bool success = spawn_kqueue_thread (pid); 268 if (success) 269 return true; 270 #endif 271 272 pthread_t thread; 273 int ret = ::pthread_create (&thread, NULL, waitpid_thread, (void *)(intptr_t)pid); 274 // pthread_create returns 0 if successful 275 if (ret == 0) 276 { 277 ::pthread_detach (thread); 278 return true; 279 } 280 return false; 281 } 282 283 nub_process_t 284 DNBProcessLaunch (const char *path, 285 char const *argv[], 286 const char *envp[], 287 const char *working_directory, // NULL => dont' change, non-NULL => set working directory for inferior to this 288 const char *stdin_path, 289 const char *stdout_path, 290 const char *stderr_path, 291 bool no_stdio, 292 nub_launch_flavor_t launch_flavor, 293 int disable_aslr, 294 const char *event_data, 295 char *err_str, 296 size_t err_len) 297 { 298 DNBLogThreadedIf(LOG_PROCESS, "%s ( path='%s', argv = %p, envp = %p, working_dir=%s, stdin=%s, stdout=%s, stderr=%s, no-stdio=%i, launch_flavor = %u, disable_aslr = %d, err = %p, err_len = %llu) called...", 299 __FUNCTION__, 300 path, 301 argv, 302 envp, 303 working_directory, 304 stdin_path, 305 stdout_path, 306 stderr_path, 307 no_stdio, 308 launch_flavor, 309 disable_aslr, 310 err_str, 311 (uint64_t)err_len); 312 313 if (err_str && err_len > 0) 314 err_str[0] = '\0'; 315 struct stat path_stat; 316 if (::stat(path, &path_stat) == -1) 317 { 318 char stat_error[256]; 319 ::strerror_r (errno, stat_error, sizeof(stat_error)); 320 snprintf(err_str, err_len, "%s (%s)", stat_error, path); 321 return INVALID_NUB_PROCESS; 322 } 323 324 MachProcessSP processSP (new MachProcess); 325 if (processSP.get()) 326 { 327 DNBError launch_err; 328 pid_t pid = processSP->LaunchForDebug (path, 329 argv, 330 envp, 331 working_directory, 332 stdin_path, 333 stdout_path, 334 stderr_path, 335 no_stdio, 336 launch_flavor, 337 disable_aslr, 338 event_data, 339 launch_err); 340 if (err_str) 341 { 342 *err_str = '\0'; 343 if (launch_err.Fail()) 344 { 345 const char *launch_err_str = launch_err.AsString(); 346 if (launch_err_str) 347 { 348 strncpy(err_str, launch_err_str, err_len-1); 349 err_str[err_len-1] = '\0'; // Make sure the error string is terminated 350 } 351 } 352 } 353 354 DNBLogThreadedIf(LOG_PROCESS, "(DebugNub) new pid is %d...", pid); 355 356 if (pid != INVALID_NUB_PROCESS) 357 { 358 // Spawn a thread to reap our child inferior process... 359 spawn_waitpid_thread (pid); 360 361 if (processSP->Task().TaskPortForProcessID (launch_err) == TASK_NULL) 362 { 363 // We failed to get the task for our process ID which is bad. 364 // Kill our process otherwise it will be stopped at the entry 365 // point and get reparented to someone else and never go away. 366 DNBLog ("Could not get task port for process, sending SIGKILL and exiting."); 367 kill (SIGKILL, pid); 368 369 if (err_str && err_len > 0) 370 { 371 if (launch_err.AsString()) 372 { 373 ::snprintf (err_str, err_len, "failed to get the task for process %i (%s)", pid, launch_err.AsString()); 374 } 375 else 376 { 377 ::snprintf (err_str, err_len, "failed to get the task for process %i", pid); 378 } 379 } 380 } 381 else 382 { 383 bool res = AddProcessToMap(pid, processSP); 384 assert(res && "Couldn't add process to map!"); 385 return pid; 386 } 387 } 388 } 389 return INVALID_NUB_PROCESS; 390 } 391 392 nub_process_t 393 DNBProcessAttachByName (const char *name, struct timespec *timeout, char *err_str, size_t err_len) 394 { 395 if (err_str && err_len > 0) 396 err_str[0] = '\0'; 397 std::vector<struct kinfo_proc> matching_proc_infos; 398 size_t num_matching_proc_infos = GetAllInfosMatchingName(name, matching_proc_infos); 399 if (num_matching_proc_infos == 0) 400 { 401 DNBLogError ("error: no processes match '%s'\n", name); 402 return INVALID_NUB_PROCESS; 403 } 404 else if (num_matching_proc_infos > 1) 405 { 406 DNBLogError ("error: %llu processes match '%s':\n", (uint64_t)num_matching_proc_infos, name); 407 size_t i; 408 for (i=0; i<num_matching_proc_infos; ++i) 409 DNBLogError ("%6u - %s\n", matching_proc_infos[i].kp_proc.p_pid, matching_proc_infos[i].kp_proc.p_comm); 410 return INVALID_NUB_PROCESS; 411 } 412 413 return DNBProcessAttach (matching_proc_infos[0].kp_proc.p_pid, timeout, err_str, err_len); 414 } 415 416 nub_process_t 417 DNBProcessAttach (nub_process_t attach_pid, struct timespec *timeout, char *err_str, size_t err_len) 418 { 419 if (err_str && err_len > 0) 420 err_str[0] = '\0'; 421 422 pid_t pid = INVALID_NUB_PROCESS; 423 MachProcessSP processSP(new MachProcess); 424 if (processSP.get()) 425 { 426 DNBLogThreadedIf(LOG_PROCESS, "(DebugNub) attaching to pid %d...", attach_pid); 427 pid = processSP->AttachForDebug (attach_pid, err_str, err_len); 428 429 if (pid != INVALID_NUB_PROCESS) 430 { 431 bool res = AddProcessToMap(pid, processSP); 432 assert(res && "Couldn't add process to map!"); 433 spawn_waitpid_thread(pid); 434 } 435 } 436 437 while (pid != INVALID_NUB_PROCESS) 438 { 439 // Wait for process to start up and hit entry point 440 DNBLogThreadedIf (LOG_PROCESS, 441 "%s DNBProcessWaitForEvent (%4.4x, eEventProcessRunningStateChanged | eEventProcessStoppedStateChanged, true, INFINITE)...", 442 __FUNCTION__, 443 pid); 444 nub_event_t set_events = DNBProcessWaitForEvents (pid, 445 eEventProcessRunningStateChanged | eEventProcessStoppedStateChanged, 446 true, 447 timeout); 448 449 DNBLogThreadedIf (LOG_PROCESS, 450 "%s DNBProcessWaitForEvent (%4.4x, eEventProcessRunningStateChanged | eEventProcessStoppedStateChanged, true, INFINITE) => 0x%8.8x", 451 __FUNCTION__, 452 pid, 453 set_events); 454 455 if (set_events == 0) 456 { 457 if (err_str && err_len > 0) 458 snprintf(err_str, err_len, "operation timed out"); 459 pid = INVALID_NUB_PROCESS; 460 } 461 else 462 { 463 if (set_events & (eEventProcessRunningStateChanged | eEventProcessStoppedStateChanged)) 464 { 465 nub_state_t pid_state = DNBProcessGetState (pid); 466 DNBLogThreadedIf (LOG_PROCESS, "%s process %4.4x state changed (eEventProcessStateChanged): %s", 467 __FUNCTION__, pid, DNBStateAsString(pid_state)); 468 469 switch (pid_state) 470 { 471 default: 472 case eStateInvalid: 473 case eStateUnloaded: 474 case eStateAttaching: 475 case eStateLaunching: 476 case eStateSuspended: 477 break; // Ignore 478 479 case eStateRunning: 480 case eStateStepping: 481 // Still waiting to stop at entry point... 482 break; 483 484 case eStateStopped: 485 case eStateCrashed: 486 return pid; 487 488 case eStateDetached: 489 case eStateExited: 490 if (err_str && err_len > 0) 491 snprintf(err_str, err_len, "process exited"); 492 return INVALID_NUB_PROCESS; 493 } 494 } 495 496 DNBProcessResetEvents(pid, set_events); 497 } 498 } 499 500 return INVALID_NUB_PROCESS; 501 } 502 503 size_t 504 GetAllInfos (std::vector<struct kinfo_proc>& proc_infos) 505 { 506 size_t size = 0; 507 int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL }; 508 u_int namelen = sizeof(name)/sizeof(int); 509 int err; 510 511 // Try to find out how many processes are around so we can 512 // size the buffer appropriately. sysctl's man page specifically suggests 513 // this approach, and says it returns a bit larger size than needed to 514 // handle any new processes created between then and now. 515 516 err = ::sysctl (name, namelen, NULL, &size, NULL, 0); 517 518 if ((err < 0) && (err != ENOMEM)) 519 { 520 proc_infos.clear(); 521 perror("sysctl (mib, miblen, NULL, &num_processes, NULL, 0)"); 522 return 0; 523 } 524 525 526 // Increase the size of the buffer by a few processes in case more have 527 // been spawned 528 proc_infos.resize (size / sizeof(struct kinfo_proc)); 529 size = proc_infos.size() * sizeof(struct kinfo_proc); // Make sure we don't exceed our resize... 530 err = ::sysctl (name, namelen, &proc_infos[0], &size, NULL, 0); 531 if (err < 0) 532 { 533 proc_infos.clear(); 534 return 0; 535 } 536 537 // Trim down our array to fit what we actually got back 538 proc_infos.resize(size / sizeof(struct kinfo_proc)); 539 return proc_infos.size(); 540 } 541 542 static size_t 543 GetAllInfosMatchingName(const char *full_process_name, std::vector<struct kinfo_proc>& matching_proc_infos) 544 { 545 546 matching_proc_infos.clear(); 547 if (full_process_name && full_process_name[0]) 548 { 549 // We only get the process name, not the full path, from the proc_info. So just take the 550 // base name of the process name... 551 const char *process_name; 552 process_name = strrchr (full_process_name, '/'); 553 if (process_name == NULL) 554 process_name = full_process_name; 555 else 556 process_name++; 557 558 const int process_name_len = strlen(process_name); 559 std::vector<struct kinfo_proc> proc_infos; 560 const size_t num_proc_infos = GetAllInfos(proc_infos); 561 if (num_proc_infos > 0) 562 { 563 uint32_t i; 564 for (i=0; i<num_proc_infos; i++) 565 { 566 // Skip zombie processes and processes with unset status 567 if (proc_infos[i].kp_proc.p_stat == 0 || proc_infos[i].kp_proc.p_stat == SZOMB) 568 continue; 569 570 // Check for process by name. We only check the first MAXCOMLEN 571 // chars as that is all that kp_proc.p_comm holds. 572 573 if (::strncasecmp(process_name, proc_infos[i].kp_proc.p_comm, MAXCOMLEN) == 0) 574 { 575 if (process_name_len > MAXCOMLEN) 576 { 577 // We found a matching process name whose first MAXCOMLEN 578 // characters match, but there is more to the name than 579 // this. We need to get the full process name. Use proc_pidpath, which will get 580 // us the full path to the executed process. 581 582 char proc_path_buf[PATH_MAX]; 583 584 int return_val = proc_pidpath (proc_infos[i].kp_proc.p_pid, proc_path_buf, PATH_MAX); 585 if (return_val > 0) 586 { 587 // Okay, now search backwards from that to see if there is a 588 // slash in the name. Note, even though we got all the args we don't care 589 // because the list data is just a bunch of concatenated null terminated strings 590 // so strrchr will start from the end of argv0. 591 592 const char *argv_basename = strrchr(proc_path_buf, '/'); 593 if (argv_basename) 594 { 595 // Skip the '/' 596 ++argv_basename; 597 } 598 else 599 { 600 // We didn't find a directory delimiter in the process argv[0], just use what was in there 601 argv_basename = proc_path_buf; 602 } 603 604 if (argv_basename) 605 { 606 if (::strncasecmp(process_name, argv_basename, PATH_MAX) == 0) 607 { 608 matching_proc_infos.push_back(proc_infos[i]); 609 } 610 } 611 } 612 } 613 else 614 { 615 // We found a matching process, add it to our list 616 matching_proc_infos.push_back(proc_infos[i]); 617 } 618 } 619 } 620 } 621 } 622 // return the newly added matches. 623 return matching_proc_infos.size(); 624 } 625 626 nub_process_t 627 DNBProcessAttachWait (const char *waitfor_process_name, 628 nub_launch_flavor_t launch_flavor, 629 bool ignore_existing, 630 struct timespec *timeout_abstime, 631 useconds_t waitfor_interval, 632 char *err_str, 633 size_t err_len, 634 DNBShouldCancelCallback should_cancel_callback, 635 void *callback_data) 636 { 637 DNBError prepare_error; 638 std::vector<struct kinfo_proc> exclude_proc_infos; 639 size_t num_exclude_proc_infos; 640 641 // If the PrepareForAttach returns a valid token, use MachProcess to check 642 // for the process, otherwise scan the process table. 643 644 const void *attach_token = MachProcess::PrepareForAttach (waitfor_process_name, launch_flavor, true, prepare_error); 645 646 if (prepare_error.Fail()) 647 { 648 DNBLogError ("Error in PrepareForAttach: %s", prepare_error.AsString()); 649 return INVALID_NUB_PROCESS; 650 } 651 652 if (attach_token == NULL) 653 { 654 if (ignore_existing) 655 num_exclude_proc_infos = GetAllInfosMatchingName (waitfor_process_name, exclude_proc_infos); 656 else 657 num_exclude_proc_infos = 0; 658 } 659 660 DNBLogThreadedIf (LOG_PROCESS, "Waiting for '%s' to appear...\n", waitfor_process_name); 661 662 // Loop and try to find the process by name 663 nub_process_t waitfor_pid = INVALID_NUB_PROCESS; 664 665 while (waitfor_pid == INVALID_NUB_PROCESS) 666 { 667 if (attach_token != NULL) 668 { 669 nub_process_t pid; 670 pid = MachProcess::CheckForProcess(attach_token); 671 if (pid != INVALID_NUB_PROCESS) 672 { 673 waitfor_pid = pid; 674 break; 675 } 676 } 677 else 678 { 679 680 // Get the current process list, and check for matches that 681 // aren't in our original list. If anyone wants to attach 682 // to an existing process by name, they should do it with 683 // --attach=PROCNAME. Else we will wait for the first matching 684 // process that wasn't in our exclusion list. 685 std::vector<struct kinfo_proc> proc_infos; 686 const size_t num_proc_infos = GetAllInfosMatchingName (waitfor_process_name, proc_infos); 687 for (size_t i=0; i<num_proc_infos; i++) 688 { 689 nub_process_t curr_pid = proc_infos[i].kp_proc.p_pid; 690 for (size_t j=0; j<num_exclude_proc_infos; j++) 691 { 692 if (curr_pid == exclude_proc_infos[j].kp_proc.p_pid) 693 { 694 // This process was in our exclusion list, don't use it. 695 curr_pid = INVALID_NUB_PROCESS; 696 break; 697 } 698 } 699 700 // If we didn't find CURR_PID in our exclusion list, then use it. 701 if (curr_pid != INVALID_NUB_PROCESS) 702 { 703 // We found our process! 704 waitfor_pid = curr_pid; 705 break; 706 } 707 } 708 } 709 710 // If we haven't found our process yet, check for a timeout 711 // and then sleep for a bit until we poll again. 712 if (waitfor_pid == INVALID_NUB_PROCESS) 713 { 714 if (timeout_abstime != NULL) 715 { 716 // Check to see if we have a waitfor-duration option that 717 // has timed out? 718 if (DNBTimer::TimeOfDayLaterThan(*timeout_abstime)) 719 { 720 if (err_str && err_len > 0) 721 snprintf(err_str, err_len, "operation timed out"); 722 DNBLogError ("error: waiting for process '%s' timed out.\n", waitfor_process_name); 723 return INVALID_NUB_PROCESS; 724 } 725 } 726 727 // Call the should cancel callback as well... 728 729 if (should_cancel_callback != NULL 730 && should_cancel_callback (callback_data)) 731 { 732 DNBLogThreadedIf (LOG_PROCESS, "DNBProcessAttachWait cancelled by should_cancel callback."); 733 waitfor_pid = INVALID_NUB_PROCESS; 734 break; 735 } 736 737 ::usleep (waitfor_interval); // Sleep for WAITFOR_INTERVAL, then poll again 738 } 739 } 740 741 if (waitfor_pid != INVALID_NUB_PROCESS) 742 { 743 DNBLogThreadedIf (LOG_PROCESS, "Attaching to %s with pid %i...\n", waitfor_process_name, waitfor_pid); 744 waitfor_pid = DNBProcessAttach (waitfor_pid, timeout_abstime, err_str, err_len); 745 } 746 747 bool success = waitfor_pid != INVALID_NUB_PROCESS; 748 MachProcess::CleanupAfterAttach (attach_token, success, prepare_error); 749 750 return waitfor_pid; 751 } 752 753 nub_bool_t 754 DNBProcessDetach (nub_process_t pid) 755 { 756 MachProcessSP procSP; 757 if (GetProcessSP (pid, procSP)) 758 { 759 const bool remove = true; 760 DNBLogThreaded("Disabling breakpoints and watchpoints, and detaching from %d.", pid); 761 procSP->DisableAllBreakpoints(remove); 762 procSP->DisableAllWatchpoints (remove); 763 return procSP->Detach(); 764 } 765 return false; 766 } 767 768 nub_bool_t 769 DNBProcessKill (nub_process_t pid) 770 { 771 MachProcessSP procSP; 772 if (GetProcessSP (pid, procSP)) 773 { 774 return procSP->Kill (); 775 } 776 return false; 777 } 778 779 nub_bool_t 780 DNBProcessSignal (nub_process_t pid, int signal) 781 { 782 MachProcessSP procSP; 783 if (GetProcessSP (pid, procSP)) 784 { 785 return procSP->Signal (signal); 786 } 787 return false; 788 } 789 790 nub_bool_t 791 DNBProcessSendEvent (nub_process_t pid, const char *event) 792 { 793 MachProcessSP procSP; 794 if (GetProcessSP (pid, procSP)) 795 { 796 // FIXME: Do something with the error... 797 DNBError send_error; 798 return procSP->SendEvent (event, send_error); 799 } 800 return false; 801 } 802 803 804 nub_bool_t 805 DNBProcessIsAlive (nub_process_t pid) 806 { 807 MachProcessSP procSP; 808 if (GetProcessSP (pid, procSP)) 809 { 810 return MachTask::IsValid (procSP->Task().TaskPort()); 811 } 812 return eStateInvalid; 813 } 814 815 //---------------------------------------------------------------------- 816 // Process and Thread state information 817 //---------------------------------------------------------------------- 818 nub_state_t 819 DNBProcessGetState (nub_process_t pid) 820 { 821 MachProcessSP procSP; 822 if (GetProcessSP (pid, procSP)) 823 { 824 return procSP->GetState(); 825 } 826 return eStateInvalid; 827 } 828 829 //---------------------------------------------------------------------- 830 // Process and Thread state information 831 //---------------------------------------------------------------------- 832 nub_bool_t 833 DNBProcessGetExitStatus (nub_process_t pid, int* status) 834 { 835 MachProcessSP procSP; 836 if (GetProcessSP (pid, procSP)) 837 { 838 return procSP->GetExitStatus(status); 839 } 840 return false; 841 } 842 843 nub_bool_t 844 DNBProcessSetExitStatus (nub_process_t pid, int status) 845 { 846 MachProcessSP procSP; 847 if (GetProcessSP (pid, procSP)) 848 { 849 procSP->SetExitStatus(status); 850 return true; 851 } 852 return false; 853 } 854 855 const char * 856 DNBProcessGetExitInfo (nub_process_t pid) 857 { 858 MachProcessSP procSP; 859 if (GetProcessSP (pid, procSP)) 860 { 861 return procSP->GetExitInfo(); 862 } 863 return NULL; 864 } 865 866 nub_bool_t 867 DNBProcessSetExitInfo (nub_process_t pid, const char *info) 868 { 869 MachProcessSP procSP; 870 if (GetProcessSP (pid, procSP)) 871 { 872 procSP->SetExitInfo(info); 873 return true; 874 } 875 return false; 876 } 877 878 const char * 879 DNBThreadGetName (nub_process_t pid, nub_thread_t tid) 880 { 881 MachProcessSP procSP; 882 if (GetProcessSP (pid, procSP)) 883 return procSP->ThreadGetName(tid); 884 return NULL; 885 } 886 887 888 nub_bool_t 889 DNBThreadGetIdentifierInfo (nub_process_t pid, nub_thread_t tid, thread_identifier_info_data_t *ident_info) 890 { 891 MachProcessSP procSP; 892 if (GetProcessSP (pid, procSP)) 893 return procSP->GetThreadList().GetIdentifierInfo(tid, ident_info); 894 return false; 895 } 896 897 nub_state_t 898 DNBThreadGetState (nub_process_t pid, nub_thread_t tid) 899 { 900 MachProcessSP procSP; 901 if (GetProcessSP (pid, procSP)) 902 { 903 return procSP->ThreadGetState(tid); 904 } 905 return eStateInvalid; 906 } 907 908 const char * 909 DNBStateAsString(nub_state_t state) 910 { 911 switch (state) 912 { 913 case eStateInvalid: return "Invalid"; 914 case eStateUnloaded: return "Unloaded"; 915 case eStateAttaching: return "Attaching"; 916 case eStateLaunching: return "Launching"; 917 case eStateStopped: return "Stopped"; 918 case eStateRunning: return "Running"; 919 case eStateStepping: return "Stepping"; 920 case eStateCrashed: return "Crashed"; 921 case eStateDetached: return "Detached"; 922 case eStateExited: return "Exited"; 923 case eStateSuspended: return "Suspended"; 924 } 925 return "nub_state_t ???"; 926 } 927 928 const char * 929 DNBProcessGetExecutablePath (nub_process_t pid) 930 { 931 MachProcessSP procSP; 932 if (GetProcessSP (pid, procSP)) 933 { 934 return procSP->Path(); 935 } 936 return NULL; 937 } 938 939 nub_size_t 940 DNBProcessGetArgumentCount (nub_process_t pid) 941 { 942 MachProcessSP procSP; 943 if (GetProcessSP (pid, procSP)) 944 { 945 return procSP->ArgumentCount(); 946 } 947 return 0; 948 } 949 950 const char * 951 DNBProcessGetArgumentAtIndex (nub_process_t pid, nub_size_t idx) 952 { 953 MachProcessSP procSP; 954 if (GetProcessSP (pid, procSP)) 955 { 956 return procSP->ArgumentAtIndex (idx); 957 } 958 return NULL; 959 } 960 961 962 //---------------------------------------------------------------------- 963 // Execution control 964 //---------------------------------------------------------------------- 965 nub_bool_t 966 DNBProcessResume (nub_process_t pid, const DNBThreadResumeAction *actions, size_t num_actions) 967 { 968 DNBLogThreadedIf(LOG_PROCESS, "%s(pid = %4.4x)", __FUNCTION__, pid); 969 MachProcessSP procSP; 970 if (GetProcessSP (pid, procSP)) 971 { 972 DNBThreadResumeActions thread_actions (actions, num_actions); 973 974 // Below we add a default thread plan just in case one wasn't 975 // provided so all threads always know what they were supposed to do 976 if (thread_actions.IsEmpty()) 977 { 978 // No thread plans were given, so the default it to run all threads 979 thread_actions.SetDefaultThreadActionIfNeeded (eStateRunning, 0); 980 } 981 else 982 { 983 // Some thread plans were given which means anything that wasn't 984 // specified should remain stopped. 985 thread_actions.SetDefaultThreadActionIfNeeded (eStateStopped, 0); 986 } 987 return procSP->Resume (thread_actions); 988 } 989 return false; 990 } 991 992 nub_bool_t 993 DNBProcessHalt (nub_process_t pid) 994 { 995 DNBLogThreadedIf(LOG_PROCESS, "%s(pid = %4.4x)", __FUNCTION__, pid); 996 MachProcessSP procSP; 997 if (GetProcessSP (pid, procSP)) 998 return procSP->Signal (SIGSTOP); 999 return false; 1000 } 1001 // 1002 //nub_bool_t 1003 //DNBThreadResume (nub_process_t pid, nub_thread_t tid, nub_bool_t step) 1004 //{ 1005 // DNBLogThreadedIf(LOG_THREAD, "%s(pid = %4.4x, tid = %4.4x, step = %u)", __FUNCTION__, pid, tid, (uint32_t)step); 1006 // MachProcessSP procSP; 1007 // if (GetProcessSP (pid, procSP)) 1008 // { 1009 // return procSP->Resume(tid, step, 0); 1010 // } 1011 // return false; 1012 //} 1013 // 1014 //nub_bool_t 1015 //DNBThreadResumeWithSignal (nub_process_t pid, nub_thread_t tid, nub_bool_t step, int signal) 1016 //{ 1017 // DNBLogThreadedIf(LOG_THREAD, "%s(pid = %4.4x, tid = %4.4x, step = %u, signal = %i)", __FUNCTION__, pid, tid, (uint32_t)step, signal); 1018 // MachProcessSP procSP; 1019 // if (GetProcessSP (pid, procSP)) 1020 // { 1021 // return procSP->Resume(tid, step, signal); 1022 // } 1023 // return false; 1024 //} 1025 1026 nub_event_t 1027 DNBProcessWaitForEvents (nub_process_t pid, nub_event_t event_mask, bool wait_for_set, struct timespec* timeout) 1028 { 1029 nub_event_t result = 0; 1030 MachProcessSP procSP; 1031 if (GetProcessSP (pid, procSP)) 1032 { 1033 if (wait_for_set) 1034 result = procSP->Events().WaitForSetEvents(event_mask, timeout); 1035 else 1036 result = procSP->Events().WaitForEventsToReset(event_mask, timeout); 1037 } 1038 return result; 1039 } 1040 1041 void 1042 DNBProcessResetEvents (nub_process_t pid, nub_event_t event_mask) 1043 { 1044 MachProcessSP procSP; 1045 if (GetProcessSP (pid, procSP)) 1046 procSP->Events().ResetEvents(event_mask); 1047 } 1048 1049 // Breakpoints 1050 nub_bool_t 1051 DNBBreakpointSet (nub_process_t pid, nub_addr_t addr, nub_size_t size, nub_bool_t hardware) 1052 { 1053 MachProcessSP procSP; 1054 if (GetProcessSP (pid, procSP)) 1055 return procSP->CreateBreakpoint(addr, size, hardware) != NULL; 1056 return false; 1057 } 1058 1059 nub_bool_t 1060 DNBBreakpointClear (nub_process_t pid, nub_addr_t addr) 1061 { 1062 MachProcessSP procSP; 1063 if (GetProcessSP (pid, procSP)) 1064 return procSP->DisableBreakpoint(addr, true); 1065 return false; // Failed 1066 } 1067 1068 1069 //---------------------------------------------------------------------- 1070 // Watchpoints 1071 //---------------------------------------------------------------------- 1072 nub_bool_t 1073 DNBWatchpointSet (nub_process_t pid, nub_addr_t addr, nub_size_t size, uint32_t watch_flags, nub_bool_t hardware) 1074 { 1075 MachProcessSP procSP; 1076 if (GetProcessSP (pid, procSP)) 1077 return procSP->CreateWatchpoint(addr, size, watch_flags, hardware) != NULL; 1078 return false; 1079 } 1080 1081 nub_bool_t 1082 DNBWatchpointClear (nub_process_t pid, nub_addr_t addr) 1083 { 1084 MachProcessSP procSP; 1085 if (GetProcessSP (pid, procSP)) 1086 return procSP->DisableWatchpoint(addr, true); 1087 return false; // Failed 1088 } 1089 1090 //---------------------------------------------------------------------- 1091 // Return the number of supported hardware watchpoints. 1092 //---------------------------------------------------------------------- 1093 uint32_t 1094 DNBWatchpointGetNumSupportedHWP (nub_process_t pid) 1095 { 1096 MachProcessSP procSP; 1097 if (GetProcessSP (pid, procSP)) 1098 return procSP->GetNumSupportedHardwareWatchpoints(); 1099 return 0; 1100 } 1101 1102 //---------------------------------------------------------------------- 1103 // Read memory in the address space of process PID. This call will take 1104 // care of setting and restoring permissions and breaking up the memory 1105 // read into multiple chunks as required. 1106 // 1107 // RETURNS: number of bytes actually read 1108 //---------------------------------------------------------------------- 1109 nub_size_t 1110 DNBProcessMemoryRead (nub_process_t pid, nub_addr_t addr, nub_size_t size, void *buf) 1111 { 1112 MachProcessSP procSP; 1113 if (GetProcessSP (pid, procSP)) 1114 return procSP->ReadMemory(addr, size, buf); 1115 return 0; 1116 } 1117 1118 //---------------------------------------------------------------------- 1119 // Write memory to the address space of process PID. This call will take 1120 // care of setting and restoring permissions and breaking up the memory 1121 // write into multiple chunks as required. 1122 // 1123 // RETURNS: number of bytes actually written 1124 //---------------------------------------------------------------------- 1125 nub_size_t 1126 DNBProcessMemoryWrite (nub_process_t pid, nub_addr_t addr, nub_size_t size, const void *buf) 1127 { 1128 MachProcessSP procSP; 1129 if (GetProcessSP (pid, procSP)) 1130 return procSP->WriteMemory(addr, size, buf); 1131 return 0; 1132 } 1133 1134 nub_addr_t 1135 DNBProcessMemoryAllocate (nub_process_t pid, nub_size_t size, uint32_t permissions) 1136 { 1137 MachProcessSP procSP; 1138 if (GetProcessSP (pid, procSP)) 1139 return procSP->Task().AllocateMemory (size, permissions); 1140 return 0; 1141 } 1142 1143 nub_bool_t 1144 DNBProcessMemoryDeallocate (nub_process_t pid, nub_addr_t addr) 1145 { 1146 MachProcessSP procSP; 1147 if (GetProcessSP (pid, procSP)) 1148 return procSP->Task().DeallocateMemory (addr); 1149 return 0; 1150 } 1151 1152 //---------------------------------------------------------------------- 1153 // Find attributes of the memory region that contains ADDR for process PID, 1154 // if possible, and return a string describing those attributes. 1155 // 1156 // Returns 1 if we could find attributes for this region and OUTBUF can 1157 // be sent to the remote debugger. 1158 // 1159 // Returns 0 if we couldn't find the attributes for a region of memory at 1160 // that address and OUTBUF should not be sent. 1161 // 1162 // Returns -1 if this platform cannot look up information about memory regions 1163 // or if we do not yet have a valid launched process. 1164 // 1165 //---------------------------------------------------------------------- 1166 int 1167 DNBProcessMemoryRegionInfo (nub_process_t pid, nub_addr_t addr, DNBRegionInfo *region_info) 1168 { 1169 MachProcessSP procSP; 1170 if (GetProcessSP (pid, procSP)) 1171 return procSP->Task().GetMemoryRegionInfo (addr, region_info); 1172 1173 return -1; 1174 } 1175 1176 std::string 1177 DNBProcessGetProfileData (nub_process_t pid, DNBProfileDataScanType scanType) 1178 { 1179 MachProcessSP procSP; 1180 if (GetProcessSP (pid, procSP)) 1181 return procSP->Task().GetProfileData(scanType); 1182 1183 return std::string(""); 1184 } 1185 1186 nub_bool_t 1187 DNBProcessSetEnableAsyncProfiling (nub_process_t pid, nub_bool_t enable, uint64_t interval_usec, DNBProfileDataScanType scan_type) 1188 { 1189 MachProcessSP procSP; 1190 if (GetProcessSP (pid, procSP)) 1191 { 1192 procSP->SetEnableAsyncProfiling(enable, interval_usec, scan_type); 1193 return true; 1194 } 1195 1196 return false; 1197 } 1198 1199 //---------------------------------------------------------------------- 1200 // Formatted output that uses memory and registers from process and 1201 // thread in place of arguments. 1202 //---------------------------------------------------------------------- 1203 nub_size_t 1204 DNBPrintf (nub_process_t pid, nub_thread_t tid, nub_addr_t base_addr, FILE *file, const char *format) 1205 { 1206 if (file == NULL) 1207 return 0; 1208 enum printf_flags 1209 { 1210 alternate_form = (1 << 0), 1211 zero_padding = (1 << 1), 1212 negative_field_width = (1 << 2), 1213 blank_space = (1 << 3), 1214 show_sign = (1 << 4), 1215 show_thousands_separator= (1 << 5), 1216 }; 1217 1218 enum printf_length_modifiers 1219 { 1220 length_mod_h = (1 << 0), 1221 length_mod_hh = (1 << 1), 1222 length_mod_l = (1 << 2), 1223 length_mod_ll = (1 << 3), 1224 length_mod_L = (1 << 4), 1225 length_mod_j = (1 << 5), 1226 length_mod_t = (1 << 6), 1227 length_mod_z = (1 << 7), 1228 length_mod_q = (1 << 8), 1229 }; 1230 1231 nub_addr_t addr = base_addr; 1232 char *end_format = (char*)format + strlen(format); 1233 char *end = NULL; // For strtoXXXX calls; 1234 std::basic_string<uint8_t> buf; 1235 nub_size_t total_bytes_read = 0; 1236 DNBDataRef data; 1237 const char *f; 1238 for (f = format; *f != '\0' && f < end_format; f++) 1239 { 1240 char ch = *f; 1241 switch (ch) 1242 { 1243 case '%': 1244 { 1245 f++; // Skip the '%' character 1246 // int min_field_width = 0; 1247 // int precision = 0; 1248 //uint32_t flags = 0; 1249 uint32_t length_modifiers = 0; 1250 uint32_t byte_size = 0; 1251 uint32_t actual_byte_size = 0; 1252 bool is_string = false; 1253 bool is_register = false; 1254 DNBRegisterValue register_value; 1255 int64_t register_offset = 0; 1256 nub_addr_t register_addr = INVALID_NUB_ADDRESS; 1257 1258 // Create the format string to use for this conversion specification 1259 // so we can remove and mprintf specific flags and formatters. 1260 std::string fprintf_format("%"); 1261 1262 // Decode any flags 1263 switch (*f) 1264 { 1265 case '#': fprintf_format += *f++; break; //flags |= alternate_form; break; 1266 case '0': fprintf_format += *f++; break; //flags |= zero_padding; break; 1267 case '-': fprintf_format += *f++; break; //flags |= negative_field_width; break; 1268 case ' ': fprintf_format += *f++; break; //flags |= blank_space; break; 1269 case '+': fprintf_format += *f++; break; //flags |= show_sign; break; 1270 case ',': fprintf_format += *f++; break; //flags |= show_thousands_separator;break; 1271 case '{': 1272 case '[': 1273 { 1274 // We have a register name specification that can take two forms: 1275 // ${regname} or ${regname+offset} 1276 // The action is to read the register value and add the signed offset 1277 // (if any) and use that as the value to format. 1278 // $[regname] or $[regname+offset] 1279 // The action is to read the register value and add the signed offset 1280 // (if any) and use the result as an address to dereference. The size 1281 // of what is dereferenced is specified by the actual byte size that 1282 // follows the minimum field width and precision (see comments below). 1283 switch (*f) 1284 { 1285 case '{': 1286 case '[': 1287 { 1288 char open_scope_ch = *f; 1289 f++; 1290 const char *reg_name = f; 1291 size_t reg_name_length = strcspn(f, "+-}]"); 1292 if (reg_name_length > 0) 1293 { 1294 std::string register_name(reg_name, reg_name_length); 1295 f += reg_name_length; 1296 register_offset = strtoll(f, &end, 0); 1297 if (f < end) 1298 f = end; 1299 if ((open_scope_ch == '{' && *f != '}') || (open_scope_ch == '[' && *f != ']')) 1300 { 1301 fprintf(file, "error: Invalid register format string. Valid formats are %%{regname} or %%{regname+offset}, %%[regname] or %%[regname+offset]\n"); 1302 return total_bytes_read; 1303 } 1304 else 1305 { 1306 f++; 1307 if (DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, register_name.c_str(), ®ister_value)) 1308 { 1309 // Set the address to dereference using the register value plus the offset 1310 switch (register_value.info.size) 1311 { 1312 default: 1313 case 0: 1314 fprintf (file, "error: unsupported register size of %u.\n", register_value.info.size); 1315 return total_bytes_read; 1316 1317 case 1: register_addr = register_value.value.uint8 + register_offset; break; 1318 case 2: register_addr = register_value.value.uint16 + register_offset; break; 1319 case 4: register_addr = register_value.value.uint32 + register_offset; break; 1320 case 8: register_addr = register_value.value.uint64 + register_offset; break; 1321 case 16: 1322 if (open_scope_ch == '[') 1323 { 1324 fprintf (file, "error: register size (%u) too large for address.\n", register_value.info.size); 1325 return total_bytes_read; 1326 } 1327 break; 1328 } 1329 1330 if (open_scope_ch == '{') 1331 { 1332 byte_size = register_value.info.size; 1333 is_register = true; // value is in a register 1334 1335 } 1336 else 1337 { 1338 addr = register_addr; // Use register value and offset as the address 1339 } 1340 } 1341 else 1342 { 1343 fprintf(file, "error: unable to read register '%s' for process %#.4x and thread %#.8" PRIx64 "\n", register_name.c_str(), pid, tid); 1344 return total_bytes_read; 1345 } 1346 } 1347 } 1348 } 1349 break; 1350 1351 default: 1352 fprintf(file, "error: %%$ must be followed by (regname + n) or [regname + n]\n"); 1353 return total_bytes_read; 1354 } 1355 } 1356 break; 1357 } 1358 1359 // Check for a minimum field width 1360 if (isdigit(*f)) 1361 { 1362 //min_field_width = strtoul(f, &end, 10); 1363 strtoul(f, &end, 10); 1364 if (end > f) 1365 { 1366 fprintf_format.append(f, end - f); 1367 f = end; 1368 } 1369 } 1370 1371 1372 // Check for a precision 1373 if (*f == '.') 1374 { 1375 f++; 1376 if (isdigit(*f)) 1377 { 1378 fprintf_format += '.'; 1379 //precision = strtoul(f, &end, 10); 1380 strtoul(f, &end, 10); 1381 if (end > f) 1382 { 1383 fprintf_format.append(f, end - f); 1384 f = end; 1385 } 1386 } 1387 } 1388 1389 1390 // mprintf specific: read the optional actual byte size (abs) 1391 // after the standard minimum field width (mfw) and precision (prec). 1392 // Standard printf calls you can have "mfw.prec" or ".prec", but 1393 // mprintf can have "mfw.prec.abs", ".prec.abs" or "..abs". This is nice 1394 // for strings that may be in a fixed size buffer, but may not use all bytes 1395 // in that buffer for printable characters. 1396 if (*f == '.') 1397 { 1398 f++; 1399 actual_byte_size = strtoul(f, &end, 10); 1400 if (end > f) 1401 { 1402 byte_size = actual_byte_size; 1403 f = end; 1404 } 1405 } 1406 1407 // Decode the length modifiers 1408 switch (*f) 1409 { 1410 case 'h': // h and hh length modifiers 1411 fprintf_format += *f++; 1412 length_modifiers |= length_mod_h; 1413 if (*f == 'h') 1414 { 1415 fprintf_format += *f++; 1416 length_modifiers |= length_mod_hh; 1417 } 1418 break; 1419 1420 case 'l': // l and ll length modifiers 1421 fprintf_format += *f++; 1422 length_modifiers |= length_mod_l; 1423 if (*f == 'h') 1424 { 1425 fprintf_format += *f++; 1426 length_modifiers |= length_mod_ll; 1427 } 1428 break; 1429 1430 case 'L': fprintf_format += *f++; length_modifiers |= length_mod_L; break; 1431 case 'j': fprintf_format += *f++; length_modifiers |= length_mod_j; break; 1432 case 't': fprintf_format += *f++; length_modifiers |= length_mod_t; break; 1433 case 'z': fprintf_format += *f++; length_modifiers |= length_mod_z; break; 1434 case 'q': fprintf_format += *f++; length_modifiers |= length_mod_q; break; 1435 } 1436 1437 // Decode the conversion specifier 1438 switch (*f) 1439 { 1440 case '_': 1441 // mprintf specific format items 1442 { 1443 ++f; // Skip the '_' character 1444 switch (*f) 1445 { 1446 case 'a': // Print the current address 1447 ++f; 1448 fprintf_format += "ll"; 1449 fprintf_format += *f; // actual format to show address with folows the 'a' ("%_ax") 1450 fprintf (file, fprintf_format.c_str(), addr); 1451 break; 1452 case 'o': // offset from base address 1453 ++f; 1454 fprintf_format += "ll"; 1455 fprintf_format += *f; // actual format to show address with folows the 'a' ("%_ox") 1456 fprintf(file, fprintf_format.c_str(), addr - base_addr); 1457 break; 1458 default: 1459 fprintf (file, "error: unsupported mprintf specific format character '%c'.\n", *f); 1460 break; 1461 } 1462 continue; 1463 } 1464 break; 1465 1466 case 'D': 1467 case 'O': 1468 case 'U': 1469 fprintf_format += *f; 1470 if (byte_size == 0) 1471 byte_size = sizeof(long int); 1472 break; 1473 1474 case 'd': 1475 case 'i': 1476 case 'o': 1477 case 'u': 1478 case 'x': 1479 case 'X': 1480 fprintf_format += *f; 1481 if (byte_size == 0) 1482 { 1483 if (length_modifiers & length_mod_hh) 1484 byte_size = sizeof(char); 1485 else if (length_modifiers & length_mod_h) 1486 byte_size = sizeof(short); 1487 else if (length_modifiers & length_mod_ll) 1488 byte_size = sizeof(long long); 1489 else if (length_modifiers & length_mod_l) 1490 byte_size = sizeof(long); 1491 else 1492 byte_size = sizeof(int); 1493 } 1494 break; 1495 1496 case 'a': 1497 case 'A': 1498 case 'f': 1499 case 'F': 1500 case 'e': 1501 case 'E': 1502 case 'g': 1503 case 'G': 1504 fprintf_format += *f; 1505 if (byte_size == 0) 1506 { 1507 if (length_modifiers & length_mod_L) 1508 byte_size = sizeof(long double); 1509 else 1510 byte_size = sizeof(double); 1511 } 1512 break; 1513 1514 case 'c': 1515 if ((length_modifiers & length_mod_l) == 0) 1516 { 1517 fprintf_format += *f; 1518 if (byte_size == 0) 1519 byte_size = sizeof(char); 1520 break; 1521 } 1522 // Fall through to 'C' modifier below... 1523 1524 case 'C': 1525 fprintf_format += *f; 1526 if (byte_size == 0) 1527 byte_size = sizeof(wchar_t); 1528 break; 1529 1530 case 's': 1531 fprintf_format += *f; 1532 if (is_register || byte_size == 0) 1533 is_string = 1; 1534 break; 1535 1536 case 'p': 1537 fprintf_format += *f; 1538 if (byte_size == 0) 1539 byte_size = sizeof(void*); 1540 break; 1541 } 1542 1543 if (is_string) 1544 { 1545 std::string mem_string; 1546 const size_t string_buf_len = 4; 1547 char string_buf[string_buf_len+1]; 1548 char *string_buf_end = string_buf + string_buf_len; 1549 string_buf[string_buf_len] = '\0'; 1550 nub_size_t bytes_read; 1551 nub_addr_t str_addr = is_register ? register_addr : addr; 1552 while ((bytes_read = DNBProcessMemoryRead(pid, str_addr, string_buf_len, &string_buf[0])) > 0) 1553 { 1554 // Did we get a NULL termination character yet? 1555 if (strchr(string_buf, '\0') == string_buf_end) 1556 { 1557 // no NULL terminator yet, append as a std::string 1558 mem_string.append(string_buf, string_buf_len); 1559 str_addr += string_buf_len; 1560 } 1561 else 1562 { 1563 // yep 1564 break; 1565 } 1566 } 1567 // Append as a C-string so we don't get the extra NULL 1568 // characters in the temp buffer (since it was resized) 1569 mem_string += string_buf; 1570 size_t mem_string_len = mem_string.size() + 1; 1571 fprintf(file, fprintf_format.c_str(), mem_string.c_str()); 1572 if (mem_string_len > 0) 1573 { 1574 if (!is_register) 1575 { 1576 addr += mem_string_len; 1577 total_bytes_read += mem_string_len; 1578 } 1579 } 1580 else 1581 return total_bytes_read; 1582 } 1583 else 1584 if (byte_size > 0) 1585 { 1586 buf.resize(byte_size); 1587 nub_size_t bytes_read = 0; 1588 if (is_register) 1589 bytes_read = register_value.info.size; 1590 else 1591 bytes_read = DNBProcessMemoryRead(pid, addr, buf.size(), &buf[0]); 1592 if (bytes_read > 0) 1593 { 1594 if (!is_register) 1595 total_bytes_read += bytes_read; 1596 1597 if (bytes_read == byte_size) 1598 { 1599 switch (*f) 1600 { 1601 case 'd': 1602 case 'i': 1603 case 'o': 1604 case 'u': 1605 case 'X': 1606 case 'x': 1607 case 'a': 1608 case 'A': 1609 case 'f': 1610 case 'F': 1611 case 'e': 1612 case 'E': 1613 case 'g': 1614 case 'G': 1615 case 'p': 1616 case 'c': 1617 case 'C': 1618 { 1619 if (is_register) 1620 data.SetData(®ister_value.value.v_uint8[0], register_value.info.size); 1621 else 1622 data.SetData(&buf[0], bytes_read); 1623 DNBDataRef::offset_t data_offset = 0; 1624 if (byte_size <= 4) 1625 { 1626 uint32_t u32 = data.GetMax32(&data_offset, byte_size); 1627 // Show the actual byte width when displaying hex 1628 fprintf(file, fprintf_format.c_str(), u32); 1629 } 1630 else if (byte_size <= 8) 1631 { 1632 uint64_t u64 = data.GetMax64(&data_offset, byte_size); 1633 // Show the actual byte width when displaying hex 1634 fprintf(file, fprintf_format.c_str(), u64); 1635 } 1636 else 1637 { 1638 fprintf(file, "error: integer size not supported, must be 8 bytes or less (%u bytes).\n", byte_size); 1639 } 1640 if (!is_register) 1641 addr += byte_size; 1642 } 1643 break; 1644 1645 case 's': 1646 fprintf(file, fprintf_format.c_str(), buf.c_str()); 1647 addr += byte_size; 1648 break; 1649 1650 default: 1651 fprintf(file, "error: unsupported conversion specifier '%c'.\n", *f); 1652 break; 1653 } 1654 } 1655 } 1656 } 1657 else 1658 return total_bytes_read; 1659 } 1660 break; 1661 1662 case '\\': 1663 { 1664 f++; 1665 switch (*f) 1666 { 1667 case 'e': ch = '\e'; break; 1668 case 'a': ch = '\a'; break; 1669 case 'b': ch = '\b'; break; 1670 case 'f': ch = '\f'; break; 1671 case 'n': ch = '\n'; break; 1672 case 'r': ch = '\r'; break; 1673 case 't': ch = '\t'; break; 1674 case 'v': ch = '\v'; break; 1675 case '\'': ch = '\''; break; 1676 case '\\': ch = '\\'; break; 1677 case '0': 1678 case '1': 1679 case '2': 1680 case '3': 1681 case '4': 1682 case '5': 1683 case '6': 1684 case '7': 1685 ch = strtoul(f, &end, 8); 1686 f = end; 1687 break; 1688 default: 1689 ch = *f; 1690 break; 1691 } 1692 fputc(ch, file); 1693 } 1694 break; 1695 1696 default: 1697 fputc(ch, file); 1698 break; 1699 } 1700 } 1701 return total_bytes_read; 1702 } 1703 1704 1705 //---------------------------------------------------------------------- 1706 // Get the number of threads for the specified process. 1707 //---------------------------------------------------------------------- 1708 nub_size_t 1709 DNBProcessGetNumThreads (nub_process_t pid) 1710 { 1711 MachProcessSP procSP; 1712 if (GetProcessSP (pid, procSP)) 1713 return procSP->GetNumThreads(); 1714 return 0; 1715 } 1716 1717 //---------------------------------------------------------------------- 1718 // Get the thread ID of the current thread. 1719 //---------------------------------------------------------------------- 1720 nub_thread_t 1721 DNBProcessGetCurrentThread (nub_process_t pid) 1722 { 1723 MachProcessSP procSP; 1724 if (GetProcessSP (pid, procSP)) 1725 return procSP->GetCurrentThread(); 1726 return 0; 1727 } 1728 1729 //---------------------------------------------------------------------- 1730 // Get the mach port number of the current thread. 1731 //---------------------------------------------------------------------- 1732 nub_thread_t 1733 DNBProcessGetCurrentThreadMachPort (nub_process_t pid) 1734 { 1735 MachProcessSP procSP; 1736 if (GetProcessSP (pid, procSP)) 1737 return procSP->GetCurrentThreadMachPort(); 1738 return 0; 1739 } 1740 1741 //---------------------------------------------------------------------- 1742 // Change the current thread. 1743 //---------------------------------------------------------------------- 1744 nub_thread_t 1745 DNBProcessSetCurrentThread (nub_process_t pid, nub_thread_t tid) 1746 { 1747 MachProcessSP procSP; 1748 if (GetProcessSP (pid, procSP)) 1749 return procSP->SetCurrentThread (tid); 1750 return INVALID_NUB_THREAD; 1751 } 1752 1753 1754 //---------------------------------------------------------------------- 1755 // Dump a string describing a thread's stop reason to the specified file 1756 // handle 1757 //---------------------------------------------------------------------- 1758 nub_bool_t 1759 DNBThreadGetStopReason (nub_process_t pid, nub_thread_t tid, struct DNBThreadStopInfo *stop_info) 1760 { 1761 MachProcessSP procSP; 1762 if (GetProcessSP (pid, procSP)) 1763 return procSP->GetThreadStoppedReason (tid, stop_info); 1764 return false; 1765 } 1766 1767 //---------------------------------------------------------------------- 1768 // Return string description for the specified thread. 1769 // 1770 // RETURNS: NULL if the thread isn't valid, else a NULL terminated C 1771 // string from a static buffer that must be copied prior to subsequent 1772 // calls. 1773 //---------------------------------------------------------------------- 1774 const char * 1775 DNBThreadGetInfo (nub_process_t pid, nub_thread_t tid) 1776 { 1777 MachProcessSP procSP; 1778 if (GetProcessSP (pid, procSP)) 1779 return procSP->GetThreadInfo (tid); 1780 return NULL; 1781 } 1782 1783 //---------------------------------------------------------------------- 1784 // Get the thread ID given a thread index. 1785 //---------------------------------------------------------------------- 1786 nub_thread_t 1787 DNBProcessGetThreadAtIndex (nub_process_t pid, size_t thread_idx) 1788 { 1789 MachProcessSP procSP; 1790 if (GetProcessSP (pid, procSP)) 1791 return procSP->GetThreadAtIndex (thread_idx); 1792 return INVALID_NUB_THREAD; 1793 } 1794 1795 //---------------------------------------------------------------------- 1796 // Do whatever is needed to sync the thread's register state with it's kernel values. 1797 //---------------------------------------------------------------------- 1798 nub_bool_t 1799 DNBProcessSyncThreadState (nub_process_t pid, nub_thread_t tid) 1800 { 1801 MachProcessSP procSP; 1802 if (GetProcessSP (pid, procSP)) 1803 return procSP->SyncThreadState (tid); 1804 return false; 1805 1806 } 1807 1808 nub_addr_t 1809 DNBProcessGetSharedLibraryInfoAddress (nub_process_t pid) 1810 { 1811 MachProcessSP procSP; 1812 DNBError err; 1813 if (GetProcessSP (pid, procSP)) 1814 return procSP->Task().GetDYLDAllImageInfosAddress (err); 1815 return INVALID_NUB_ADDRESS; 1816 } 1817 1818 1819 nub_bool_t 1820 DNBProcessSharedLibrariesUpdated(nub_process_t pid) 1821 { 1822 MachProcessSP procSP; 1823 if (GetProcessSP (pid, procSP)) 1824 { 1825 procSP->SharedLibrariesUpdated (); 1826 return true; 1827 } 1828 return false; 1829 } 1830 1831 //---------------------------------------------------------------------- 1832 // Get the current shared library information for a process. Only return 1833 // the shared libraries that have changed since the last shared library 1834 // state changed event if only_changed is non-zero. 1835 //---------------------------------------------------------------------- 1836 nub_size_t 1837 DNBProcessGetSharedLibraryInfo (nub_process_t pid, nub_bool_t only_changed, struct DNBExecutableImageInfo **image_infos) 1838 { 1839 MachProcessSP procSP; 1840 if (GetProcessSP (pid, procSP)) 1841 return procSP->CopyImageInfos (image_infos, only_changed); 1842 1843 // If we have no process, then return NULL for the shared library info 1844 // and zero for shared library count 1845 *image_infos = NULL; 1846 return 0; 1847 } 1848 1849 //---------------------------------------------------------------------- 1850 // Get the register set information for a specific thread. 1851 //---------------------------------------------------------------------- 1852 const DNBRegisterSetInfo * 1853 DNBGetRegisterSetInfo (nub_size_t *num_reg_sets) 1854 { 1855 return DNBArchProtocol::GetRegisterSetInfo (num_reg_sets); 1856 } 1857 1858 1859 //---------------------------------------------------------------------- 1860 // Read a register value by register set and register index. 1861 //---------------------------------------------------------------------- 1862 nub_bool_t 1863 DNBThreadGetRegisterValueByID (nub_process_t pid, nub_thread_t tid, uint32_t set, uint32_t reg, DNBRegisterValue *value) 1864 { 1865 MachProcessSP procSP; 1866 ::bzero (value, sizeof(DNBRegisterValue)); 1867 if (GetProcessSP (pid, procSP)) 1868 { 1869 if (tid != INVALID_NUB_THREAD) 1870 return procSP->GetRegisterValue (tid, set, reg, value); 1871 } 1872 return false; 1873 } 1874 1875 nub_bool_t 1876 DNBThreadSetRegisterValueByID (nub_process_t pid, nub_thread_t tid, uint32_t set, uint32_t reg, const DNBRegisterValue *value) 1877 { 1878 if (tid != INVALID_NUB_THREAD) 1879 { 1880 MachProcessSP procSP; 1881 if (GetProcessSP (pid, procSP)) 1882 return procSP->SetRegisterValue (tid, set, reg, value); 1883 } 1884 return false; 1885 } 1886 1887 nub_size_t 1888 DNBThreadGetRegisterContext (nub_process_t pid, nub_thread_t tid, void *buf, size_t buf_len) 1889 { 1890 MachProcessSP procSP; 1891 if (GetProcessSP (pid, procSP)) 1892 { 1893 if (tid != INVALID_NUB_THREAD) 1894 return procSP->GetThreadList().GetRegisterContext (tid, buf, buf_len); 1895 } 1896 ::bzero (buf, buf_len); 1897 return 0; 1898 1899 } 1900 1901 nub_size_t 1902 DNBThreadSetRegisterContext (nub_process_t pid, nub_thread_t tid, const void *buf, size_t buf_len) 1903 { 1904 MachProcessSP procSP; 1905 if (GetProcessSP (pid, procSP)) 1906 { 1907 if (tid != INVALID_NUB_THREAD) 1908 return procSP->GetThreadList().SetRegisterContext (tid, buf, buf_len); 1909 } 1910 return 0; 1911 } 1912 1913 uint32_t 1914 DNBThreadSaveRegisterState (nub_process_t pid, nub_thread_t tid) 1915 { 1916 if (tid != INVALID_NUB_THREAD) 1917 { 1918 MachProcessSP procSP; 1919 if (GetProcessSP (pid, procSP)) 1920 return procSP->GetThreadList().SaveRegisterState (tid); 1921 } 1922 return 0; 1923 } 1924 nub_bool_t 1925 DNBThreadRestoreRegisterState (nub_process_t pid, nub_thread_t tid, uint32_t save_id) 1926 { 1927 if (tid != INVALID_NUB_THREAD) 1928 { 1929 MachProcessSP procSP; 1930 if (GetProcessSP (pid, procSP)) 1931 return procSP->GetThreadList().RestoreRegisterState (tid, save_id); 1932 } 1933 return false; 1934 } 1935 1936 1937 1938 //---------------------------------------------------------------------- 1939 // Read a register value by name. 1940 //---------------------------------------------------------------------- 1941 nub_bool_t 1942 DNBThreadGetRegisterValueByName (nub_process_t pid, nub_thread_t tid, uint32_t reg_set, const char *reg_name, DNBRegisterValue *value) 1943 { 1944 MachProcessSP procSP; 1945 ::bzero (value, sizeof(DNBRegisterValue)); 1946 if (GetProcessSP (pid, procSP)) 1947 { 1948 const struct DNBRegisterSetInfo *set_info; 1949 nub_size_t num_reg_sets = 0; 1950 set_info = DNBGetRegisterSetInfo (&num_reg_sets); 1951 if (set_info) 1952 { 1953 uint32_t set = reg_set; 1954 uint32_t reg; 1955 if (set == REGISTER_SET_ALL) 1956 { 1957 for (set = 1; set < num_reg_sets; ++set) 1958 { 1959 for (reg = 0; reg < set_info[set].num_registers; ++reg) 1960 { 1961 if (strcasecmp(reg_name, set_info[set].registers[reg].name) == 0) 1962 return procSP->GetRegisterValue (tid, set, reg, value); 1963 } 1964 } 1965 } 1966 else 1967 { 1968 for (reg = 0; reg < set_info[set].num_registers; ++reg) 1969 { 1970 if (strcasecmp(reg_name, set_info[set].registers[reg].name) == 0) 1971 return procSP->GetRegisterValue (tid, set, reg, value); 1972 } 1973 } 1974 } 1975 } 1976 return false; 1977 } 1978 1979 1980 //---------------------------------------------------------------------- 1981 // Read a register set and register number from the register name. 1982 //---------------------------------------------------------------------- 1983 nub_bool_t 1984 DNBGetRegisterInfoByName (const char *reg_name, DNBRegisterInfo* info) 1985 { 1986 const struct DNBRegisterSetInfo *set_info; 1987 nub_size_t num_reg_sets = 0; 1988 set_info = DNBGetRegisterSetInfo (&num_reg_sets); 1989 if (set_info) 1990 { 1991 uint32_t set, reg; 1992 for (set = 1; set < num_reg_sets; ++set) 1993 { 1994 for (reg = 0; reg < set_info[set].num_registers; ++reg) 1995 { 1996 if (strcasecmp(reg_name, set_info[set].registers[reg].name) == 0) 1997 { 1998 *info = set_info[set].registers[reg]; 1999 return true; 2000 } 2001 } 2002 } 2003 2004 for (set = 1; set < num_reg_sets; ++set) 2005 { 2006 uint32_t reg; 2007 for (reg = 0; reg < set_info[set].num_registers; ++reg) 2008 { 2009 if (set_info[set].registers[reg].alt == NULL) 2010 continue; 2011 2012 if (strcasecmp(reg_name, set_info[set].registers[reg].alt) == 0) 2013 { 2014 *info = set_info[set].registers[reg]; 2015 return true; 2016 } 2017 } 2018 } 2019 } 2020 2021 ::bzero (info, sizeof(DNBRegisterInfo)); 2022 return false; 2023 } 2024 2025 2026 //---------------------------------------------------------------------- 2027 // Set the name to address callback function that this nub can use 2028 // for any name to address lookups that are needed. 2029 //---------------------------------------------------------------------- 2030 nub_bool_t 2031 DNBProcessSetNameToAddressCallback (nub_process_t pid, DNBCallbackNameToAddress callback, void *baton) 2032 { 2033 MachProcessSP procSP; 2034 if (GetProcessSP (pid, procSP)) 2035 { 2036 procSP->SetNameToAddressCallback (callback, baton); 2037 return true; 2038 } 2039 return false; 2040 } 2041 2042 2043 //---------------------------------------------------------------------- 2044 // Set the name to address callback function that this nub can use 2045 // for any name to address lookups that are needed. 2046 //---------------------------------------------------------------------- 2047 nub_bool_t 2048 DNBProcessSetSharedLibraryInfoCallback (nub_process_t pid, DNBCallbackCopyExecutableImageInfos callback, void *baton) 2049 { 2050 MachProcessSP procSP; 2051 if (GetProcessSP (pid, procSP)) 2052 { 2053 procSP->SetSharedLibraryInfoCallback (callback, baton); 2054 return true; 2055 } 2056 return false; 2057 } 2058 2059 nub_addr_t 2060 DNBProcessLookupAddress (nub_process_t pid, const char *name, const char *shlib) 2061 { 2062 MachProcessSP procSP; 2063 if (GetProcessSP (pid, procSP)) 2064 { 2065 return procSP->LookupSymbol (name, shlib); 2066 } 2067 return INVALID_NUB_ADDRESS; 2068 } 2069 2070 2071 nub_size_t 2072 DNBProcessGetAvailableSTDOUT (nub_process_t pid, char *buf, nub_size_t buf_size) 2073 { 2074 MachProcessSP procSP; 2075 if (GetProcessSP (pid, procSP)) 2076 return procSP->GetAvailableSTDOUT (buf, buf_size); 2077 return 0; 2078 } 2079 2080 nub_size_t 2081 DNBProcessGetAvailableSTDERR (nub_process_t pid, char *buf, nub_size_t buf_size) 2082 { 2083 MachProcessSP procSP; 2084 if (GetProcessSP (pid, procSP)) 2085 return procSP->GetAvailableSTDERR (buf, buf_size); 2086 return 0; 2087 } 2088 2089 nub_size_t 2090 DNBProcessGetAvailableProfileData (nub_process_t pid, char *buf, nub_size_t buf_size) 2091 { 2092 MachProcessSP procSP; 2093 if (GetProcessSP (pid, procSP)) 2094 return procSP->GetAsyncProfileData (buf, buf_size); 2095 return 0; 2096 } 2097 2098 nub_size_t 2099 DNBProcessGetStopCount (nub_process_t pid) 2100 { 2101 MachProcessSP procSP; 2102 if (GetProcessSP (pid, procSP)) 2103 return procSP->StopCount(); 2104 return 0; 2105 } 2106 2107 uint32_t 2108 DNBProcessGetCPUType (nub_process_t pid) 2109 { 2110 MachProcessSP procSP; 2111 if (GetProcessSP (pid, procSP)) 2112 return procSP->GetCPUType (); 2113 return 0; 2114 2115 } 2116 2117 nub_bool_t 2118 DNBResolveExecutablePath (const char *path, char *resolved_path, size_t resolved_path_size) 2119 { 2120 if (path == NULL || path[0] == '\0') 2121 return false; 2122 2123 char max_path[PATH_MAX]; 2124 std::string result; 2125 CFString::GlobPath(path, result); 2126 2127 if (result.empty()) 2128 result = path; 2129 2130 struct stat path_stat; 2131 if (::stat(path, &path_stat) == 0) 2132 { 2133 if ((path_stat.st_mode & S_IFMT) == S_IFDIR) 2134 { 2135 CFBundle bundle (path); 2136 CFReleaser<CFURLRef> url(bundle.CopyExecutableURL ()); 2137 if (url.get()) 2138 { 2139 if (::CFURLGetFileSystemRepresentation (url.get(), true, (UInt8*)resolved_path, resolved_path_size)) 2140 return true; 2141 } 2142 } 2143 } 2144 2145 if (realpath(path, max_path)) 2146 { 2147 // Found the path relatively... 2148 ::strncpy(resolved_path, max_path, resolved_path_size); 2149 return strlen(resolved_path) + 1 < resolved_path_size; 2150 } 2151 else 2152 { 2153 // Not a relative path, check the PATH environment variable if the 2154 const char *PATH = getenv("PATH"); 2155 if (PATH) 2156 { 2157 const char *curr_path_start = PATH; 2158 const char *curr_path_end; 2159 while (curr_path_start && *curr_path_start) 2160 { 2161 curr_path_end = strchr(curr_path_start, ':'); 2162 if (curr_path_end == NULL) 2163 { 2164 result.assign(curr_path_start); 2165 curr_path_start = NULL; 2166 } 2167 else if (curr_path_end > curr_path_start) 2168 { 2169 size_t len = curr_path_end - curr_path_start; 2170 result.assign(curr_path_start, len); 2171 curr_path_start += len + 1; 2172 } 2173 else 2174 break; 2175 2176 result += '/'; 2177 result += path; 2178 struct stat s; 2179 if (stat(result.c_str(), &s) == 0) 2180 { 2181 ::strncpy(resolved_path, result.c_str(), resolved_path_size); 2182 return result.size() + 1 < resolved_path_size; 2183 } 2184 } 2185 } 2186 } 2187 return false; 2188 } 2189 2190 2191 void 2192 DNBInitialize() 2193 { 2194 DNBLogThreadedIf (LOG_PROCESS, "DNBInitialize ()"); 2195 #if defined (__i386__) || defined (__x86_64__) 2196 DNBArchImplI386::Initialize(); 2197 DNBArchImplX86_64::Initialize(); 2198 #elif defined (__arm__) || defined (__arm64__) 2199 DNBArchMachARM::Initialize(); 2200 DNBArchMachARM64::Initialize(); 2201 #endif 2202 } 2203 2204 void 2205 DNBTerminate() 2206 { 2207 } 2208 2209 nub_bool_t 2210 DNBSetArchitecture (const char *arch) 2211 { 2212 if (arch && arch[0]) 2213 { 2214 if (strcasecmp (arch, "i386") == 0) 2215 return DNBArchProtocol::SetArchitecture (CPU_TYPE_I386); 2216 else if ((strcasecmp (arch, "x86_64") == 0) || (strcasecmp (arch, "x86_64h") == 0)) 2217 return DNBArchProtocol::SetArchitecture (CPU_TYPE_X86_64); 2218 else if (strstr (arch, "arm64") == arch || strstr (arch, "armv8") == arch) 2219 return DNBArchProtocol::SetArchitecture (CPU_TYPE_ARM64); 2220 else if (strstr (arch, "arm") == arch) 2221 return DNBArchProtocol::SetArchitecture (CPU_TYPE_ARM); 2222 } 2223 return false; 2224 } 2225