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