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