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