SBProcess.cpp revision 327952
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/Interpreter/Args.h"
24#include "lldb/Target/MemoryRegionInfo.h"
25#include "lldb/Target/Process.h"
26#include "lldb/Target/RegisterContext.h"
27#include "lldb/Target/SystemRuntime.h"
28#include "lldb/Target/Target.h"
29#include "lldb/Target/Thread.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
51using namespace lldb;
52using namespace lldb_private;
53
54SBProcess::SBProcess() : m_opaque_wp() {}
55
56//----------------------------------------------------------------------
57// SBProcess constructor
58//----------------------------------------------------------------------
59
60SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
61
62SBProcess::SBProcess(const lldb::ProcessSP &process_sp)
63    : m_opaque_wp(process_sp) {}
64
65const 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//----------------------------------------------------------------------
74SBProcess::~SBProcess() {}
75
76const char *SBProcess::GetBroadcasterClassName() {
77  return Process::GetStaticBroadcasterClass().AsCString();
78}
79
80const char *SBProcess::GetPluginName() {
81  ProcessSP process_sp(GetSP());
82  if (process_sp) {
83    return process_sp->GetPluginName().GetCString();
84  }
85  return "<Unknown>";
86}
87
88const char *SBProcess::GetShortPluginName() {
89  ProcessSP process_sp(GetSP());
90  if (process_sp) {
91    return process_sp->GetPluginName().GetCString();
92  }
93  return "<Unknown>";
94}
95
96lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }
97
98void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
99
100void SBProcess::Clear() { m_opaque_wp.reset(); }
101
102bool SBProcess::IsValid() const {
103  ProcessSP process_sp(m_opaque_wp.lock());
104  return ((bool)process_sp && process_sp->IsValid());
105}
106
107bool 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.GetEnvironmentEntries().SetArguments(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
163bool 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
194uint32_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
215SBThread 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
236SBThread 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
259SBTarget 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
278size_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
297size_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
316size_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
335size_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
354lldb::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
373void 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
390void 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
403bool 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
414bool 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
434bool 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
453SBThread 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
477uint32_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
498SBQueue 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
523uint32_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
536SBEvent 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
558StateType 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
577int 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
594const 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
609lldb::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
623uint32_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
635ByteOrder 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
649uint32_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
663SBError 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
695SBError 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
717SBError 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
739SBError 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
761SBError SBProcess::Detach() {
762  // FIXME: This should come from a process default.
763  bool keep_stopped = false;
764  return Detach(keep_stopped);
765}
766
767SBError 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
780SBError 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
800SBUnixSignals SBProcess::GetUnixSignals() {
801  if (auto process_sp = GetSP())
802    return SBUnixSignals{process_sp};
803
804  return {};
805}
806
807void SBProcess::SendAsyncInterrupt() {
808  ProcessSP process_sp(GetSP());
809  if (process_sp) {
810    process_sp->SendAsyncInterrupt();
811  }
812}
813
814SBThread 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
837SBThread 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
860StateType 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
873bool 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
885size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
886  return Process::ProcessEventData::GetNumRestartedReasons(event.get());
887}
888
889const char *
890SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
891                                              size_t idx) {
892  return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
893}
894
895SBProcess 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.
900    // Try that.
901    process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
902  }
903
904  return SBProcess(process_sp);
905}
906
907bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
908  return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
909}
910
911lldb::SBStructuredData
912SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
913  return SBStructuredData(event.GetSP());
914}
915
916bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
917  return (event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass()) &&
918         !EventIsStructuredDataEvent(event);
919}
920
921bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
922  EventSP event_sp = event.GetSP();
923  EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
924  return event_data && (event_data->GetFlavor() ==
925                        EventDataStructuredData::GetFlavorString());
926}
927
928SBBroadcaster SBProcess::GetBroadcaster() const {
929  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
930
931  ProcessSP process_sp(GetSP());
932
933  SBBroadcaster broadcaster(process_sp.get(), false);
934
935  if (log)
936    log->Printf("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",
937                static_cast<void *>(process_sp.get()),
938                static_cast<void *>(broadcaster.get()));
939
940  return broadcaster;
941}
942
943const char *SBProcess::GetBroadcasterClass() {
944  return Process::GetStaticBroadcasterClass().AsCString();
945}
946
947size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
948                             SBError &sb_error) {
949  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
950
951  size_t bytes_read = 0;
952
953  ProcessSP process_sp(GetSP());
954
955  if (log)
956    log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
957                ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
958                static_cast<void *>(process_sp.get()), addr,
959                static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
960                static_cast<void *>(sb_error.get()));
961
962  if (process_sp) {
963    Process::StopLocker stop_locker;
964    if (stop_locker.TryLock(&process_sp->GetRunLock())) {
965      std::lock_guard<std::recursive_mutex> guard(
966          process_sp->GetTarget().GetAPIMutex());
967      bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
968    } else {
969      if (log)
970        log->Printf("SBProcess(%p)::ReadMemory() => error: process is running",
971                    static_cast<void *>(process_sp.get()));
972      sb_error.SetErrorString("process is running");
973    }
974  } else {
975    sb_error.SetErrorString("SBProcess is invalid");
976  }
977
978  if (log) {
979    SBStream sstr;
980    sb_error.GetDescription(sstr);
981    log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
982                ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
983                static_cast<void *>(process_sp.get()), addr,
984                static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
985                static_cast<void *>(sb_error.get()), sstr.GetData(),
986                static_cast<uint64_t>(bytes_read));
987  }
988
989  return bytes_read;
990}
991
992size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
993                                        lldb::SBError &sb_error) {
994  size_t bytes_read = 0;
995  ProcessSP process_sp(GetSP());
996  if (process_sp) {
997    Process::StopLocker stop_locker;
998    if (stop_locker.TryLock(&process_sp->GetRunLock())) {
999      std::lock_guard<std::recursive_mutex> guard(
1000          process_sp->GetTarget().GetAPIMutex());
1001      bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
1002                                                     sb_error.ref());
1003    } else {
1004      Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1005      if (log)
1006        log->Printf("SBProcess(%p)::ReadCStringFromMemory() => error: process "
1007                    "is running",
1008                    static_cast<void *>(process_sp.get()));
1009      sb_error.SetErrorString("process is running");
1010    }
1011  } else {
1012    sb_error.SetErrorString("SBProcess is invalid");
1013  }
1014  return bytes_read;
1015}
1016
1017uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
1018                                           lldb::SBError &sb_error) {
1019  uint64_t value = 0;
1020  ProcessSP process_sp(GetSP());
1021  if (process_sp) {
1022    Process::StopLocker stop_locker;
1023    if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1024      std::lock_guard<std::recursive_mutex> guard(
1025          process_sp->GetTarget().GetAPIMutex());
1026      value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
1027                                                        sb_error.ref());
1028    } else {
1029      Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1030      if (log)
1031        log->Printf("SBProcess(%p)::ReadUnsignedFromMemory() => error: process "
1032                    "is running",
1033                    static_cast<void *>(process_sp.get()));
1034      sb_error.SetErrorString("process is running");
1035    }
1036  } else {
1037    sb_error.SetErrorString("SBProcess is invalid");
1038  }
1039  return value;
1040}
1041
1042lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
1043                                              lldb::SBError &sb_error) {
1044  lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1045  ProcessSP process_sp(GetSP());
1046  if (process_sp) {
1047    Process::StopLocker stop_locker;
1048    if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1049      std::lock_guard<std::recursive_mutex> guard(
1050          process_sp->GetTarget().GetAPIMutex());
1051      ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
1052    } else {
1053      Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1054      if (log)
1055        log->Printf("SBProcess(%p)::ReadPointerFromMemory() => error: process "
1056                    "is running",
1057                    static_cast<void *>(process_sp.get()));
1058      sb_error.SetErrorString("process is running");
1059    }
1060  } else {
1061    sb_error.SetErrorString("SBProcess is invalid");
1062  }
1063  return ptr;
1064}
1065
1066size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
1067                              SBError &sb_error) {
1068  size_t bytes_written = 0;
1069
1070  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1071
1072  ProcessSP process_sp(GetSP());
1073
1074  if (log)
1075    log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1076                ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1077                static_cast<void *>(process_sp.get()), addr,
1078                static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1079                static_cast<void *>(sb_error.get()));
1080
1081  if (process_sp) {
1082    Process::StopLocker stop_locker;
1083    if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1084      std::lock_guard<std::recursive_mutex> guard(
1085          process_sp->GetTarget().GetAPIMutex());
1086      bytes_written =
1087          process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
1088    } else {
1089      if (log)
1090        log->Printf("SBProcess(%p)::WriteMemory() => error: process is running",
1091                    static_cast<void *>(process_sp.get()));
1092      sb_error.SetErrorString("process is running");
1093    }
1094  }
1095
1096  if (log) {
1097    SBStream sstr;
1098    sb_error.GetDescription(sstr);
1099    log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1100                ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1101                static_cast<void *>(process_sp.get()), addr,
1102                static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1103                static_cast<void *>(sb_error.get()), sstr.GetData(),
1104                static_cast<uint64_t>(bytes_written));
1105  }
1106
1107  return bytes_written;
1108}
1109
1110bool SBProcess::GetDescription(SBStream &description) {
1111  Stream &strm = description.ref();
1112
1113  ProcessSP process_sp(GetSP());
1114  if (process_sp) {
1115    char path[PATH_MAX];
1116    GetTarget().GetExecutable().GetPath(path, sizeof(path));
1117    Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1118    const char *exe_name = NULL;
1119    if (exe_module)
1120      exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1121
1122    strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1123                process_sp->GetID(), lldb_private::StateAsCString(GetState()),
1124                GetNumThreads(), exe_name ? ", executable = " : "",
1125                exe_name ? exe_name : "");
1126  } else
1127    strm.PutCString("No value");
1128
1129  return true;
1130}
1131
1132uint32_t
1133SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1134  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1135
1136  uint32_t num = 0;
1137  ProcessSP process_sp(GetSP());
1138  if (process_sp) {
1139    std::lock_guard<std::recursive_mutex> guard(
1140        process_sp->GetTarget().GetAPIMutex());
1141    sb_error.SetError(process_sp->GetWatchpointSupportInfo(num));
1142    if (log)
1143      log->Printf("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1144                  static_cast<void *>(process_sp.get()), num);
1145  } else {
1146    sb_error.SetErrorString("SBProcess is invalid");
1147  }
1148  return num;
1149}
1150
1151uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
1152                              lldb::SBError &sb_error) {
1153  return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
1154}
1155
1156uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
1157                              const lldb::SBFileSpec &sb_remote_image_spec,
1158                              lldb::SBError &sb_error) {
1159  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1160  ProcessSP process_sp(GetSP());
1161  if (process_sp) {
1162    Process::StopLocker stop_locker;
1163    if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1164      if (log)
1165        log->Printf("SBProcess(%p)::LoadImage() => calling Platform::LoadImage"
1166                    "for: %s",
1167                    static_cast<void *>(process_sp.get()),
1168                    sb_local_image_spec.GetFilename());
1169
1170      std::lock_guard<std::recursive_mutex> guard(
1171        process_sp->GetTarget().GetAPIMutex());
1172      PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1173      return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
1174                                    *sb_remote_image_spec, sb_error.ref());
1175    } else {
1176      if (log)
1177        log->Printf("SBProcess(%p)::LoadImage() => error: process is running",
1178                    static_cast<void *>(process_sp.get()));
1179      sb_error.SetErrorString("process is running");
1180    }
1181  } else {
1182    if (log)
1183      log->Printf("SBProcess(%p)::LoadImage() => error: called with invalid"
1184                    " process",
1185                    static_cast<void *>(process_sp.get()));
1186    sb_error.SetErrorString("process is invalid");
1187  }
1188  return LLDB_INVALID_IMAGE_TOKEN;
1189}
1190
1191lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1192  lldb::SBError sb_error;
1193  ProcessSP process_sp(GetSP());
1194  if (process_sp) {
1195    Process::StopLocker stop_locker;
1196    if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1197      std::lock_guard<std::recursive_mutex> guard(
1198          process_sp->GetTarget().GetAPIMutex());
1199      PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1200      sb_error.SetError(
1201          platform_sp->UnloadImage(process_sp.get(), image_token));
1202    } else {
1203      Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1204      if (log)
1205        log->Printf("SBProcess(%p)::UnloadImage() => error: process is running",
1206                    static_cast<void *>(process_sp.get()));
1207      sb_error.SetErrorString("process is running");
1208    }
1209  } else
1210    sb_error.SetErrorString("invalid process");
1211  return sb_error;
1212}
1213
1214lldb::SBError SBProcess::SendEventData(const char *event_data) {
1215  lldb::SBError sb_error;
1216  ProcessSP process_sp(GetSP());
1217  if (process_sp) {
1218    Process::StopLocker stop_locker;
1219    if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1220      std::lock_guard<std::recursive_mutex> guard(
1221          process_sp->GetTarget().GetAPIMutex());
1222      sb_error.SetError(process_sp->SendEventData(event_data));
1223    } else {
1224      Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1225      if (log)
1226        log->Printf(
1227            "SBProcess(%p)::SendEventData() => error: process is running",
1228            static_cast<void *>(process_sp.get()));
1229      sb_error.SetErrorString("process is running");
1230    }
1231  } else
1232    sb_error.SetErrorString("invalid process");
1233  return sb_error;
1234}
1235
1236uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1237  ProcessSP process_sp(GetSP());
1238  if (process_sp && process_sp->GetSystemRuntime()) {
1239    SystemRuntime *runtime = process_sp->GetSystemRuntime();
1240    return runtime->GetExtendedBacktraceTypes().size();
1241  }
1242  return 0;
1243}
1244
1245const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1246  ProcessSP process_sp(GetSP());
1247  if (process_sp && process_sp->GetSystemRuntime()) {
1248    SystemRuntime *runtime = process_sp->GetSystemRuntime();
1249    const std::vector<ConstString> &names =
1250        runtime->GetExtendedBacktraceTypes();
1251    if (idx < names.size()) {
1252      return names[idx].AsCString();
1253    } else {
1254      Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1255      if (log)
1256        log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => "
1257                    "error: requested extended backtrace name out of bounds",
1258                    static_cast<void *>(process_sp.get()));
1259    }
1260  }
1261  return NULL;
1262}
1263
1264SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1265  ProcessSP process_sp(GetSP());
1266  SBThreadCollection threads;
1267  if (process_sp) {
1268    threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1269  }
1270  return threads;
1271}
1272
1273bool SBProcess::IsInstrumentationRuntimePresent(
1274    InstrumentationRuntimeType type) {
1275  ProcessSP process_sp(GetSP());
1276  if (!process_sp)
1277    return false;
1278
1279  InstrumentationRuntimeSP runtime_sp =
1280      process_sp->GetInstrumentationRuntime(type);
1281
1282  if (!runtime_sp.get())
1283    return false;
1284
1285  return runtime_sp->IsActive();
1286}
1287
1288lldb::SBError SBProcess::SaveCore(const char *file_name) {
1289  lldb::SBError error;
1290  ProcessSP process_sp(GetSP());
1291  if (!process_sp) {
1292    error.SetErrorString("SBProcess is invalid");
1293    return error;
1294  }
1295
1296  std::lock_guard<std::recursive_mutex> guard(
1297      process_sp->GetTarget().GetAPIMutex());
1298
1299  if (process_sp->GetState() != eStateStopped) {
1300    error.SetErrorString("the process is not stopped");
1301    return error;
1302  }
1303
1304  FileSpec core_file(file_name, false);
1305  error.ref() = PluginManager::SaveCore(process_sp, core_file);
1306  return error;
1307}
1308
1309lldb::SBError
1310SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1311                               SBMemoryRegionInfo &sb_region_info) {
1312  lldb::SBError sb_error;
1313  ProcessSP process_sp(GetSP());
1314  MemoryRegionInfoSP region_info_sp =
1315      std::make_shared<lldb_private::MemoryRegionInfo>();
1316  if (process_sp) {
1317    Process::StopLocker stop_locker;
1318    if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1319      std::lock_guard<std::recursive_mutex> guard(
1320          process_sp->GetTarget().GetAPIMutex());
1321      sb_error.ref() =
1322          process_sp->GetMemoryRegionInfo(load_addr, *region_info_sp);
1323      if (sb_error.Success()) {
1324        sb_region_info.ref() = *region_info_sp;
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_error;
1338}
1339
1340lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1341  lldb::SBError sb_error;
1342  lldb::SBMemoryRegionInfoList sb_region_list;
1343  ProcessSP process_sp(GetSP());
1344  if (process_sp) {
1345    Process::StopLocker stop_locker;
1346    if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1347      std::lock_guard<std::recursive_mutex> guard(
1348          process_sp->GetTarget().GetAPIMutex());
1349      std::vector<MemoryRegionInfoSP> region_list;
1350      sb_error.ref() = process_sp->GetMemoryRegions(region_list);
1351      if (sb_error.Success()) {
1352        std::vector<MemoryRegionInfoSP>::iterator end = region_list.end();
1353        for (std::vector<MemoryRegionInfoSP>::iterator it = region_list.begin();
1354             it != end; it++) {
1355          SBMemoryRegionInfo sb_region_info(it->get());
1356          sb_region_list.Append(sb_region_info);
1357        }
1358      }
1359    } else {
1360      Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1361      if (log)
1362        log->Printf(
1363            "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1364            static_cast<void *>(process_sp.get()));
1365      sb_error.SetErrorString("process is running");
1366    }
1367  } else {
1368    sb_error.SetErrorString("SBProcess is invalid");
1369  }
1370  return sb_region_list;
1371}
1372
1373lldb::SBProcessInfo SBProcess::GetProcessInfo() {
1374  lldb::SBProcessInfo sb_proc_info;
1375  ProcessSP process_sp(GetSP());
1376  ProcessInstanceInfo proc_info;
1377  if (process_sp && process_sp->GetProcessInfo(proc_info)) {
1378    sb_proc_info.SetProcessInfo(proc_info);
1379  }
1380  return sb_proc_info;
1381}
1382