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