]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/API/SBProcess.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / API / SBProcess.cpp
1 //===-- SBProcess.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 #include "lldb/API/SBProcess.h"
11
12 // C Includes
13 #include <inttypes.h>
14
15 #include "lldb/lldb-defines.h"
16 #include "lldb/lldb-types.h"
17
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Core/State.h"
22 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Target/MemoryRegionInfo.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/SystemRuntime.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/Thread.h"
29 #include "lldb/Utility/Args.h"
30 #include "lldb/Utility/Log.h"
31 #include "lldb/Utility/Stream.h"
32
33 // Project includes
34
35 #include "lldb/API/SBBroadcaster.h"
36 #include "lldb/API/SBCommandReturnObject.h"
37 #include "lldb/API/SBDebugger.h"
38 #include "lldb/API/SBEvent.h"
39 #include "lldb/API/SBFileSpec.h"
40 #include "lldb/API/SBMemoryRegionInfo.h"
41 #include "lldb/API/SBMemoryRegionInfoList.h"
42 #include "lldb/API/SBStream.h"
43 #include "lldb/API/SBStringList.h"
44 #include "lldb/API/SBStructuredData.h"
45 #include "lldb/API/SBThread.h"
46 #include "lldb/API/SBThreadCollection.h"
47 #include "lldb/API/SBTrace.h"
48 #include "lldb/API/SBTraceOptions.h"
49 #include "lldb/API/SBUnixSignals.h"
50
51 using namespace lldb;
52 using namespace lldb_private;
53
54 SBProcess::SBProcess() : m_opaque_wp() {}
55
56 //----------------------------------------------------------------------
57 // SBProcess constructor
58 //----------------------------------------------------------------------
59
60 SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
61
62 SBProcess::SBProcess(const lldb::ProcessSP &process_sp)
63     : m_opaque_wp(process_sp) {}
64
65 const SBProcess &SBProcess::operator=(const SBProcess &rhs) {
66   if (this != &rhs)
67     m_opaque_wp = rhs.m_opaque_wp;
68   return *this;
69 }
70
71 //----------------------------------------------------------------------
72 // Destructor
73 //----------------------------------------------------------------------
74 SBProcess::~SBProcess() {}
75
76 const char *SBProcess::GetBroadcasterClassName() {
77   return Process::GetStaticBroadcasterClass().AsCString();
78 }
79
80 const char *SBProcess::GetPluginName() {
81   ProcessSP process_sp(GetSP());
82   if (process_sp) {
83     return process_sp->GetPluginName().GetCString();
84   }
85   return "<Unknown>";
86 }
87
88 const char *SBProcess::GetShortPluginName() {
89   ProcessSP process_sp(GetSP());
90   if (process_sp) {
91     return process_sp->GetPluginName().GetCString();
92   }
93   return "<Unknown>";
94 }
95
96 lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }
97
98 void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
99
100 void SBProcess::Clear() { m_opaque_wp.reset(); }
101
102 bool SBProcess::IsValid() const {
103   ProcessSP process_sp(m_opaque_wp.lock());
104   return ((bool)process_sp && process_sp->IsValid());
105 }
106
107 bool SBProcess::RemoteLaunch(char const **argv, char const **envp,
108                              const char *stdin_path, const char *stdout_path,
109                              const char *stderr_path,
110                              const char *working_directory,
111                              uint32_t launch_flags, bool stop_at_entry,
112                              lldb::SBError &error) {
113   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
114   if (log)
115     log->Printf("SBProcess(%p)::RemoteLaunch (argv=%p, envp=%p, stdin=%s, "
116                 "stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, "
117                 "stop_at_entry=%i, &error (%p))...",
118                 static_cast<void *>(m_opaque_wp.lock().get()),
119                 static_cast<void *>(argv), static_cast<void *>(envp),
120                 stdin_path ? stdin_path : "NULL",
121                 stdout_path ? stdout_path : "NULL",
122                 stderr_path ? stderr_path : "NULL",
123                 working_directory ? working_directory : "NULL", launch_flags,
124                 stop_at_entry, static_cast<void *>(error.get()));
125
126   ProcessSP process_sp(GetSP());
127   if (process_sp) {
128     std::lock_guard<std::recursive_mutex> guard(
129         process_sp->GetTarget().GetAPIMutex());
130     if (process_sp->GetState() == eStateConnected) {
131       if (stop_at_entry)
132         launch_flags |= eLaunchFlagStopAtEntry;
133       ProcessLaunchInfo launch_info(
134           FileSpec{stdin_path, false}, FileSpec{stdout_path, false},
135           FileSpec{stderr_path, false}, FileSpec{working_directory, false},
136           launch_flags);
137       Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
138       if (exe_module)
139         launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
140       if (argv)
141         launch_info.GetArguments().AppendArguments(argv);
142       if (envp)
143         launch_info.GetEnvironment() = Environment(envp);
144       error.SetError(process_sp->Launch(launch_info));
145     } else {
146       error.SetErrorString("must be in eStateConnected to call RemoteLaunch");
147     }
148   } else {
149     error.SetErrorString("unable to attach pid");
150   }
151
152   if (log) {
153     SBStream sstr;
154     error.GetDescription(sstr);
155     log->Printf("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s",
156                 static_cast<void *>(process_sp.get()),
157                 static_cast<void *>(error.get()), sstr.GetData());
158   }
159
160   return error.Success();
161 }
162
163 bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
164                                             lldb::SBError &error) {
165   ProcessSP process_sp(GetSP());
166   if (process_sp) {
167     std::lock_guard<std::recursive_mutex> guard(
168         process_sp->GetTarget().GetAPIMutex());
169     if (process_sp->GetState() == eStateConnected) {
170       ProcessAttachInfo attach_info;
171       attach_info.SetProcessID(pid);
172       error.SetError(process_sp->Attach(attach_info));
173     } else {
174       error.SetErrorString(
175           "must be in eStateConnected to call RemoteAttachToProcessWithID");
176     }
177   } else {
178     error.SetErrorString("unable to attach pid");
179   }
180
181   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
182   if (log) {
183     SBStream sstr;
184     error.GetDescription(sstr);
185     log->Printf("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64
186                 ") => SBError (%p): %s",
187                 static_cast<void *>(process_sp.get()), pid,
188                 static_cast<void *>(error.get()), sstr.GetData());
189   }
190
191   return error.Success();
192 }
193
194 uint32_t SBProcess::GetNumThreads() {
195   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
196
197   uint32_t num_threads = 0;
198   ProcessSP process_sp(GetSP());
199   if (process_sp) {
200     Process::StopLocker stop_locker;
201
202     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
203     std::lock_guard<std::recursive_mutex> guard(
204         process_sp->GetTarget().GetAPIMutex());
205     num_threads = process_sp->GetThreadList().GetSize(can_update);
206   }
207
208   if (log)
209     log->Printf("SBProcess(%p)::GetNumThreads () => %d",
210                 static_cast<void *>(process_sp.get()), num_threads);
211
212   return num_threads;
213 }
214
215 SBThread SBProcess::GetSelectedThread() const {
216   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
217
218   SBThread sb_thread;
219   ThreadSP thread_sp;
220   ProcessSP process_sp(GetSP());
221   if (process_sp) {
222     std::lock_guard<std::recursive_mutex> guard(
223         process_sp->GetTarget().GetAPIMutex());
224     thread_sp = process_sp->GetThreadList().GetSelectedThread();
225     sb_thread.SetThread(thread_sp);
226   }
227
228   if (log)
229     log->Printf("SBProcess(%p)::GetSelectedThread () => SBThread(%p)",
230                 static_cast<void *>(process_sp.get()),
231                 static_cast<void *>(thread_sp.get()));
232
233   return sb_thread;
234 }
235
236 SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
237                                          lldb::addr_t context) {
238   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
239
240   SBThread sb_thread;
241   ThreadSP thread_sp;
242   ProcessSP process_sp(GetSP());
243   if (process_sp) {
244     std::lock_guard<std::recursive_mutex> guard(
245         process_sp->GetTarget().GetAPIMutex());
246     thread_sp = process_sp->CreateOSPluginThread(tid, context);
247     sb_thread.SetThread(thread_sp);
248   }
249
250   if (log)
251     log->Printf("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64
252                 ", context=0x%" PRIx64 ") => SBThread(%p)",
253                 static_cast<void *>(process_sp.get()), tid, context,
254                 static_cast<void *>(thread_sp.get()));
255
256   return sb_thread;
257 }
258
259 SBTarget SBProcess::GetTarget() const {
260   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
261
262   SBTarget sb_target;
263   TargetSP target_sp;
264   ProcessSP process_sp(GetSP());
265   if (process_sp) {
266     target_sp = process_sp->GetTarget().shared_from_this();
267     sb_target.SetSP(target_sp);
268   }
269
270   if (log)
271     log->Printf("SBProcess(%p)::GetTarget () => SBTarget(%p)",
272                 static_cast<void *>(process_sp.get()),
273                 static_cast<void *>(target_sp.get()));
274
275   return sb_target;
276 }
277
278 size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
279   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
280
281   size_t ret_val = 0;
282   ProcessSP process_sp(GetSP());
283   if (process_sp) {
284     Status error;
285     ret_val = process_sp->PutSTDIN(src, src_len, error);
286   }
287
288   if (log)
289     log->Printf("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%" PRIu64
290                 ") => %" PRIu64,
291                 static_cast<void *>(process_sp.get()), src,
292                 static_cast<uint64_t>(src_len), static_cast<uint64_t>(ret_val));
293
294   return ret_val;
295 }
296
297 size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
298   size_t bytes_read = 0;
299   ProcessSP process_sp(GetSP());
300   if (process_sp) {
301     Status error;
302     bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);
303   }
304
305   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
306   if (log)
307     log->Printf(
308         "SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64
309         ") => %" PRIu64,
310         static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
311         dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
312
313   return bytes_read;
314 }
315
316 size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
317   size_t bytes_read = 0;
318   ProcessSP process_sp(GetSP());
319   if (process_sp) {
320     Status error;
321     bytes_read = process_sp->GetSTDERR(dst, dst_len, error);
322   }
323
324   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
325   if (log)
326     log->Printf(
327         "SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64
328         ") => %" PRIu64,
329         static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
330         dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
331
332   return bytes_read;
333 }
334
335 size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
336   size_t bytes_read = 0;
337   ProcessSP process_sp(GetSP());
338   if (process_sp) {
339     Status error;
340     bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);
341   }
342
343   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
344   if (log)
345     log->Printf(
346         "SBProcess(%p)::GetAsyncProfileData (dst=\"%.*s\", dst_len=%" PRIu64
347         ") => %" PRIu64,
348         static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
349         dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
350
351   return bytes_read;
352 }
353
354 lldb::SBTrace SBProcess::StartTrace(SBTraceOptions &options,
355                                     lldb::SBError &error) {
356   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
357   ProcessSP process_sp(GetSP());
358   error.Clear();
359   SBTrace trace_instance;
360   trace_instance.SetSP(process_sp);
361   lldb::user_id_t uid = LLDB_INVALID_UID;
362
363   if (!process_sp) {
364     error.SetErrorString("invalid process");
365   } else {
366     uid = process_sp->StartTrace(*(options.m_traceoptions_sp), error.ref());
367     trace_instance.SetTraceUID(uid);
368     LLDB_LOG(log, "SBProcess::returned uid - {0}", uid);
369   }
370   return trace_instance;
371 }
372
373 void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
374   if (out == NULL)
375     return;
376
377   ProcessSP process_sp(GetSP());
378   if (process_sp) {
379     const StateType event_state = SBProcess::GetStateFromEvent(event);
380     char message[1024];
381     int message_len = ::snprintf(
382         message, sizeof(message), "Process %" PRIu64 " %s\n",
383         process_sp->GetID(), SBDebugger::StateAsCString(event_state));
384
385     if (message_len > 0)
386       ::fwrite(message, 1, message_len, out);
387   }
388 }
389
390 void SBProcess::AppendEventStateReport(const SBEvent &event,
391                                        SBCommandReturnObject &result) {
392   ProcessSP process_sp(GetSP());
393   if (process_sp) {
394     const StateType event_state = SBProcess::GetStateFromEvent(event);
395     char message[1024];
396     ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",
397                process_sp->GetID(), SBDebugger::StateAsCString(event_state));
398
399     result.AppendMessage(message);
400   }
401 }
402
403 bool SBProcess::SetSelectedThread(const SBThread &thread) {
404   ProcessSP process_sp(GetSP());
405   if (process_sp) {
406     std::lock_guard<std::recursive_mutex> guard(
407         process_sp->GetTarget().GetAPIMutex());
408     return process_sp->GetThreadList().SetSelectedThreadByID(
409         thread.GetThreadID());
410   }
411   return false;
412 }
413
414 bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
415   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
416
417   bool ret_val = false;
418   ProcessSP process_sp(GetSP());
419   if (process_sp) {
420     std::lock_guard<std::recursive_mutex> guard(
421         process_sp->GetTarget().GetAPIMutex());
422     ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);
423   }
424
425   if (log)
426     log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64
427                 ") => %s",
428                 static_cast<void *>(process_sp.get()), tid,
429                 (ret_val ? "true" : "false"));
430
431   return ret_val;
432 }
433
434 bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
435   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
436
437   bool ret_val = false;
438   ProcessSP process_sp(GetSP());
439   if (process_sp) {
440     std::lock_guard<std::recursive_mutex> guard(
441         process_sp->GetTarget().GetAPIMutex());
442     ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);
443   }
444
445   if (log)
446     log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
447                 static_cast<void *>(process_sp.get()), index_id,
448                 (ret_val ? "true" : "false"));
449
450   return ret_val;
451 }
452
453 SBThread SBProcess::GetThreadAtIndex(size_t index) {
454   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
455
456   SBThread sb_thread;
457   ThreadSP thread_sp;
458   ProcessSP process_sp(GetSP());
459   if (process_sp) {
460     Process::StopLocker stop_locker;
461     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
462     std::lock_guard<std::recursive_mutex> guard(
463         process_sp->GetTarget().GetAPIMutex());
464     thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
465     sb_thread.SetThread(thread_sp);
466   }
467
468   if (log)
469     log->Printf("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
470                 static_cast<void *>(process_sp.get()),
471                 static_cast<uint32_t>(index),
472                 static_cast<void *>(thread_sp.get()));
473
474   return sb_thread;
475 }
476
477 uint32_t SBProcess::GetNumQueues() {
478   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
479
480   uint32_t num_queues = 0;
481   ProcessSP process_sp(GetSP());
482   if (process_sp) {
483     Process::StopLocker stop_locker;
484     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
485       std::lock_guard<std::recursive_mutex> guard(
486           process_sp->GetTarget().GetAPIMutex());
487       num_queues = process_sp->GetQueueList().GetSize();
488     }
489   }
490
491   if (log)
492     log->Printf("SBProcess(%p)::GetNumQueues () => %d",
493                 static_cast<void *>(process_sp.get()), num_queues);
494
495   return num_queues;
496 }
497
498 SBQueue SBProcess::GetQueueAtIndex(size_t index) {
499   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
500
501   SBQueue sb_queue;
502   QueueSP queue_sp;
503   ProcessSP process_sp(GetSP());
504   if (process_sp) {
505     Process::StopLocker stop_locker;
506     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
507       std::lock_guard<std::recursive_mutex> guard(
508           process_sp->GetTarget().GetAPIMutex());
509       queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
510       sb_queue.SetQueue(queue_sp);
511     }
512   }
513
514   if (log)
515     log->Printf("SBProcess(%p)::GetQueueAtIndex (index=%d) => SBQueue(%p)",
516                 static_cast<void *>(process_sp.get()),
517                 static_cast<uint32_t>(index),
518                 static_cast<void *>(queue_sp.get()));
519
520   return sb_queue;
521 }
522
523 uint32_t SBProcess::GetStopID(bool include_expression_stops) {
524   ProcessSP process_sp(GetSP());
525   if (process_sp) {
526     std::lock_guard<std::recursive_mutex> guard(
527         process_sp->GetTarget().GetAPIMutex());
528     if (include_expression_stops)
529       return process_sp->GetStopID();
530     else
531       return process_sp->GetLastNaturalStopID();
532   }
533   return 0;
534 }
535
536 SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
537   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
538
539   SBEvent sb_event;
540   EventSP event_sp;
541   ProcessSP process_sp(GetSP());
542   if (process_sp) {
543     std::lock_guard<std::recursive_mutex> guard(
544         process_sp->GetTarget().GetAPIMutex());
545     event_sp = process_sp->GetStopEventForStopID(stop_id);
546     sb_event.reset(event_sp);
547   }
548
549   if (log)
550     log->Printf("SBProcess(%p)::GetStopEventForStopID (stop_id=%" PRIu32
551                 ") => SBEvent(%p)",
552                 static_cast<void *>(process_sp.get()), stop_id,
553                 static_cast<void *>(event_sp.get()));
554
555   return sb_event;
556 }
557
558 StateType SBProcess::GetState() {
559
560   StateType ret_val = eStateInvalid;
561   ProcessSP process_sp(GetSP());
562   if (process_sp) {
563     std::lock_guard<std::recursive_mutex> guard(
564         process_sp->GetTarget().GetAPIMutex());
565     ret_val = process_sp->GetState();
566   }
567
568   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
569   if (log)
570     log->Printf("SBProcess(%p)::GetState () => %s",
571                 static_cast<void *>(process_sp.get()),
572                 lldb_private::StateAsCString(ret_val));
573
574   return ret_val;
575 }
576
577 int SBProcess::GetExitStatus() {
578   int exit_status = 0;
579   ProcessSP process_sp(GetSP());
580   if (process_sp) {
581     std::lock_guard<std::recursive_mutex> guard(
582         process_sp->GetTarget().GetAPIMutex());
583     exit_status = process_sp->GetExitStatus();
584   }
585   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
586   if (log)
587     log->Printf("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
588                 static_cast<void *>(process_sp.get()), exit_status,
589                 exit_status);
590
591   return exit_status;
592 }
593
594 const char *SBProcess::GetExitDescription() {
595   const char *exit_desc = NULL;
596   ProcessSP process_sp(GetSP());
597   if (process_sp) {
598     std::lock_guard<std::recursive_mutex> guard(
599         process_sp->GetTarget().GetAPIMutex());
600     exit_desc = process_sp->GetExitDescription();
601   }
602   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
603   if (log)
604     log->Printf("SBProcess(%p)::GetExitDescription () => %s",
605                 static_cast<void *>(process_sp.get()), exit_desc);
606   return exit_desc;
607 }
608
609 lldb::pid_t SBProcess::GetProcessID() {
610   lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
611   ProcessSP process_sp(GetSP());
612   if (process_sp)
613     ret_val = process_sp->GetID();
614
615   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
616   if (log)
617     log->Printf("SBProcess(%p)::GetProcessID () => %" PRIu64,
618                 static_cast<void *>(process_sp.get()), ret_val);
619
620   return ret_val;
621 }
622
623 uint32_t SBProcess::GetUniqueID() {
624   uint32_t ret_val = 0;
625   ProcessSP process_sp(GetSP());
626   if (process_sp)
627     ret_val = process_sp->GetUniqueID();
628   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
629   if (log)
630     log->Printf("SBProcess(%p)::GetUniqueID () => %" PRIu32,
631                 static_cast<void *>(process_sp.get()), ret_val);
632   return ret_val;
633 }
634
635 ByteOrder SBProcess::GetByteOrder() const {
636   ByteOrder byteOrder = eByteOrderInvalid;
637   ProcessSP process_sp(GetSP());
638   if (process_sp)
639     byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
640
641   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
642   if (log)
643     log->Printf("SBProcess(%p)::GetByteOrder () => %d",
644                 static_cast<void *>(process_sp.get()), byteOrder);
645
646   return byteOrder;
647 }
648
649 uint32_t SBProcess::GetAddressByteSize() const {
650   uint32_t size = 0;
651   ProcessSP process_sp(GetSP());
652   if (process_sp)
653     size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
654
655   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
656   if (log)
657     log->Printf("SBProcess(%p)::GetAddressByteSize () => %d",
658                 static_cast<void *>(process_sp.get()), size);
659
660   return size;
661 }
662
663 SBError SBProcess::Continue() {
664   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
665
666   SBError sb_error;
667   ProcessSP process_sp(GetSP());
668
669   if (log)
670     log->Printf("SBProcess(%p)::Continue ()...",
671                 static_cast<void *>(process_sp.get()));
672
673   if (process_sp) {
674     std::lock_guard<std::recursive_mutex> guard(
675         process_sp->GetTarget().GetAPIMutex());
676
677     if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
678       sb_error.ref() = process_sp->Resume();
679     else
680       sb_error.ref() = process_sp->ResumeSynchronous(NULL);
681   } else
682     sb_error.SetErrorString("SBProcess is invalid");
683
684   if (log) {
685     SBStream sstr;
686     sb_error.GetDescription(sstr);
687     log->Printf("SBProcess(%p)::Continue () => SBError (%p): %s",
688                 static_cast<void *>(process_sp.get()),
689                 static_cast<void *>(sb_error.get()), sstr.GetData());
690   }
691
692   return sb_error;
693 }
694
695 SBError SBProcess::Destroy() {
696   SBError sb_error;
697   ProcessSP process_sp(GetSP());
698   if (process_sp) {
699     std::lock_guard<std::recursive_mutex> guard(
700         process_sp->GetTarget().GetAPIMutex());
701     sb_error.SetError(process_sp->Destroy(false));
702   } else
703     sb_error.SetErrorString("SBProcess is invalid");
704
705   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
706   if (log) {
707     SBStream sstr;
708     sb_error.GetDescription(sstr);
709     log->Printf("SBProcess(%p)::Destroy () => SBError (%p): %s",
710                 static_cast<void *>(process_sp.get()),
711                 static_cast<void *>(sb_error.get()), sstr.GetData());
712   }
713
714   return sb_error;
715 }
716
717 SBError SBProcess::Stop() {
718   SBError sb_error;
719   ProcessSP process_sp(GetSP());
720   if (process_sp) {
721     std::lock_guard<std::recursive_mutex> guard(
722         process_sp->GetTarget().GetAPIMutex());
723     sb_error.SetError(process_sp->Halt());
724   } else
725     sb_error.SetErrorString("SBProcess is invalid");
726
727   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
728   if (log) {
729     SBStream sstr;
730     sb_error.GetDescription(sstr);
731     log->Printf("SBProcess(%p)::Stop () => SBError (%p): %s",
732                 static_cast<void *>(process_sp.get()),
733                 static_cast<void *>(sb_error.get()), sstr.GetData());
734   }
735
736   return sb_error;
737 }
738
739 SBError SBProcess::Kill() {
740   SBError sb_error;
741   ProcessSP process_sp(GetSP());
742   if (process_sp) {
743     std::lock_guard<std::recursive_mutex> guard(
744         process_sp->GetTarget().GetAPIMutex());
745     sb_error.SetError(process_sp->Destroy(true));
746   } else
747     sb_error.SetErrorString("SBProcess is invalid");
748
749   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
750   if (log) {
751     SBStream sstr;
752     sb_error.GetDescription(sstr);
753     log->Printf("SBProcess(%p)::Kill () => SBError (%p): %s",
754                 static_cast<void *>(process_sp.get()),
755                 static_cast<void *>(sb_error.get()), sstr.GetData());
756   }
757
758   return sb_error;
759 }
760
761 SBError SBProcess::Detach() {
762   // FIXME: This should come from a process default.
763   bool keep_stopped = false;
764   return Detach(keep_stopped);
765 }
766
767 SBError SBProcess::Detach(bool keep_stopped) {
768   SBError sb_error;
769   ProcessSP process_sp(GetSP());
770   if (process_sp) {
771     std::lock_guard<std::recursive_mutex> guard(
772         process_sp->GetTarget().GetAPIMutex());
773     sb_error.SetError(process_sp->Detach(keep_stopped));
774   } else
775     sb_error.SetErrorString("SBProcess is invalid");
776
777   return sb_error;
778 }
779
780 SBError SBProcess::Signal(int signo) {
781   SBError sb_error;
782   ProcessSP process_sp(GetSP());
783   if (process_sp) {
784     std::lock_guard<std::recursive_mutex> guard(
785         process_sp->GetTarget().GetAPIMutex());
786     sb_error.SetError(process_sp->Signal(signo));
787   } else
788     sb_error.SetErrorString("SBProcess is invalid");
789   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
790   if (log) {
791     SBStream sstr;
792     sb_error.GetDescription(sstr);
793     log->Printf("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
794                 static_cast<void *>(process_sp.get()), signo,
795                 static_cast<void *>(sb_error.get()), sstr.GetData());
796   }
797   return sb_error;
798 }
799
800 SBUnixSignals SBProcess::GetUnixSignals() {
801   if (auto process_sp = GetSP())
802     return SBUnixSignals{process_sp};
803
804   return {};
805 }
806
807 void SBProcess::SendAsyncInterrupt() {
808   ProcessSP process_sp(GetSP());
809   if (process_sp) {
810     process_sp->SendAsyncInterrupt();
811   }
812 }
813
814 SBThread SBProcess::GetThreadByID(tid_t tid) {
815   SBThread sb_thread;
816   ThreadSP thread_sp;
817   ProcessSP process_sp(GetSP());
818   if (process_sp) {
819     Process::StopLocker stop_locker;
820     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
821     std::lock_guard<std::recursive_mutex> guard(
822         process_sp->GetTarget().GetAPIMutex());
823     thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);
824     sb_thread.SetThread(thread_sp);
825   }
826
827   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
828   if (log)
829     log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64
830                 ") => SBThread (%p)",
831                 static_cast<void *>(process_sp.get()), tid,
832                 static_cast<void *>(thread_sp.get()));
833
834   return sb_thread;
835 }
836
837 SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
838   SBThread sb_thread;
839   ThreadSP thread_sp;
840   ProcessSP process_sp(GetSP());
841   if (process_sp) {
842     Process::StopLocker stop_locker;
843     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
844     std::lock_guard<std::recursive_mutex> guard(
845         process_sp->GetTarget().GetAPIMutex());
846     thread_sp =
847         process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);
848     sb_thread.SetThread(thread_sp);
849   }
850
851   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
852   if (log)
853     log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
854                 static_cast<void *>(process_sp.get()), index_id,
855                 static_cast<void *>(thread_sp.get()));
856
857   return sb_thread;
858 }
859
860 StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
861   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
862
863   StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
864
865   if (log)
866     log->Printf("SBProcess::GetStateFromEvent (event.sp=%p) => %s",
867                 static_cast<void *>(event.get()),
868                 lldb_private::StateAsCString(ret_val));
869
870   return ret_val;
871 }
872
873 bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
874   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
875
876   bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
877
878   if (log)
879     log->Printf("SBProcess::%s (event.sp=%p) => %d", __FUNCTION__,
880                 static_cast<void *>(event.get()), ret_val);
881
882   return ret_val;
883 }
884
885 size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
886   return Process::ProcessEventData::GetNumRestartedReasons(event.get());
887 }
888
889 const char *
890 SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
891                                               size_t idx) {
892   return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
893 }
894
895 SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
896   ProcessSP process_sp =
897       Process::ProcessEventData::GetProcessFromEvent(event.get());
898   if (!process_sp) {
899     // StructuredData events also know the process they come from. Try that.
900     process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
901   }
902
903   return SBProcess(process_sp);
904 }
905
906 bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
907   return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
908 }
909
910 lldb::SBStructuredData
911 SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
912   return SBStructuredData(event.GetSP());
913 }
914
915 bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
916   return (event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass()) &&
917          !EventIsStructuredDataEvent(event);
918 }
919
920 bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
921   EventSP event_sp = event.GetSP();
922   EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
923   return event_data && (event_data->GetFlavor() ==
924                         EventDataStructuredData::GetFlavorString());
925 }
926
927 SBBroadcaster SBProcess::GetBroadcaster() const {
928   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
929
930   ProcessSP process_sp(GetSP());
931
932   SBBroadcaster broadcaster(process_sp.get(), false);
933
934   if (log)
935     log->Printf("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",
936                 static_cast<void *>(process_sp.get()),
937                 static_cast<void *>(broadcaster.get()));
938
939   return broadcaster;
940 }
941
942 const char *SBProcess::GetBroadcasterClass() {
943   return Process::GetStaticBroadcasterClass().AsCString();
944 }
945
946 size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
947                              SBError &sb_error) {
948   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
949
950   size_t bytes_read = 0;
951
952   ProcessSP process_sp(GetSP());
953
954   if (log)
955     log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
956                 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
957                 static_cast<void *>(process_sp.get()), addr,
958                 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
959                 static_cast<void *>(sb_error.get()));
960
961   if (process_sp) {
962     Process::StopLocker stop_locker;
963     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
964       std::lock_guard<std::recursive_mutex> guard(
965           process_sp->GetTarget().GetAPIMutex());
966       bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
967     } else {
968       if (log)
969         log->Printf("SBProcess(%p)::ReadMemory() => error: process is running",
970                     static_cast<void *>(process_sp.get()));
971       sb_error.SetErrorString("process is running");
972     }
973   } else {
974     sb_error.SetErrorString("SBProcess is invalid");
975   }
976
977   if (log) {
978     SBStream sstr;
979     sb_error.GetDescription(sstr);
980     log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
981                 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
982                 static_cast<void *>(process_sp.get()), addr,
983                 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
984                 static_cast<void *>(sb_error.get()), sstr.GetData(),
985                 static_cast<uint64_t>(bytes_read));
986   }
987
988   return bytes_read;
989 }
990
991 size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
992                                         lldb::SBError &sb_error) {
993   size_t bytes_read = 0;
994   ProcessSP process_sp(GetSP());
995   if (process_sp) {
996     Process::StopLocker stop_locker;
997     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
998       std::lock_guard<std::recursive_mutex> guard(
999           process_sp->GetTarget().GetAPIMutex());
1000       bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
1001                                                      sb_error.ref());
1002     } else {
1003       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1004       if (log)
1005         log->Printf("SBProcess(%p)::ReadCStringFromMemory() => error: process "
1006                     "is running",
1007                     static_cast<void *>(process_sp.get()));
1008       sb_error.SetErrorString("process is running");
1009     }
1010   } else {
1011     sb_error.SetErrorString("SBProcess is invalid");
1012   }
1013   return bytes_read;
1014 }
1015
1016 uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
1017                                            lldb::SBError &sb_error) {
1018   uint64_t value = 0;
1019   ProcessSP process_sp(GetSP());
1020   if (process_sp) {
1021     Process::StopLocker stop_locker;
1022     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1023       std::lock_guard<std::recursive_mutex> guard(
1024           process_sp->GetTarget().GetAPIMutex());
1025       value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
1026                                                         sb_error.ref());
1027     } else {
1028       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1029       if (log)
1030         log->Printf("SBProcess(%p)::ReadUnsignedFromMemory() => error: process "
1031                     "is running",
1032                     static_cast<void *>(process_sp.get()));
1033       sb_error.SetErrorString("process is running");
1034     }
1035   } else {
1036     sb_error.SetErrorString("SBProcess is invalid");
1037   }
1038   return value;
1039 }
1040
1041 lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
1042                                               lldb::SBError &sb_error) {
1043   lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1044   ProcessSP process_sp(GetSP());
1045   if (process_sp) {
1046     Process::StopLocker stop_locker;
1047     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1048       std::lock_guard<std::recursive_mutex> guard(
1049           process_sp->GetTarget().GetAPIMutex());
1050       ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
1051     } else {
1052       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1053       if (log)
1054         log->Printf("SBProcess(%p)::ReadPointerFromMemory() => error: process "
1055                     "is running",
1056                     static_cast<void *>(process_sp.get()));
1057       sb_error.SetErrorString("process is running");
1058     }
1059   } else {
1060     sb_error.SetErrorString("SBProcess is invalid");
1061   }
1062   return ptr;
1063 }
1064
1065 size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
1066                               SBError &sb_error) {
1067   size_t bytes_written = 0;
1068
1069   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1070
1071   ProcessSP process_sp(GetSP());
1072
1073   if (log)
1074     log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1075                 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1076                 static_cast<void *>(process_sp.get()), addr,
1077                 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1078                 static_cast<void *>(sb_error.get()));
1079
1080   if (process_sp) {
1081     Process::StopLocker stop_locker;
1082     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1083       std::lock_guard<std::recursive_mutex> guard(
1084           process_sp->GetTarget().GetAPIMutex());
1085       bytes_written =
1086           process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
1087     } else {
1088       if (log)
1089         log->Printf("SBProcess(%p)::WriteMemory() => error: process is running",
1090                     static_cast<void *>(process_sp.get()));
1091       sb_error.SetErrorString("process is running");
1092     }
1093   }
1094
1095   if (log) {
1096     SBStream sstr;
1097     sb_error.GetDescription(sstr);
1098     log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1099                 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1100                 static_cast<void *>(process_sp.get()), addr,
1101                 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1102                 static_cast<void *>(sb_error.get()), sstr.GetData(),
1103                 static_cast<uint64_t>(bytes_written));
1104   }
1105
1106   return bytes_written;
1107 }
1108
1109 bool SBProcess::GetDescription(SBStream &description) {
1110   Stream &strm = description.ref();
1111
1112   ProcessSP process_sp(GetSP());
1113   if (process_sp) {
1114     char path[PATH_MAX];
1115     GetTarget().GetExecutable().GetPath(path, sizeof(path));
1116     Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1117     const char *exe_name = NULL;
1118     if (exe_module)
1119       exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1120
1121     strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1122                 process_sp->GetID(), lldb_private::StateAsCString(GetState()),
1123                 GetNumThreads(), exe_name ? ", executable = " : "",
1124                 exe_name ? exe_name : "");
1125   } else
1126     strm.PutCString("No value");
1127
1128   return true;
1129 }
1130
1131 uint32_t
1132 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1133   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1134
1135   uint32_t num = 0;
1136   ProcessSP process_sp(GetSP());
1137   if (process_sp) {
1138     std::lock_guard<std::recursive_mutex> guard(
1139         process_sp->GetTarget().GetAPIMutex());
1140     sb_error.SetError(process_sp->GetWatchpointSupportInfo(num));
1141     if (log)
1142       log->Printf("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1143                   static_cast<void *>(process_sp.get()), num);
1144   } else {
1145     sb_error.SetErrorString("SBProcess is invalid");
1146   }
1147   return num;
1148 }
1149
1150 uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
1151                               lldb::SBError &sb_error) {
1152   return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
1153 }
1154
1155 uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
1156                               const lldb::SBFileSpec &sb_remote_image_spec,
1157                               lldb::SBError &sb_error) {
1158   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1159   ProcessSP process_sp(GetSP());
1160   if (process_sp) {
1161     Process::StopLocker stop_locker;
1162     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1163       if (log)
1164         log->Printf("SBProcess(%p)::LoadImage() => calling Platform::LoadImage"
1165                     "for: %s",
1166                     static_cast<void *>(process_sp.get()),
1167                     sb_local_image_spec.GetFilename());
1168
1169       std::lock_guard<std::recursive_mutex> guard(
1170         process_sp->GetTarget().GetAPIMutex());
1171       PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1172       return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
1173                                     *sb_remote_image_spec, sb_error.ref());
1174     } else {
1175       if (log)
1176         log->Printf("SBProcess(%p)::LoadImage() => error: process is running",
1177                     static_cast<void *>(process_sp.get()));
1178       sb_error.SetErrorString("process is running");
1179     }
1180   } else { 
1181     if (log)
1182       log->Printf("SBProcess(%p)::LoadImage() => error: called with invalid"
1183                     " process",
1184                     static_cast<void *>(process_sp.get()));
1185     sb_error.SetErrorString("process is invalid");
1186   }
1187   return LLDB_INVALID_IMAGE_TOKEN;
1188 }
1189
1190 uint32_t SBProcess::LoadImageUsingPaths(const lldb::SBFileSpec &image_spec,
1191                                         SBStringList &paths,
1192                                         lldb::SBFileSpec &loaded_path, 
1193                                         lldb::SBError &error) {
1194   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1195   ProcessSP process_sp(GetSP());
1196   if (process_sp) {
1197     Process::StopLocker stop_locker;
1198     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1199       if (log)
1200         log->Printf("SBProcess(%p)::LoadImageUsingPaths() => "
1201                     "calling Platform::LoadImageUsingPaths for: %s",
1202                     static_cast<void *>(process_sp.get()),
1203                     image_spec.GetFilename());
1204
1205       std::lock_guard<std::recursive_mutex> guard(
1206         process_sp->GetTarget().GetAPIMutex());
1207       PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1208       size_t num_paths = paths.GetSize();
1209       std::vector<std::string> paths_vec;
1210       paths_vec.reserve(num_paths);
1211       for (size_t i = 0; i < num_paths; i++)
1212         paths_vec.push_back(paths.GetStringAtIndex(i));
1213       FileSpec loaded_spec;
1214       
1215       uint32_t token = platform_sp->LoadImageUsingPaths(process_sp.get(),
1216                                                         *image_spec, 
1217                                                         paths_vec, 
1218                                                         error.ref(), 
1219                                                         &loaded_spec);
1220        if (token != LLDB_INVALID_IMAGE_TOKEN)
1221           loaded_path = loaded_spec;
1222        return token;
1223     } else {
1224       if (log)
1225         log->Printf("SBProcess(%p)::LoadImageUsingPaths() => error: "
1226                     "process is running",
1227                     static_cast<void *>(process_sp.get()));
1228       error.SetErrorString("process is running");
1229     }
1230   } else { 
1231     if (log)
1232       log->Printf("SBProcess(%p)::LoadImageUsingPaths() => error: "
1233                    "called with invalid process",
1234                     static_cast<void *>(process_sp.get()));
1235     error.SetErrorString("process is invalid");
1236   }
1237   
1238   return LLDB_INVALID_IMAGE_TOKEN;
1239 }
1240
1241 lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1242   lldb::SBError sb_error;
1243   ProcessSP process_sp(GetSP());
1244   if (process_sp) {
1245     Process::StopLocker stop_locker;
1246     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1247       std::lock_guard<std::recursive_mutex> guard(
1248           process_sp->GetTarget().GetAPIMutex());
1249       PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1250       sb_error.SetError(
1251           platform_sp->UnloadImage(process_sp.get(), image_token));
1252     } else {
1253       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1254       if (log)
1255         log->Printf("SBProcess(%p)::UnloadImage() => error: process is running",
1256                     static_cast<void *>(process_sp.get()));
1257       sb_error.SetErrorString("process is running");
1258     }
1259   } else
1260     sb_error.SetErrorString("invalid process");
1261   return sb_error;
1262 }
1263
1264 lldb::SBError SBProcess::SendEventData(const char *event_data) {
1265   lldb::SBError sb_error;
1266   ProcessSP process_sp(GetSP());
1267   if (process_sp) {
1268     Process::StopLocker stop_locker;
1269     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1270       std::lock_guard<std::recursive_mutex> guard(
1271           process_sp->GetTarget().GetAPIMutex());
1272       sb_error.SetError(process_sp->SendEventData(event_data));
1273     } else {
1274       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1275       if (log)
1276         log->Printf(
1277             "SBProcess(%p)::SendEventData() => error: process is running",
1278             static_cast<void *>(process_sp.get()));
1279       sb_error.SetErrorString("process is running");
1280     }
1281   } else
1282     sb_error.SetErrorString("invalid process");
1283   return sb_error;
1284 }
1285
1286 uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1287   ProcessSP process_sp(GetSP());
1288   if (process_sp && process_sp->GetSystemRuntime()) {
1289     SystemRuntime *runtime = process_sp->GetSystemRuntime();
1290     return runtime->GetExtendedBacktraceTypes().size();
1291   }
1292   return 0;
1293 }
1294
1295 const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1296   ProcessSP process_sp(GetSP());
1297   if (process_sp && process_sp->GetSystemRuntime()) {
1298     SystemRuntime *runtime = process_sp->GetSystemRuntime();
1299     const std::vector<ConstString> &names =
1300         runtime->GetExtendedBacktraceTypes();
1301     if (idx < names.size()) {
1302       return names[idx].AsCString();
1303     } else {
1304       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1305       if (log)
1306         log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => "
1307                     "error: requested extended backtrace name out of bounds",
1308                     static_cast<void *>(process_sp.get()));
1309     }
1310   }
1311   return NULL;
1312 }
1313
1314 SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1315   ProcessSP process_sp(GetSP());
1316   SBThreadCollection threads;
1317   if (process_sp) {
1318     threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1319   }
1320   return threads;
1321 }
1322
1323 bool SBProcess::IsInstrumentationRuntimePresent(
1324     InstrumentationRuntimeType type) {
1325   ProcessSP process_sp(GetSP());
1326   if (!process_sp)
1327     return false;
1328
1329   InstrumentationRuntimeSP runtime_sp =
1330       process_sp->GetInstrumentationRuntime(type);
1331
1332   if (!runtime_sp.get())
1333     return false;
1334
1335   return runtime_sp->IsActive();
1336 }
1337
1338 lldb::SBError SBProcess::SaveCore(const char *file_name) {
1339   lldb::SBError error;
1340   ProcessSP process_sp(GetSP());
1341   if (!process_sp) {
1342     error.SetErrorString("SBProcess is invalid");
1343     return error;
1344   }
1345
1346   std::lock_guard<std::recursive_mutex> guard(
1347       process_sp->GetTarget().GetAPIMutex());
1348
1349   if (process_sp->GetState() != eStateStopped) {
1350     error.SetErrorString("the process is not stopped");
1351     return error;
1352   }
1353
1354   FileSpec core_file(file_name, false);
1355   error.ref() = PluginManager::SaveCore(process_sp, core_file);
1356   return error;
1357 }
1358
1359 lldb::SBError
1360 SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1361                                SBMemoryRegionInfo &sb_region_info) {
1362   lldb::SBError sb_error;
1363   ProcessSP process_sp(GetSP());
1364   MemoryRegionInfoSP region_info_sp =
1365       std::make_shared<lldb_private::MemoryRegionInfo>();
1366   if (process_sp) {
1367     Process::StopLocker stop_locker;
1368     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1369       std::lock_guard<std::recursive_mutex> guard(
1370           process_sp->GetTarget().GetAPIMutex());
1371       sb_error.ref() =
1372           process_sp->GetMemoryRegionInfo(load_addr, *region_info_sp);
1373       if (sb_error.Success()) {
1374         sb_region_info.ref() = *region_info_sp;
1375       }
1376     } else {
1377       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1378       if (log)
1379         log->Printf(
1380             "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1381             static_cast<void *>(process_sp.get()));
1382       sb_error.SetErrorString("process is running");
1383     }
1384   } else {
1385     sb_error.SetErrorString("SBProcess is invalid");
1386   }
1387   return sb_error;
1388 }
1389
1390 lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1391   lldb::SBError sb_error;
1392   lldb::SBMemoryRegionInfoList sb_region_list;
1393   ProcessSP process_sp(GetSP());
1394   if (process_sp) {
1395     Process::StopLocker stop_locker;
1396     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1397       std::lock_guard<std::recursive_mutex> guard(
1398           process_sp->GetTarget().GetAPIMutex());
1399       std::vector<MemoryRegionInfoSP> region_list;
1400       sb_error.ref() = process_sp->GetMemoryRegions(region_list);
1401       if (sb_error.Success()) {
1402         std::vector<MemoryRegionInfoSP>::iterator end = region_list.end();
1403         for (std::vector<MemoryRegionInfoSP>::iterator it = region_list.begin();
1404              it != end; it++) {
1405           SBMemoryRegionInfo sb_region_info(it->get());
1406           sb_region_list.Append(sb_region_info);
1407         }
1408       }
1409     } else {
1410       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1411       if (log)
1412         log->Printf(
1413             "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1414             static_cast<void *>(process_sp.get()));
1415       sb_error.SetErrorString("process is running");
1416     }
1417   } else {
1418     sb_error.SetErrorString("SBProcess is invalid");
1419   }
1420   return sb_region_list;
1421 }
1422
1423 lldb::SBProcessInfo SBProcess::GetProcessInfo() {
1424   lldb::SBProcessInfo sb_proc_info;
1425   ProcessSP process_sp(GetSP());
1426   ProcessInstanceInfo proc_info;
1427   if (process_sp && process_sp->GetProcessInfo(proc_info)) {
1428     sb_proc_info.SetProcessInfo(proc_info);
1429   }
1430   return sb_proc_info;
1431 }