blob: f98670e07ae39d2ef6c6c5e79fc0d425bf6e27cc [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Eli Friedman4c5de692010-06-09 07:44:37 +000010#include "lldb/API/SBProcess.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Virgile Bellobdae3782013-08-28 12:14:27 +000012// C Includes
13#include <inttypes.h>
14
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/lldb-defines.h"
16#include "lldb/lldb-types.h"
17
Greg Clayton5d5028b2010-10-06 03:53:16 +000018#include "lldb/Core/Debugger.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/Module.h"
Adrian McCarthyf7d18932015-11-20 23:09:11 +000020#include "lldb/Core/PluginManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/StreamFile.h"
Howard Hellyer260368432016-06-23 08:35:37 +000023#include "lldb/Target/MemoryRegionInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Target/Process.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/RegisterContext.h"
Jason Molenda8c713372013-11-05 11:00:35 +000026#include "lldb/Target/SystemRuntime.h"
Greg Clayton66111032010-06-23 01:19:29 +000027#include "lldb/Target/Target.h"
28#include "lldb/Target/Thread.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000029#include "lldb/Utility/Args.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000030#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000031#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
33// Project includes
34
Eli Friedman4c5de692010-06-09 07:44:37 +000035#include "lldb/API/SBBroadcaster.h"
Eli Friedman4c5de692010-06-09 07:44:37 +000036#include "lldb/API/SBCommandReturnObject.h"
Greg Clayton0e615682012-02-24 05:03:03 +000037#include "lldb/API/SBDebugger.h"
Eli Friedman4c5de692010-06-09 07:44:37 +000038#include "lldb/API/SBEvent.h"
Greg Clayton0e615682012-02-24 05:03:03 +000039#include "lldb/API/SBFileSpec.h"
Howard Hellyer260368432016-06-23 08:35:37 +000040#include "lldb/API/SBMemoryRegionInfo.h"
41#include "lldb/API/SBMemoryRegionInfoList.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000042#include "lldb/API/SBStream.h"
43#include "lldb/API/SBStringList.h"
Todd Fiala75930012016-08-19 04:21:48 +000044#include "lldb/API/SBStructuredData.h"
Eli Friedman4c5de692010-06-09 07:44:37 +000045#include "lldb/API/SBThread.h"
Kuba Breckaa51ea382014-09-06 01:33:13 +000046#include "lldb/API/SBThreadCollection.h"
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000047#include "lldb/API/SBTrace.h"
48#include "lldb/API/SBTraceOptions.h"
Todd Fiala802dc4022014-06-23 19:30:49 +000049#include "lldb/API/SBUnixSignals.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
51using namespace lldb;
52using namespace lldb_private;
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054SBProcess::SBProcess() : m_opaque_wp() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
56//----------------------------------------------------------------------
57// SBProcess constructor
58//----------------------------------------------------------------------
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062SBProcess::SBProcess(const lldb::ProcessSP &process_sp)
63 : m_opaque_wp(process_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
Kate Stoneb9c1b512016-09-06 20:57:50 +000065const SBProcess &SBProcess::operator=(const SBProcess &rhs) {
66 if (this != &rhs)
67 m_opaque_wp = rhs.m_opaque_wp;
68 return *this;
Greg Claytonefabb122010-11-05 23:17:00 +000069}
70
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071//----------------------------------------------------------------------
72// Destructor
73//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000074SBProcess::~SBProcess() {}
75
76const char *SBProcess::GetBroadcasterClassName() {
77 return Process::GetStaticBroadcasterClass().AsCString();
78}
79
80const char *SBProcess::GetPluginName() {
81 ProcessSP process_sp(GetSP());
82 if (process_sp) {
83 return process_sp->GetPluginName().GetCString();
84 }
85 return "<Unknown>";
86}
87
88const char *SBProcess::GetShortPluginName() {
89 ProcessSP process_sp(GetSP());
90 if (process_sp) {
91 return process_sp->GetPluginName().GetCString();
92 }
93 return "<Unknown>";
94}
95
96lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }
97
98void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
99
100void SBProcess::Clear() { m_opaque_wp.reset(); }
101
102bool SBProcess::IsValid() const {
103 ProcessSP process_sp(m_opaque_wp.lock());
104 return ((bool)process_sp && process_sp->IsValid());
105}
106
107bool SBProcess::RemoteLaunch(char const **argv, char const **envp,
108 const char *stdin_path, const char *stdout_path,
109 const char *stderr_path,
110 const char *working_directory,
111 uint32_t launch_flags, bool stop_at_entry,
112 lldb::SBError &error) {
113 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
114 if (log)
115 log->Printf("SBProcess(%p)::RemoteLaunch (argv=%p, envp=%p, stdin=%s, "
116 "stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, "
117 "stop_at_entry=%i, &error (%p))...",
118 static_cast<void *>(m_opaque_wp.lock().get()),
119 static_cast<void *>(argv), static_cast<void *>(envp),
120 stdin_path ? stdin_path : "NULL",
121 stdout_path ? stdout_path : "NULL",
122 stderr_path ? stderr_path : "NULL",
123 working_directory ? working_directory : "NULL", launch_flags,
124 stop_at_entry, static_cast<void *>(error.get()));
125
126 ProcessSP process_sp(GetSP());
127 if (process_sp) {
128 std::lock_guard<std::recursive_mutex> guard(
129 process_sp->GetTarget().GetAPIMutex());
130 if (process_sp->GetState() == eStateConnected) {
131 if (stop_at_entry)
132 launch_flags |= eLaunchFlagStopAtEntry;
133 ProcessLaunchInfo launch_info(
134 FileSpec{stdin_path, false}, FileSpec{stdout_path, false},
135 FileSpec{stderr_path, false}, FileSpec{working_directory, false},
136 launch_flags);
137 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
138 if (exe_module)
139 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
140 if (argv)
141 launch_info.GetArguments().AppendArguments(argv);
142 if (envp)
Pavel Labath62930e52018-01-10 11:57:31 +0000143 launch_info.GetEnvironment() = Environment(envp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 error.SetError(process_sp->Launch(launch_info));
145 } else {
146 error.SetErrorString("must be in eStateConnected to call RemoteLaunch");
147 }
148 } else {
149 error.SetErrorString("unable to attach pid");
150 }
151
152 if (log) {
153 SBStream sstr;
154 error.GetDescription(sstr);
155 log->Printf("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s",
156 static_cast<void *>(process_sp.get()),
157 static_cast<void *>(error.get()), sstr.GetData());
158 }
159
160 return error.Success();
161}
162
163bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
164 lldb::SBError &error) {
165 ProcessSP process_sp(GetSP());
166 if (process_sp) {
167 std::lock_guard<std::recursive_mutex> guard(
168 process_sp->GetTarget().GetAPIMutex());
169 if (process_sp->GetState() == eStateConnected) {
170 ProcessAttachInfo attach_info;
171 attach_info.SetProcessID(pid);
172 error.SetError(process_sp->Attach(attach_info));
173 } else {
174 error.SetErrorString(
175 "must be in eStateConnected to call RemoteAttachToProcessWithID");
176 }
177 } else {
178 error.SetErrorString("unable to attach pid");
179 }
180
181 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
182 if (log) {
183 SBStream sstr;
184 error.GetDescription(sstr);
185 log->Printf("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64
186 ") => SBError (%p): %s",
187 static_cast<void *>(process_sp.get()), pid,
188 static_cast<void *>(error.get()), sstr.GetData());
189 }
190
191 return error.Success();
192}
193
194uint32_t SBProcess::GetNumThreads() {
195 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
196
197 uint32_t num_threads = 0;
198 ProcessSP process_sp(GetSP());
199 if (process_sp) {
200 Process::StopLocker stop_locker;
201
202 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
203 std::lock_guard<std::recursive_mutex> guard(
204 process_sp->GetTarget().GetAPIMutex());
205 num_threads = process_sp->GetThreadList().GetSize(can_update);
206 }
207
208 if (log)
209 log->Printf("SBProcess(%p)::GetNumThreads () => %d",
210 static_cast<void *>(process_sp.get()), num_threads);
211
212 return num_threads;
213}
214
215SBThread SBProcess::GetSelectedThread() const {
216 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
217
218 SBThread sb_thread;
219 ThreadSP thread_sp;
220 ProcessSP process_sp(GetSP());
221 if (process_sp) {
222 std::lock_guard<std::recursive_mutex> guard(
223 process_sp->GetTarget().GetAPIMutex());
224 thread_sp = process_sp->GetThreadList().GetSelectedThread();
225 sb_thread.SetThread(thread_sp);
226 }
227
228 if (log)
229 log->Printf("SBProcess(%p)::GetSelectedThread () => SBThread(%p)",
230 static_cast<void *>(process_sp.get()),
231 static_cast<void *>(thread_sp.get()));
232
233 return sb_thread;
234}
235
236SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
237 lldb::addr_t context) {
238 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
239
240 SBThread sb_thread;
241 ThreadSP thread_sp;
242 ProcessSP process_sp(GetSP());
243 if (process_sp) {
244 std::lock_guard<std::recursive_mutex> guard(
245 process_sp->GetTarget().GetAPIMutex());
246 thread_sp = process_sp->CreateOSPluginThread(tid, context);
247 sb_thread.SetThread(thread_sp);
248 }
249
250 if (log)
251 log->Printf("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64
252 ", context=0x%" PRIx64 ") => SBThread(%p)",
253 static_cast<void *>(process_sp.get()), tid, context,
254 static_cast<void *>(thread_sp.get()));
255
256 return sb_thread;
257}
258
259SBTarget SBProcess::GetTarget() const {
260 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
261
262 SBTarget sb_target;
263 TargetSP target_sp;
264 ProcessSP process_sp(GetSP());
265 if (process_sp) {
266 target_sp = process_sp->GetTarget().shared_from_this();
267 sb_target.SetSP(target_sp);
268 }
269
270 if (log)
271 log->Printf("SBProcess(%p)::GetTarget () => SBTarget(%p)",
272 static_cast<void *>(process_sp.get()),
273 static_cast<void *>(target_sp.get()));
274
275 return sb_target;
276}
277
278size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
279 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
280
281 size_t ret_val = 0;
282 ProcessSP process_sp(GetSP());
283 if (process_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000284 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 ret_val = process_sp->PutSTDIN(src, src_len, error);
286 }
287
288 if (log)
289 log->Printf("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%" PRIu64
290 ") => %" PRIu64,
291 static_cast<void *>(process_sp.get()), src,
292 static_cast<uint64_t>(src_len), static_cast<uint64_t>(ret_val));
293
294 return ret_val;
295}
296
297size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
298 size_t bytes_read = 0;
299 ProcessSP process_sp(GetSP());
300 if (process_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000301 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);
303 }
304
305 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
306 if (log)
307 log->Printf(
308 "SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64
309 ") => %" PRIu64,
310 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
311 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
312
313 return bytes_read;
314}
315
316size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
317 size_t bytes_read = 0;
318 ProcessSP process_sp(GetSP());
319 if (process_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000320 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 bytes_read = process_sp->GetSTDERR(dst, dst_len, error);
322 }
323
324 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
325 if (log)
326 log->Printf(
327 "SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64
328 ") => %" PRIu64,
329 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
330 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
331
332 return bytes_read;
333}
334
335size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
336 size_t bytes_read = 0;
337 ProcessSP process_sp(GetSP());
338 if (process_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +0000339 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340 bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);
341 }
342
343 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
344 if (log)
345 log->Printf(
346 "SBProcess(%p)::GetAsyncProfileData (dst=\"%.*s\", dst_len=%" PRIu64
347 ") => %" PRIu64,
348 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
349 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
350
351 return bytes_read;
352}
353
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +0000354lldb::SBTrace SBProcess::StartTrace(SBTraceOptions &options,
355 lldb::SBError &error) {
356 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
357 ProcessSP process_sp(GetSP());
358 error.Clear();
359 SBTrace trace_instance;
360 trace_instance.SetSP(process_sp);
361 lldb::user_id_t uid = LLDB_INVALID_UID;
362
363 if (!process_sp) {
364 error.SetErrorString("invalid process");
365 } else {
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +0000366 uid = process_sp->StartTrace(*(options.m_traceoptions_sp), error.ref());
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +0000367 trace_instance.SetTraceUID(uid);
Ravitheja Addepallye714c4f2017-05-26 11:46:27 +0000368 LLDB_LOG(log, "SBProcess::returned uid - {0}", uid);
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +0000369 }
370 return trace_instance;
371}
372
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
374 if (out == NULL)
375 return;
376
377 ProcessSP process_sp(GetSP());
378 if (process_sp) {
379 const StateType event_state = SBProcess::GetStateFromEvent(event);
380 char message[1024];
381 int message_len = ::snprintf(
382 message, sizeof(message), "Process %" PRIu64 " %s\n",
383 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
384
385 if (message_len > 0)
386 ::fwrite(message, 1, message_len, out);
387 }
388}
389
390void SBProcess::AppendEventStateReport(const SBEvent &event,
391 SBCommandReturnObject &result) {
392 ProcessSP process_sp(GetSP());
393 if (process_sp) {
394 const StateType event_state = SBProcess::GetStateFromEvent(event);
395 char message[1024];
396 ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",
397 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
398
399 result.AppendMessage(message);
400 }
401}
402
403bool SBProcess::SetSelectedThread(const SBThread &thread) {
404 ProcessSP process_sp(GetSP());
405 if (process_sp) {
406 std::lock_guard<std::recursive_mutex> guard(
407 process_sp->GetTarget().GetAPIMutex());
408 return process_sp->GetThreadList().SetSelectedThreadByID(
409 thread.GetThreadID());
410 }
411 return false;
412}
413
414bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
415 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
416
417 bool ret_val = false;
418 ProcessSP process_sp(GetSP());
419 if (process_sp) {
420 std::lock_guard<std::recursive_mutex> guard(
421 process_sp->GetTarget().GetAPIMutex());
422 ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);
423 }
424
425 if (log)
426 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64
427 ") => %s",
428 static_cast<void *>(process_sp.get()), tid,
429 (ret_val ? "true" : "false"));
430
431 return ret_val;
432}
433
434bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
435 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
436
437 bool ret_val = false;
438 ProcessSP process_sp(GetSP());
439 if (process_sp) {
440 std::lock_guard<std::recursive_mutex> guard(
441 process_sp->GetTarget().GetAPIMutex());
442 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);
443 }
444
445 if (log)
446 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
447 static_cast<void *>(process_sp.get()), index_id,
448 (ret_val ? "true" : "false"));
449
450 return ret_val;
451}
452
453SBThread SBProcess::GetThreadAtIndex(size_t index) {
454 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
455
456 SBThread sb_thread;
457 ThreadSP thread_sp;
458 ProcessSP process_sp(GetSP());
459 if (process_sp) {
460 Process::StopLocker stop_locker;
461 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
462 std::lock_guard<std::recursive_mutex> guard(
463 process_sp->GetTarget().GetAPIMutex());
464 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
465 sb_thread.SetThread(thread_sp);
466 }
467
468 if (log)
469 log->Printf("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
470 static_cast<void *>(process_sp.get()),
471 static_cast<uint32_t>(index),
472 static_cast<void *>(thread_sp.get()));
473
474 return sb_thread;
475}
476
477uint32_t SBProcess::GetNumQueues() {
478 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
479
480 uint32_t num_queues = 0;
481 ProcessSP process_sp(GetSP());
482 if (process_sp) {
483 Process::StopLocker stop_locker;
484 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
485 std::lock_guard<std::recursive_mutex> guard(
486 process_sp->GetTarget().GetAPIMutex());
487 num_queues = process_sp->GetQueueList().GetSize();
488 }
489 }
490
491 if (log)
492 log->Printf("SBProcess(%p)::GetNumQueues () => %d",
493 static_cast<void *>(process_sp.get()), num_queues);
494
495 return num_queues;
496}
497
498SBQueue SBProcess::GetQueueAtIndex(size_t index) {
499 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
500
501 SBQueue sb_queue;
502 QueueSP queue_sp;
503 ProcessSP process_sp(GetSP());
504 if (process_sp) {
505 Process::StopLocker stop_locker;
506 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
507 std::lock_guard<std::recursive_mutex> guard(
508 process_sp->GetTarget().GetAPIMutex());
509 queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
510 sb_queue.SetQueue(queue_sp);
511 }
512 }
513
514 if (log)
515 log->Printf("SBProcess(%p)::GetQueueAtIndex (index=%d) => SBQueue(%p)",
516 static_cast<void *>(process_sp.get()),
517 static_cast<uint32_t>(index),
518 static_cast<void *>(queue_sp.get()));
519
520 return sb_queue;
521}
522
523uint32_t SBProcess::GetStopID(bool include_expression_stops) {
524 ProcessSP process_sp(GetSP());
525 if (process_sp) {
526 std::lock_guard<std::recursive_mutex> guard(
527 process_sp->GetTarget().GetAPIMutex());
528 if (include_expression_stops)
529 return process_sp->GetStopID();
530 else
531 return process_sp->GetLastNaturalStopID();
532 }
533 return 0;
534}
535
536SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
537 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
538
539 SBEvent sb_event;
540 EventSP event_sp;
541 ProcessSP process_sp(GetSP());
542 if (process_sp) {
543 std::lock_guard<std::recursive_mutex> guard(
544 process_sp->GetTarget().GetAPIMutex());
545 event_sp = process_sp->GetStopEventForStopID(stop_id);
546 sb_event.reset(event_sp);
547 }
548
549 if (log)
550 log->Printf("SBProcess(%p)::GetStopEventForStopID (stop_id=%" PRIu32
551 ") => SBEvent(%p)",
552 static_cast<void *>(process_sp.get()), stop_id,
553 static_cast<void *>(event_sp.get()));
554
555 return sb_event;
556}
557
558StateType SBProcess::GetState() {
559
560 StateType ret_val = eStateInvalid;
561 ProcessSP process_sp(GetSP());
562 if (process_sp) {
563 std::lock_guard<std::recursive_mutex> guard(
564 process_sp->GetTarget().GetAPIMutex());
565 ret_val = process_sp->GetState();
566 }
567
568 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
569 if (log)
570 log->Printf("SBProcess(%p)::GetState () => %s",
571 static_cast<void *>(process_sp.get()),
572 lldb_private::StateAsCString(ret_val));
573
574 return ret_val;
575}
576
577int SBProcess::GetExitStatus() {
578 int exit_status = 0;
579 ProcessSP process_sp(GetSP());
580 if (process_sp) {
581 std::lock_guard<std::recursive_mutex> guard(
582 process_sp->GetTarget().GetAPIMutex());
583 exit_status = process_sp->GetExitStatus();
584 }
585 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
586 if (log)
587 log->Printf("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
588 static_cast<void *>(process_sp.get()), exit_status,
589 exit_status);
590
591 return exit_status;
592}
593
594const char *SBProcess::GetExitDescription() {
595 const char *exit_desc = NULL;
596 ProcessSP process_sp(GetSP());
597 if (process_sp) {
598 std::lock_guard<std::recursive_mutex> guard(
599 process_sp->GetTarget().GetAPIMutex());
600 exit_desc = process_sp->GetExitDescription();
601 }
602 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
603 if (log)
604 log->Printf("SBProcess(%p)::GetExitDescription () => %s",
605 static_cast<void *>(process_sp.get()), exit_desc);
606 return exit_desc;
607}
608
609lldb::pid_t SBProcess::GetProcessID() {
610 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
611 ProcessSP process_sp(GetSP());
612 if (process_sp)
613 ret_val = process_sp->GetID();
614
615 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
616 if (log)
617 log->Printf("SBProcess(%p)::GetProcessID () => %" PRIu64,
618 static_cast<void *>(process_sp.get()), ret_val);
619
620 return ret_val;
621}
622
623uint32_t SBProcess::GetUniqueID() {
624 uint32_t ret_val = 0;
625 ProcessSP process_sp(GetSP());
626 if (process_sp)
627 ret_val = process_sp->GetUniqueID();
628 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
629 if (log)
630 log->Printf("SBProcess(%p)::GetUniqueID () => %" PRIu32,
631 static_cast<void *>(process_sp.get()), ret_val);
632 return ret_val;
633}
634
635ByteOrder SBProcess::GetByteOrder() const {
636 ByteOrder byteOrder = eByteOrderInvalid;
637 ProcessSP process_sp(GetSP());
638 if (process_sp)
639 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
640
641 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
642 if (log)
643 log->Printf("SBProcess(%p)::GetByteOrder () => %d",
644 static_cast<void *>(process_sp.get()), byteOrder);
645
646 return byteOrder;
647}
648
649uint32_t SBProcess::GetAddressByteSize() const {
650 uint32_t size = 0;
651 ProcessSP process_sp(GetSP());
652 if (process_sp)
653 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
654
655 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
656 if (log)
657 log->Printf("SBProcess(%p)::GetAddressByteSize () => %d",
658 static_cast<void *>(process_sp.get()), size);
659
660 return size;
661}
662
663SBError SBProcess::Continue() {
664 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
665
666 SBError sb_error;
667 ProcessSP process_sp(GetSP());
668
669 if (log)
670 log->Printf("SBProcess(%p)::Continue ()...",
671 static_cast<void *>(process_sp.get()));
672
673 if (process_sp) {
674 std::lock_guard<std::recursive_mutex> guard(
675 process_sp->GetTarget().GetAPIMutex());
676
677 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
678 sb_error.ref() = process_sp->Resume();
679 else
680 sb_error.ref() = process_sp->ResumeSynchronous(NULL);
681 } else
682 sb_error.SetErrorString("SBProcess is invalid");
683
684 if (log) {
685 SBStream sstr;
686 sb_error.GetDescription(sstr);
687 log->Printf("SBProcess(%p)::Continue () => SBError (%p): %s",
688 static_cast<void *>(process_sp.get()),
689 static_cast<void *>(sb_error.get()), sstr.GetData());
690 }
691
692 return sb_error;
693}
694
695SBError SBProcess::Destroy() {
696 SBError sb_error;
697 ProcessSP process_sp(GetSP());
698 if (process_sp) {
699 std::lock_guard<std::recursive_mutex> guard(
700 process_sp->GetTarget().GetAPIMutex());
701 sb_error.SetError(process_sp->Destroy(false));
702 } else
703 sb_error.SetErrorString("SBProcess is invalid");
704
705 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
706 if (log) {
707 SBStream sstr;
708 sb_error.GetDescription(sstr);
709 log->Printf("SBProcess(%p)::Destroy () => SBError (%p): %s",
710 static_cast<void *>(process_sp.get()),
711 static_cast<void *>(sb_error.get()), sstr.GetData());
712 }
713
714 return sb_error;
715}
716
717SBError SBProcess::Stop() {
718 SBError sb_error;
719 ProcessSP process_sp(GetSP());
720 if (process_sp) {
721 std::lock_guard<std::recursive_mutex> guard(
722 process_sp->GetTarget().GetAPIMutex());
723 sb_error.SetError(process_sp->Halt());
724 } else
725 sb_error.SetErrorString("SBProcess is invalid");
726
727 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
728 if (log) {
729 SBStream sstr;
730 sb_error.GetDescription(sstr);
731 log->Printf("SBProcess(%p)::Stop () => SBError (%p): %s",
732 static_cast<void *>(process_sp.get()),
733 static_cast<void *>(sb_error.get()), sstr.GetData());
734 }
735
736 return sb_error;
737}
738
739SBError SBProcess::Kill() {
740 SBError sb_error;
741 ProcessSP process_sp(GetSP());
742 if (process_sp) {
743 std::lock_guard<std::recursive_mutex> guard(
744 process_sp->GetTarget().GetAPIMutex());
745 sb_error.SetError(process_sp->Destroy(true));
746 } else
747 sb_error.SetErrorString("SBProcess is invalid");
748
749 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
750 if (log) {
751 SBStream sstr;
752 sb_error.GetDescription(sstr);
753 log->Printf("SBProcess(%p)::Kill () => SBError (%p): %s",
754 static_cast<void *>(process_sp.get()),
755 static_cast<void *>(sb_error.get()), sstr.GetData());
756 }
757
758 return sb_error;
759}
760
761SBError SBProcess::Detach() {
762 // FIXME: This should come from a process default.
763 bool keep_stopped = false;
764 return Detach(keep_stopped);
765}
766
767SBError SBProcess::Detach(bool keep_stopped) {
768 SBError sb_error;
769 ProcessSP process_sp(GetSP());
770 if (process_sp) {
771 std::lock_guard<std::recursive_mutex> guard(
772 process_sp->GetTarget().GetAPIMutex());
773 sb_error.SetError(process_sp->Detach(keep_stopped));
774 } else
775 sb_error.SetErrorString("SBProcess is invalid");
776
777 return sb_error;
778}
779
780SBError SBProcess::Signal(int signo) {
781 SBError sb_error;
782 ProcessSP process_sp(GetSP());
783 if (process_sp) {
784 std::lock_guard<std::recursive_mutex> guard(
785 process_sp->GetTarget().GetAPIMutex());
786 sb_error.SetError(process_sp->Signal(signo));
787 } else
788 sb_error.SetErrorString("SBProcess is invalid");
789 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
790 if (log) {
791 SBStream sstr;
792 sb_error.GetDescription(sstr);
793 log->Printf("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
794 static_cast<void *>(process_sp.get()), signo,
795 static_cast<void *>(sb_error.get()), sstr.GetData());
796 }
797 return sb_error;
798}
799
800SBUnixSignals SBProcess::GetUnixSignals() {
801 if (auto process_sp = GetSP())
802 return SBUnixSignals{process_sp};
803
804 return {};
805}
806
807void SBProcess::SendAsyncInterrupt() {
808 ProcessSP process_sp(GetSP());
809 if (process_sp) {
810 process_sp->SendAsyncInterrupt();
811 }
812}
813
814SBThread SBProcess::GetThreadByID(tid_t tid) {
815 SBThread sb_thread;
816 ThreadSP thread_sp;
817 ProcessSP process_sp(GetSP());
818 if (process_sp) {
819 Process::StopLocker stop_locker;
820 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
821 std::lock_guard<std::recursive_mutex> guard(
822 process_sp->GetTarget().GetAPIMutex());
823 thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);
824 sb_thread.SetThread(thread_sp);
825 }
826
827 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
828 if (log)
829 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64
830 ") => SBThread (%p)",
831 static_cast<void *>(process_sp.get()), tid,
832 static_cast<void *>(thread_sp.get()));
833
834 return sb_thread;
835}
836
837SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
838 SBThread sb_thread;
839 ThreadSP thread_sp;
840 ProcessSP process_sp(GetSP());
841 if (process_sp) {
842 Process::StopLocker stop_locker;
843 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
844 std::lock_guard<std::recursive_mutex> guard(
845 process_sp->GetTarget().GetAPIMutex());
846 thread_sp =
847 process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);
848 sb_thread.SetThread(thread_sp);
849 }
850
851 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
852 if (log)
853 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
854 static_cast<void *>(process_sp.get()), index_id,
855 static_cast<void *>(thread_sp.get()));
856
857 return sb_thread;
858}
859
860StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
861 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
862
863 StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
864
865 if (log)
866 log->Printf("SBProcess::GetStateFromEvent (event.sp=%p) => %s",
867 static_cast<void *>(event.get()),
868 lldb_private::StateAsCString(ret_val));
869
870 return ret_val;
871}
872
873bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
874 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
875
876 bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
877
878 if (log)
879 log->Printf("SBProcess::%s (event.sp=%p) => %d", __FUNCTION__,
880 static_cast<void *>(event.get()), ret_val);
881
882 return ret_val;
883}
884
885size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
886 return Process::ProcessEventData::GetNumRestartedReasons(event.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887}
888
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000889const char *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000890SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
891 size_t idx) {
892 return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000893}
894
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
896 ProcessSP process_sp =
897 Process::ProcessEventData::GetProcessFromEvent(event.get());
898 if (!process_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +0000899 // StructuredData events also know the process they come from. Try that.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
901 }
902
903 return SBProcess(process_sp);
Jim Inghamd7b30ef2012-10-26 19:18:04 +0000904}
905
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
907 return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
Ilia K06d28552015-05-15 09:29:09 +0000908}
909
Todd Fiala75930012016-08-19 04:21:48 +0000910lldb::SBStructuredData
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
912 return SBStructuredData(event.GetSP());
Todd Fiala75930012016-08-19 04:21:48 +0000913}
914
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
916 return (event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass()) &&
917 !EventIsStructuredDataEvent(event);
Todd Fiala75930012016-08-19 04:21:48 +0000918}
919
Kate Stoneb9c1b512016-09-06 20:57:50 +0000920bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
921 EventSP event_sp = event.GetSP();
922 EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
923 return event_data && (event_data->GetFlavor() ==
924 EventDataStructuredData::GetFlavorString());
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000925}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926
Kate Stoneb9c1b512016-09-06 20:57:50 +0000927SBBroadcaster SBProcess::GetBroadcaster() const {
928 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000929
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 ProcessSP process_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000931
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932 SBBroadcaster broadcaster(process_sp.get(), false);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000933
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 if (log)
935 log->Printf("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",
936 static_cast<void *>(process_sp.get()),
937 static_cast<void *>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000938
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939 return broadcaster;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940}
941
Kate Stoneb9c1b512016-09-06 20:57:50 +0000942const char *SBProcess::GetBroadcasterClass() {
943 return Process::GetStaticBroadcasterClass().AsCString();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000944}
945
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
947 SBError &sb_error) {
948 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000949
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950 size_t bytes_read = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952 ProcessSP process_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000953
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954 if (log)
955 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
956 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
957 static_cast<void *>(process_sp.get()), addr,
958 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
959 static_cast<void *>(sb_error.get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000960
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961 if (process_sp) {
962 Process::StopLocker stop_locker;
963 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
964 std::lock_guard<std::recursive_mutex> guard(
965 process_sp->GetTarget().GetAPIMutex());
966 bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
967 } else {
968 if (log)
969 log->Printf("SBProcess(%p)::ReadMemory() => error: process is running",
970 static_cast<void *>(process_sp.get()));
971 sb_error.SetErrorString("process is running");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973 } else {
974 sb_error.SetErrorString("SBProcess is invalid");
975 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000976
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 if (log) {
978 SBStream sstr;
979 sb_error.GetDescription(sstr);
980 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
981 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
982 static_cast<void *>(process_sp.get()), addr,
983 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
984 static_cast<void *>(sb_error.get()), sstr.GetData(),
985 static_cast<uint64_t>(bytes_read));
986 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000987
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988 return bytes_read;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989}
990
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
992 lldb::SBError &sb_error) {
993 size_t bytes_read = 0;
994 ProcessSP process_sp(GetSP());
995 if (process_sp) {
996 Process::StopLocker stop_locker;
997 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
998 std::lock_guard<std::recursive_mutex> guard(
999 process_sp->GetTarget().GetAPIMutex());
1000 bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
1001 sb_error.ref());
1002 } else {
1003 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1004 if (log)
1005 log->Printf("SBProcess(%p)::ReadCStringFromMemory() => error: process "
1006 "is running",
1007 static_cast<void *>(process_sp.get()));
1008 sb_error.SetErrorString("process is running");
Greg Claytone91b7952011-12-15 03:14:23 +00001009 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 } else {
1011 sb_error.SetErrorString("SBProcess is invalid");
1012 }
1013 return bytes_read;
Greg Claytone91b7952011-12-15 03:14:23 +00001014}
1015
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
1017 lldb::SBError &sb_error) {
1018 uint64_t value = 0;
1019 ProcessSP process_sp(GetSP());
1020 if (process_sp) {
1021 Process::StopLocker stop_locker;
1022 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1023 std::lock_guard<std::recursive_mutex> guard(
1024 process_sp->GetTarget().GetAPIMutex());
1025 value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
1026 sb_error.ref());
1027 } else {
1028 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1029 if (log)
1030 log->Printf("SBProcess(%p)::ReadUnsignedFromMemory() => error: process "
1031 "is running",
1032 static_cast<void *>(process_sp.get()));
1033 sb_error.SetErrorString("process is running");
Greg Claytone91b7952011-12-15 03:14:23 +00001034 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 } else {
1036 sb_error.SetErrorString("SBProcess is invalid");
1037 }
1038 return value;
Greg Claytone91b7952011-12-15 03:14:23 +00001039}
1040
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
1042 lldb::SBError &sb_error) {
1043 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1044 ProcessSP process_sp(GetSP());
1045 if (process_sp) {
1046 Process::StopLocker stop_locker;
1047 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1048 std::lock_guard<std::recursive_mutex> guard(
1049 process_sp->GetTarget().GetAPIMutex());
1050 ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
1051 } else {
1052 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1053 if (log)
1054 log->Printf("SBProcess(%p)::ReadPointerFromMemory() => error: process "
1055 "is running",
1056 static_cast<void *>(process_sp.get()));
1057 sb_error.SetErrorString("process is running");
Greg Claytone91b7952011-12-15 03:14:23 +00001058 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 } else {
1060 sb_error.SetErrorString("SBProcess is invalid");
1061 }
1062 return ptr;
Greg Claytone91b7952011-12-15 03:14:23 +00001063}
1064
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
1066 SBError &sb_error) {
1067 size_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Claytonacdbe812012-01-30 09:04:36 +00001070
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 ProcessSP process_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +00001072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 if (log)
1074 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1075 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1076 static_cast<void *>(process_sp.get()), addr,
1077 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1078 static_cast<void *>(sb_error.get()));
Greg Clayton48381312010-10-30 04:51:46 +00001079
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 if (process_sp) {
1081 Process::StopLocker stop_locker;
1082 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1083 std::lock_guard<std::recursive_mutex> guard(
1084 process_sp->GetTarget().GetAPIMutex());
1085 bytes_written =
1086 process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
1087 } else {
1088 if (log)
1089 log->Printf("SBProcess(%p)::WriteMemory() => error: process is running",
1090 static_cast<void *>(process_sp.get()));
1091 sb_error.SetErrorString("process is running");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 if (log) {
1096 SBStream sstr;
1097 sb_error.GetDescription(sstr);
1098 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1099 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1100 static_cast<void *>(process_sp.get()), addr,
1101 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1102 static_cast<void *>(sb_error.get()), sstr.GetData(),
1103 static_cast<uint64_t>(bytes_written));
1104 }
Greg Clayton48381312010-10-30 04:51:46 +00001105
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107}
1108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109bool SBProcess::GetDescription(SBStream &description) {
1110 Stream &strm = description.ref();
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001111
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 ProcessSP process_sp(GetSP());
1113 if (process_sp) {
1114 char path[PATH_MAX];
1115 GetTarget().GetExecutable().GetPath(path, sizeof(path));
1116 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1117 const char *exe_name = NULL;
1118 if (exe_module)
1119 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
Greg Clayton1d273162010-10-06 03:09:58 +00001120
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121 strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1122 process_sp->GetID(), lldb_private::StateAsCString(GetState()),
1123 GetNumThreads(), exe_name ? ", executable = " : "",
1124 exe_name ? exe_name : "");
1125 } else
1126 strm.PutCString("No value");
Caroline Ticedde9cff2010-09-20 05:20:02 +00001127
Kate Stoneb9c1b512016-09-06 20:57:50 +00001128 return true;
Caroline Ticedde9cff2010-09-20 05:20:02 +00001129}
Greg Clayton8f343b02010-11-04 01:54:29 +00001130
1131uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1133 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Johnny Chenf9ef60d2012-05-23 22:34:34 +00001134
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135 uint32_t num = 0;
1136 ProcessSP process_sp(GetSP());
1137 if (process_sp) {
1138 std::lock_guard<std::recursive_mutex> guard(
1139 process_sp->GetTarget().GetAPIMutex());
1140 sb_error.SetError(process_sp->GetWatchpointSupportInfo(num));
1141 if (log)
1142 log->Printf("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1143 static_cast<void *>(process_sp.get()), num);
1144 } else {
1145 sb_error.SetErrorString("SBProcess is invalid");
1146 }
1147 return num;
Johnny Chenf9ef60d2012-05-23 22:34:34 +00001148}
1149
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
1151 lldb::SBError &sb_error) {
1152 return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001153}
1154
Kate Stoneb9c1b512016-09-06 20:57:50 +00001155uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
1156 const lldb::SBFileSpec &sb_remote_image_spec,
1157 lldb::SBError &sb_error) {
Jim Inghamabc5d722017-05-06 01:15:47 +00001158 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 ProcessSP process_sp(GetSP());
1160 if (process_sp) {
1161 Process::StopLocker stop_locker;
1162 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
Jim Inghamabc5d722017-05-06 01:15:47 +00001163 if (log)
1164 log->Printf("SBProcess(%p)::LoadImage() => calling Platform::LoadImage"
1165 "for: %s",
1166 static_cast<void *>(process_sp.get()),
1167 sb_local_image_spec.GetFilename());
1168
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169 std::lock_guard<std::recursive_mutex> guard(
Jim Inghamabc5d722017-05-06 01:15:47 +00001170 process_sp->GetTarget().GetAPIMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1172 return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
1173 *sb_remote_image_spec, sb_error.ref());
1174 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001175 if (log)
1176 log->Printf("SBProcess(%p)::LoadImage() => error: process is running",
1177 static_cast<void *>(process_sp.get()));
1178 sb_error.SetErrorString("process is running");
Greg Claytonaf67cec2010-12-20 20:49:23 +00001179 }
Jim Inghamabc5d722017-05-06 01:15:47 +00001180 } else {
1181 if (log)
1182 log->Printf("SBProcess(%p)::LoadImage() => error: called with invalid"
1183 " process",
1184 static_cast<void *>(process_sp.get()));
1185 sb_error.SetErrorString("process is invalid");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186 }
1187 return LLDB_INVALID_IMAGE_TOKEN;
Greg Clayton8f343b02010-11-04 01:54:29 +00001188}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001189
Kate Stoneb9c1b512016-09-06 20:57:50 +00001190lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1191 lldb::SBError sb_error;
1192 ProcessSP process_sp(GetSP());
1193 if (process_sp) {
1194 Process::StopLocker stop_locker;
1195 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1196 std::lock_guard<std::recursive_mutex> guard(
1197 process_sp->GetTarget().GetAPIMutex());
1198 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1199 sb_error.SetError(
1200 platform_sp->UnloadImage(process_sp.get(), image_token));
1201 } else {
1202 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1203 if (log)
1204 log->Printf("SBProcess(%p)::UnloadImage() => error: process is running",
1205 static_cast<void *>(process_sp.get()));
1206 sb_error.SetErrorString("process is running");
Greg Claytonaf67cec2010-12-20 20:49:23 +00001207 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001208 } else
1209 sb_error.SetErrorString("invalid process");
1210 return sb_error;
Greg Clayton8f343b02010-11-04 01:54:29 +00001211}
Jason Molenda8c713372013-11-05 11:00:35 +00001212
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213lldb::SBError SBProcess::SendEventData(const char *event_data) {
1214 lldb::SBError sb_error;
1215 ProcessSP process_sp(GetSP());
1216 if (process_sp) {
1217 Process::StopLocker stop_locker;
1218 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1219 std::lock_guard<std::recursive_mutex> guard(
1220 process_sp->GetTarget().GetAPIMutex());
1221 sb_error.SetError(process_sp->SendEventData(event_data));
1222 } else {
1223 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1224 if (log)
1225 log->Printf(
1226 "SBProcess(%p)::SendEventData() => error: process is running",
1227 static_cast<void *>(process_sp.get()));
1228 sb_error.SetErrorString("process is running");
Jason Molendaa3329782014-03-29 18:54:20 +00001229 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 } else
1231 sb_error.SetErrorString("invalid process");
1232 return sb_error;
Jason Molendaa3329782014-03-29 18:54:20 +00001233}
1234
Kate Stoneb9c1b512016-09-06 20:57:50 +00001235uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1236 ProcessSP process_sp(GetSP());
1237 if (process_sp && process_sp->GetSystemRuntime()) {
1238 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1239 return runtime->GetExtendedBacktraceTypes().size();
1240 }
1241 return 0;
Jason Molenda8c713372013-11-05 11:00:35 +00001242}
1243
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1245 ProcessSP process_sp(GetSP());
1246 if (process_sp && process_sp->GetSystemRuntime()) {
1247 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1248 const std::vector<ConstString> &names =
1249 runtime->GetExtendedBacktraceTypes();
1250 if (idx < names.size()) {
1251 return names[idx].AsCString();
1252 } else {
1253 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1254 if (log)
1255 log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => "
1256 "error: requested extended backtrace name out of bounds",
1257 static_cast<void *>(process_sp.get()));
Jason Molenda8c713372013-11-05 11:00:35 +00001258 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 }
1260 return NULL;
Jason Molenda8c713372013-11-05 11:00:35 +00001261}
Kuba Breckaa51ea382014-09-06 01:33:13 +00001262
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1264 ProcessSP process_sp(GetSP());
1265 SBThreadCollection threads;
1266 if (process_sp) {
1267 threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1268 }
1269 return threads;
Kuba Breckaa51ea382014-09-06 01:33:13 +00001270}
Kuba Brecka63927542014-10-11 01:59:32 +00001271
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272bool SBProcess::IsInstrumentationRuntimePresent(
1273 InstrumentationRuntimeType type) {
1274 ProcessSP process_sp(GetSP());
1275 if (!process_sp)
1276 return false;
1277
1278 InstrumentationRuntimeSP runtime_sp =
1279 process_sp->GetInstrumentationRuntime(type);
1280
1281 if (!runtime_sp.get())
1282 return false;
1283
1284 return runtime_sp->IsActive();
Kuba Brecka63927542014-10-11 01:59:32 +00001285}
Adrian McCarthyf7d18932015-11-20 23:09:11 +00001286
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287lldb::SBError SBProcess::SaveCore(const char *file_name) {
1288 lldb::SBError error;
1289 ProcessSP process_sp(GetSP());
1290 if (!process_sp) {
1291 error.SetErrorString("SBProcess is invalid");
Adrian McCarthyf7d18932015-11-20 23:09:11 +00001292 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293 }
1294
1295 std::lock_guard<std::recursive_mutex> guard(
1296 process_sp->GetTarget().GetAPIMutex());
1297
1298 if (process_sp->GetState() != eStateStopped) {
1299 error.SetErrorString("the process is not stopped");
1300 return error;
1301 }
1302
1303 FileSpec core_file(file_name, false);
1304 error.ref() = PluginManager::SaveCore(process_sp, core_file);
1305 return error;
Adrian McCarthyf7d18932015-11-20 23:09:11 +00001306}
Howard Hellyer260368432016-06-23 08:35:37 +00001307
1308lldb::SBError
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1310 SBMemoryRegionInfo &sb_region_info) {
1311 lldb::SBError sb_error;
1312 ProcessSP process_sp(GetSP());
1313 MemoryRegionInfoSP region_info_sp =
1314 std::make_shared<lldb_private::MemoryRegionInfo>();
1315 if (process_sp) {
1316 Process::StopLocker stop_locker;
1317 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1318 std::lock_guard<std::recursive_mutex> guard(
1319 process_sp->GetTarget().GetAPIMutex());
1320 sb_error.ref() =
1321 process_sp->GetMemoryRegionInfo(load_addr, *region_info_sp);
1322 if (sb_error.Success()) {
1323 sb_region_info.ref() = *region_info_sp;
1324 }
1325 } else {
1326 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1327 if (log)
1328 log->Printf(
1329 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1330 static_cast<void *>(process_sp.get()));
1331 sb_error.SetErrorString("process is running");
Howard Hellyer260368432016-06-23 08:35:37 +00001332 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001333 } else {
1334 sb_error.SetErrorString("SBProcess is invalid");
1335 }
1336 return sb_error;
Howard Hellyer260368432016-06-23 08:35:37 +00001337}
1338
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1340 lldb::SBError sb_error;
1341 lldb::SBMemoryRegionInfoList sb_region_list;
1342 ProcessSP process_sp(GetSP());
1343 if (process_sp) {
1344 Process::StopLocker stop_locker;
1345 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1346 std::lock_guard<std::recursive_mutex> guard(
1347 process_sp->GetTarget().GetAPIMutex());
1348 std::vector<MemoryRegionInfoSP> region_list;
1349 sb_error.ref() = process_sp->GetMemoryRegions(region_list);
1350 if (sb_error.Success()) {
1351 std::vector<MemoryRegionInfoSP>::iterator end = region_list.end();
1352 for (std::vector<MemoryRegionInfoSP>::iterator it = region_list.begin();
1353 it != end; it++) {
1354 SBMemoryRegionInfo sb_region_info(it->get());
1355 sb_region_list.Append(sb_region_info);
Howard Hellyer260368432016-06-23 08:35:37 +00001356 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 }
1358 } else {
1359 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1360 if (log)
1361 log->Printf(
1362 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1363 static_cast<void *>(process_sp.get()));
1364 sb_error.SetErrorString("process is running");
Howard Hellyer260368432016-06-23 08:35:37 +00001365 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366 } else {
1367 sb_error.SetErrorString("SBProcess is invalid");
1368 }
1369 return sb_region_list;
Howard Hellyer260368432016-06-23 08:35:37 +00001370}
Vadim Macagon141a6262017-08-01 07:34:26 +00001371
1372lldb::SBProcessInfo SBProcess::GetProcessInfo() {
1373 lldb::SBProcessInfo sb_proc_info;
1374 ProcessSP process_sp(GetSP());
1375 ProcessInstanceInfo proc_info;
1376 if (process_sp && process_sp->GetProcessInfo(proc_info)) {
1377 sb_proc_info.SetProcessInfo(proc_info);
1378 }
1379 return sb_proc_info;
1380}