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