blob: 5614cb468a69bdc1b5480b510bb0b0a1cfbdf031 [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"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/Interpreter/Args.h"
Howard Hellyer260368432016-06-23 08:35:37 +000024#include "lldb/Target/MemoryRegionInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/Process.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/RegisterContext.h"
Jason Molenda8c713372013-11-05 11:00:35 +000027#include "lldb/Target/SystemRuntime.h"
Greg Clayton66111032010-06-23 01:19:29 +000028#include "lldb/Target/Target.h"
29#include "lldb/Target/Thread.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)
143 launch_info.GetEnvironmentEntries().SetArguments(envp);
144 error.SetError(process_sp->Launch(launch_info));
145 } else {
146 error.SetErrorString("must be in eStateConnected to call RemoteLaunch");
147 }
148 } else {
149 error.SetErrorString("unable to attach pid");
150 }
151
152 if (log) {
153 SBStream sstr;
154 error.GetDescription(sstr);
155 log->Printf("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s",
156 static_cast<void *>(process_sp.get()),
157 static_cast<void *>(error.get()), sstr.GetData());
158 }
159
160 return error.Success();
161}
162
163bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
164 lldb::SBError &error) {
165 ProcessSP process_sp(GetSP());
166 if (process_sp) {
167 std::lock_guard<std::recursive_mutex> guard(
168 process_sp->GetTarget().GetAPIMutex());
169 if (process_sp->GetState() == eStateConnected) {
170 ProcessAttachInfo attach_info;
171 attach_info.SetProcessID(pid);
172 error.SetError(process_sp->Attach(attach_info));
173 } else {
174 error.SetErrorString(
175 "must be in eStateConnected to call RemoteAttachToProcessWithID");
176 }
177 } else {
178 error.SetErrorString("unable to attach pid");
179 }
180
181 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
182 if (log) {
183 SBStream sstr;
184 error.GetDescription(sstr);
185 log->Printf("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64
186 ") => SBError (%p): %s",
187 static_cast<void *>(process_sp.get()), pid,
188 static_cast<void *>(error.get()), sstr.GetData());
189 }
190
191 return error.Success();
192}
193
194uint32_t SBProcess::GetNumThreads() {
195 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
196
197 uint32_t num_threads = 0;
198 ProcessSP process_sp(GetSP());
199 if (process_sp) {
200 Process::StopLocker stop_locker;
201
202 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
203 std::lock_guard<std::recursive_mutex> guard(
204 process_sp->GetTarget().GetAPIMutex());
205 num_threads = process_sp->GetThreadList().GetSize(can_update);
206 }
207
208 if (log)
209 log->Printf("SBProcess(%p)::GetNumThreads () => %d",
210 static_cast<void *>(process_sp.get()), num_threads);
211
212 return num_threads;
213}
214
215SBThread SBProcess::GetSelectedThread() const {
216 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
217
218 SBThread sb_thread;
219 ThreadSP thread_sp;
220 ProcessSP process_sp(GetSP());
221 if (process_sp) {
222 std::lock_guard<std::recursive_mutex> guard(
223 process_sp->GetTarget().GetAPIMutex());
224 thread_sp = process_sp->GetThreadList().GetSelectedThread();
225 sb_thread.SetThread(thread_sp);
226 }
227
228 if (log)
229 log->Printf("SBProcess(%p)::GetSelectedThread () => SBThread(%p)",
230 static_cast<void *>(process_sp.get()),
231 static_cast<void *>(thread_sp.get()));
232
233 return sb_thread;
234}
235
236SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
237 lldb::addr_t context) {
238 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
239
240 SBThread sb_thread;
241 ThreadSP thread_sp;
242 ProcessSP process_sp(GetSP());
243 if (process_sp) {
244 std::lock_guard<std::recursive_mutex> guard(
245 process_sp->GetTarget().GetAPIMutex());
246 thread_sp = process_sp->CreateOSPluginThread(tid, context);
247 sb_thread.SetThread(thread_sp);
248 }
249
250 if (log)
251 log->Printf("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64
252 ", context=0x%" PRIx64 ") => SBThread(%p)",
253 static_cast<void *>(process_sp.get()), tid, context,
254 static_cast<void *>(thread_sp.get()));
255
256 return sb_thread;
257}
258
259SBTarget SBProcess::GetTarget() const {
260 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
261
262 SBTarget sb_target;
263 TargetSP target_sp;
264 ProcessSP process_sp(GetSP());
265 if (process_sp) {
266 target_sp = process_sp->GetTarget().shared_from_this();
267 sb_target.SetSP(target_sp);
268 }
269
270 if (log)
271 log->Printf("SBProcess(%p)::GetTarget () => SBTarget(%p)",
272 static_cast<void *>(process_sp.get()),
273 static_cast<void *>(target_sp.get()));
274
275 return sb_target;
276}
277
278size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
279 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
280
281 size_t ret_val = 0;
282 ProcessSP process_sp(GetSP());
283 if (process_sp) {
284 Error error;
285 ret_val = process_sp->PutSTDIN(src, src_len, error);
286 }
287
288 if (log)
289 log->Printf("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%" PRIu64
290 ") => %" PRIu64,
291 static_cast<void *>(process_sp.get()), src,
292 static_cast<uint64_t>(src_len), static_cast<uint64_t>(ret_val));
293
294 return ret_val;
295}
296
297size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
298 size_t bytes_read = 0;
299 ProcessSP process_sp(GetSP());
300 if (process_sp) {
301 Error error;
302 bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);
303 }
304
305 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
306 if (log)
307 log->Printf(
308 "SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64
309 ") => %" PRIu64,
310 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
311 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
312
313 return bytes_read;
314}
315
316size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
317 size_t bytes_read = 0;
318 ProcessSP process_sp(GetSP());
319 if (process_sp) {
320 Error error;
321 bytes_read = process_sp->GetSTDERR(dst, dst_len, error);
322 }
323
324 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
325 if (log)
326 log->Printf(
327 "SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64
328 ") => %" PRIu64,
329 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
330 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
331
332 return bytes_read;
333}
334
335size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
336 size_t bytes_read = 0;
337 ProcessSP process_sp(GetSP());
338 if (process_sp) {
339 Error error;
340 bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);
341 }
342
343 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
344 if (log)
345 log->Printf(
346 "SBProcess(%p)::GetAsyncProfileData (dst=\"%.*s\", dst_len=%" PRIu64
347 ") => %" PRIu64,
348 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
349 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
350
351 return bytes_read;
352}
353
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 {
366
367 uid = process_sp->StartTrace(options.m_traceoptions_sp, error.ref());
368 trace_instance.SetTraceUID(uid);
369 LLDB_LOG(log, "SBProcess::returned uid - %" PRIx64, uid);
370 }
371 return trace_instance;
372}
373
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
375 if (out == NULL)
376 return;
377
378 ProcessSP process_sp(GetSP());
379 if (process_sp) {
380 const StateType event_state = SBProcess::GetStateFromEvent(event);
381 char message[1024];
382 int message_len = ::snprintf(
383 message, sizeof(message), "Process %" PRIu64 " %s\n",
384 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
385
386 if (message_len > 0)
387 ::fwrite(message, 1, message_len, out);
388 }
389}
390
391void SBProcess::AppendEventStateReport(const SBEvent &event,
392 SBCommandReturnObject &result) {
393 ProcessSP process_sp(GetSP());
394 if (process_sp) {
395 const StateType event_state = SBProcess::GetStateFromEvent(event);
396 char message[1024];
397 ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",
398 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
399
400 result.AppendMessage(message);
401 }
402}
403
404bool SBProcess::SetSelectedThread(const SBThread &thread) {
405 ProcessSP process_sp(GetSP());
406 if (process_sp) {
407 std::lock_guard<std::recursive_mutex> guard(
408 process_sp->GetTarget().GetAPIMutex());
409 return process_sp->GetThreadList().SetSelectedThreadByID(
410 thread.GetThreadID());
411 }
412 return false;
413}
414
415bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
416 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
417
418 bool ret_val = false;
419 ProcessSP process_sp(GetSP());
420 if (process_sp) {
421 std::lock_guard<std::recursive_mutex> guard(
422 process_sp->GetTarget().GetAPIMutex());
423 ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);
424 }
425
426 if (log)
427 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64
428 ") => %s",
429 static_cast<void *>(process_sp.get()), tid,
430 (ret_val ? "true" : "false"));
431
432 return ret_val;
433}
434
435bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
436 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
437
438 bool ret_val = false;
439 ProcessSP process_sp(GetSP());
440 if (process_sp) {
441 std::lock_guard<std::recursive_mutex> guard(
442 process_sp->GetTarget().GetAPIMutex());
443 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);
444 }
445
446 if (log)
447 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
448 static_cast<void *>(process_sp.get()), index_id,
449 (ret_val ? "true" : "false"));
450
451 return ret_val;
452}
453
454SBThread SBProcess::GetThreadAtIndex(size_t index) {
455 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
456
457 SBThread sb_thread;
458 ThreadSP thread_sp;
459 ProcessSP process_sp(GetSP());
460 if (process_sp) {
461 Process::StopLocker stop_locker;
462 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
463 std::lock_guard<std::recursive_mutex> guard(
464 process_sp->GetTarget().GetAPIMutex());
465 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
466 sb_thread.SetThread(thread_sp);
467 }
468
469 if (log)
470 log->Printf("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
471 static_cast<void *>(process_sp.get()),
472 static_cast<uint32_t>(index),
473 static_cast<void *>(thread_sp.get()));
474
475 return sb_thread;
476}
477
478uint32_t SBProcess::GetNumQueues() {
479 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
480
481 uint32_t num_queues = 0;
482 ProcessSP process_sp(GetSP());
483 if (process_sp) {
484 Process::StopLocker stop_locker;
485 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
486 std::lock_guard<std::recursive_mutex> guard(
487 process_sp->GetTarget().GetAPIMutex());
488 num_queues = process_sp->GetQueueList().GetSize();
489 }
490 }
491
492 if (log)
493 log->Printf("SBProcess(%p)::GetNumQueues () => %d",
494 static_cast<void *>(process_sp.get()), num_queues);
495
496 return num_queues;
497}
498
499SBQueue SBProcess::GetQueueAtIndex(size_t index) {
500 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
501
502 SBQueue sb_queue;
503 QueueSP queue_sp;
504 ProcessSP process_sp(GetSP());
505 if (process_sp) {
506 Process::StopLocker stop_locker;
507 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
508 std::lock_guard<std::recursive_mutex> guard(
509 process_sp->GetTarget().GetAPIMutex());
510 queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
511 sb_queue.SetQueue(queue_sp);
512 }
513 }
514
515 if (log)
516 log->Printf("SBProcess(%p)::GetQueueAtIndex (index=%d) => SBQueue(%p)",
517 static_cast<void *>(process_sp.get()),
518 static_cast<uint32_t>(index),
519 static_cast<void *>(queue_sp.get()));
520
521 return sb_queue;
522}
523
524uint32_t SBProcess::GetStopID(bool include_expression_stops) {
525 ProcessSP process_sp(GetSP());
526 if (process_sp) {
527 std::lock_guard<std::recursive_mutex> guard(
528 process_sp->GetTarget().GetAPIMutex());
529 if (include_expression_stops)
530 return process_sp->GetStopID();
531 else
532 return process_sp->GetLastNaturalStopID();
533 }
534 return 0;
535}
536
537SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
538 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
539
540 SBEvent sb_event;
541 EventSP event_sp;
542 ProcessSP process_sp(GetSP());
543 if (process_sp) {
544 std::lock_guard<std::recursive_mutex> guard(
545 process_sp->GetTarget().GetAPIMutex());
546 event_sp = process_sp->GetStopEventForStopID(stop_id);
547 sb_event.reset(event_sp);
548 }
549
550 if (log)
551 log->Printf("SBProcess(%p)::GetStopEventForStopID (stop_id=%" PRIu32
552 ") => SBEvent(%p)",
553 static_cast<void *>(process_sp.get()), stop_id,
554 static_cast<void *>(event_sp.get()));
555
556 return sb_event;
557}
558
559StateType SBProcess::GetState() {
560
561 StateType ret_val = eStateInvalid;
562 ProcessSP process_sp(GetSP());
563 if (process_sp) {
564 std::lock_guard<std::recursive_mutex> guard(
565 process_sp->GetTarget().GetAPIMutex());
566 ret_val = process_sp->GetState();
567 }
568
569 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
570 if (log)
571 log->Printf("SBProcess(%p)::GetState () => %s",
572 static_cast<void *>(process_sp.get()),
573 lldb_private::StateAsCString(ret_val));
574
575 return ret_val;
576}
577
578int SBProcess::GetExitStatus() {
579 int exit_status = 0;
580 ProcessSP process_sp(GetSP());
581 if (process_sp) {
582 std::lock_guard<std::recursive_mutex> guard(
583 process_sp->GetTarget().GetAPIMutex());
584 exit_status = process_sp->GetExitStatus();
585 }
586 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
587 if (log)
588 log->Printf("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
589 static_cast<void *>(process_sp.get()), exit_status,
590 exit_status);
591
592 return exit_status;
593}
594
595const char *SBProcess::GetExitDescription() {
596 const char *exit_desc = NULL;
597 ProcessSP process_sp(GetSP());
598 if (process_sp) {
599 std::lock_guard<std::recursive_mutex> guard(
600 process_sp->GetTarget().GetAPIMutex());
601 exit_desc = process_sp->GetExitDescription();
602 }
603 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
604 if (log)
605 log->Printf("SBProcess(%p)::GetExitDescription () => %s",
606 static_cast<void *>(process_sp.get()), exit_desc);
607 return exit_desc;
608}
609
610lldb::pid_t SBProcess::GetProcessID() {
611 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
612 ProcessSP process_sp(GetSP());
613 if (process_sp)
614 ret_val = process_sp->GetID();
615
616 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
617 if (log)
618 log->Printf("SBProcess(%p)::GetProcessID () => %" PRIu64,
619 static_cast<void *>(process_sp.get()), ret_val);
620
621 return ret_val;
622}
623
624uint32_t SBProcess::GetUniqueID() {
625 uint32_t ret_val = 0;
626 ProcessSP process_sp(GetSP());
627 if (process_sp)
628 ret_val = process_sp->GetUniqueID();
629 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
630 if (log)
631 log->Printf("SBProcess(%p)::GetUniqueID () => %" PRIu32,
632 static_cast<void *>(process_sp.get()), ret_val);
633 return ret_val;
634}
635
636ByteOrder SBProcess::GetByteOrder() const {
637 ByteOrder byteOrder = eByteOrderInvalid;
638 ProcessSP process_sp(GetSP());
639 if (process_sp)
640 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
641
642 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
643 if (log)
644 log->Printf("SBProcess(%p)::GetByteOrder () => %d",
645 static_cast<void *>(process_sp.get()), byteOrder);
646
647 return byteOrder;
648}
649
650uint32_t SBProcess::GetAddressByteSize() const {
651 uint32_t size = 0;
652 ProcessSP process_sp(GetSP());
653 if (process_sp)
654 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
655
656 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
657 if (log)
658 log->Printf("SBProcess(%p)::GetAddressByteSize () => %d",
659 static_cast<void *>(process_sp.get()), size);
660
661 return size;
662}
663
664SBError SBProcess::Continue() {
665 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
666
667 SBError sb_error;
668 ProcessSP process_sp(GetSP());
669
670 if (log)
671 log->Printf("SBProcess(%p)::Continue ()...",
672 static_cast<void *>(process_sp.get()));
673
674 if (process_sp) {
675 std::lock_guard<std::recursive_mutex> guard(
676 process_sp->GetTarget().GetAPIMutex());
677
678 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
679 sb_error.ref() = process_sp->Resume();
680 else
681 sb_error.ref() = process_sp->ResumeSynchronous(NULL);
682 } else
683 sb_error.SetErrorString("SBProcess is invalid");
684
685 if (log) {
686 SBStream sstr;
687 sb_error.GetDescription(sstr);
688 log->Printf("SBProcess(%p)::Continue () => SBError (%p): %s",
689 static_cast<void *>(process_sp.get()),
690 static_cast<void *>(sb_error.get()), sstr.GetData());
691 }
692
693 return sb_error;
694}
695
696SBError SBProcess::Destroy() {
697 SBError sb_error;
698 ProcessSP process_sp(GetSP());
699 if (process_sp) {
700 std::lock_guard<std::recursive_mutex> guard(
701 process_sp->GetTarget().GetAPIMutex());
702 sb_error.SetError(process_sp->Destroy(false));
703 } else
704 sb_error.SetErrorString("SBProcess is invalid");
705
706 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
707 if (log) {
708 SBStream sstr;
709 sb_error.GetDescription(sstr);
710 log->Printf("SBProcess(%p)::Destroy () => SBError (%p): %s",
711 static_cast<void *>(process_sp.get()),
712 static_cast<void *>(sb_error.get()), sstr.GetData());
713 }
714
715 return sb_error;
716}
717
718SBError SBProcess::Stop() {
719 SBError sb_error;
720 ProcessSP process_sp(GetSP());
721 if (process_sp) {
722 std::lock_guard<std::recursive_mutex> guard(
723 process_sp->GetTarget().GetAPIMutex());
724 sb_error.SetError(process_sp->Halt());
725 } else
726 sb_error.SetErrorString("SBProcess is invalid");
727
728 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
729 if (log) {
730 SBStream sstr;
731 sb_error.GetDescription(sstr);
732 log->Printf("SBProcess(%p)::Stop () => SBError (%p): %s",
733 static_cast<void *>(process_sp.get()),
734 static_cast<void *>(sb_error.get()), sstr.GetData());
735 }
736
737 return sb_error;
738}
739
740SBError SBProcess::Kill() {
741 SBError sb_error;
742 ProcessSP process_sp(GetSP());
743 if (process_sp) {
744 std::lock_guard<std::recursive_mutex> guard(
745 process_sp->GetTarget().GetAPIMutex());
746 sb_error.SetError(process_sp->Destroy(true));
747 } else
748 sb_error.SetErrorString("SBProcess is invalid");
749
750 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
751 if (log) {
752 SBStream sstr;
753 sb_error.GetDescription(sstr);
754 log->Printf("SBProcess(%p)::Kill () => SBError (%p): %s",
755 static_cast<void *>(process_sp.get()),
756 static_cast<void *>(sb_error.get()), sstr.GetData());
757 }
758
759 return sb_error;
760}
761
762SBError SBProcess::Detach() {
763 // FIXME: This should come from a process default.
764 bool keep_stopped = false;
765 return Detach(keep_stopped);
766}
767
768SBError SBProcess::Detach(bool keep_stopped) {
769 SBError sb_error;
770 ProcessSP process_sp(GetSP());
771 if (process_sp) {
772 std::lock_guard<std::recursive_mutex> guard(
773 process_sp->GetTarget().GetAPIMutex());
774 sb_error.SetError(process_sp->Detach(keep_stopped));
775 } else
776 sb_error.SetErrorString("SBProcess is invalid");
777
778 return sb_error;
779}
780
781SBError SBProcess::Signal(int signo) {
782 SBError sb_error;
783 ProcessSP process_sp(GetSP());
784 if (process_sp) {
785 std::lock_guard<std::recursive_mutex> guard(
786 process_sp->GetTarget().GetAPIMutex());
787 sb_error.SetError(process_sp->Signal(signo));
788 } else
789 sb_error.SetErrorString("SBProcess is invalid");
790 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
791 if (log) {
792 SBStream sstr;
793 sb_error.GetDescription(sstr);
794 log->Printf("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
795 static_cast<void *>(process_sp.get()), signo,
796 static_cast<void *>(sb_error.get()), sstr.GetData());
797 }
798 return sb_error;
799}
800
801SBUnixSignals SBProcess::GetUnixSignals() {
802 if (auto process_sp = GetSP())
803 return SBUnixSignals{process_sp};
804
805 return {};
806}
807
808void SBProcess::SendAsyncInterrupt() {
809 ProcessSP process_sp(GetSP());
810 if (process_sp) {
811 process_sp->SendAsyncInterrupt();
812 }
813}
814
815SBThread SBProcess::GetThreadByID(tid_t tid) {
816 SBThread sb_thread;
817 ThreadSP thread_sp;
818 ProcessSP process_sp(GetSP());
819 if (process_sp) {
820 Process::StopLocker stop_locker;
821 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
822 std::lock_guard<std::recursive_mutex> guard(
823 process_sp->GetTarget().GetAPIMutex());
824 thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);
825 sb_thread.SetThread(thread_sp);
826 }
827
828 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
829 if (log)
830 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64
831 ") => SBThread (%p)",
832 static_cast<void *>(process_sp.get()), tid,
833 static_cast<void *>(thread_sp.get()));
834
835 return sb_thread;
836}
837
838SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
839 SBThread sb_thread;
840 ThreadSP thread_sp;
841 ProcessSP process_sp(GetSP());
842 if (process_sp) {
843 Process::StopLocker stop_locker;
844 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
845 std::lock_guard<std::recursive_mutex> guard(
846 process_sp->GetTarget().GetAPIMutex());
847 thread_sp =
848 process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);
849 sb_thread.SetThread(thread_sp);
850 }
851
852 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
853 if (log)
854 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
855 static_cast<void *>(process_sp.get()), index_id,
856 static_cast<void *>(thread_sp.get()));
857
858 return sb_thread;
859}
860
861StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
862 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
863
864 StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
865
866 if (log)
867 log->Printf("SBProcess::GetStateFromEvent (event.sp=%p) => %s",
868 static_cast<void *>(event.get()),
869 lldb_private::StateAsCString(ret_val));
870
871 return ret_val;
872}
873
874bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
875 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
876
877 bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
878
879 if (log)
880 log->Printf("SBProcess::%s (event.sp=%p) => %d", __FUNCTION__,
881 static_cast<void *>(event.get()), ret_val);
882
883 return ret_val;
884}
885
886size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
887 return Process::ProcessEventData::GetNumRestartedReasons(event.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888}
889
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000890const char *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
892 size_t idx) {
893 return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000894}
895
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
897 ProcessSP process_sp =
898 Process::ProcessEventData::GetProcessFromEvent(event.get());
899 if (!process_sp) {
900 // StructuredData events also know the process they come from.
901 // Try that.
902 process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
903 }
904
905 return SBProcess(process_sp);
Jim Inghamd7b30ef2012-10-26 19:18:04 +0000906}
907
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
909 return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
Ilia K06d28552015-05-15 09:29:09 +0000910}
911
Todd Fiala75930012016-08-19 04:21:48 +0000912lldb::SBStructuredData
Kate Stoneb9c1b512016-09-06 20:57:50 +0000913SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
914 return SBStructuredData(event.GetSP());
Todd Fiala75930012016-08-19 04:21:48 +0000915}
916
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
918 return (event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass()) &&
919 !EventIsStructuredDataEvent(event);
Todd Fiala75930012016-08-19 04:21:48 +0000920}
921
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
923 EventSP event_sp = event.GetSP();
924 EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
925 return event_data && (event_data->GetFlavor() ==
926 EventDataStructuredData::GetFlavorString());
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000927}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929SBBroadcaster SBProcess::GetBroadcaster() const {
930 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000931
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932 ProcessSP process_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000933
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 SBBroadcaster broadcaster(process_sp.get(), false);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000935
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 if (log)
937 log->Printf("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",
938 static_cast<void *>(process_sp.get()),
939 static_cast<void *>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000940
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 return broadcaster;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942}
943
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944const char *SBProcess::GetBroadcasterClass() {
945 return Process::GetStaticBroadcasterClass().AsCString();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000946}
947
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
949 SBError &sb_error) {
950 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000951
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952 size_t bytes_read = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000953
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954 ProcessSP process_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000955
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 if (log)
957 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
958 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
959 static_cast<void *>(process_sp.get()), addr,
960 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
961 static_cast<void *>(sb_error.get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000962
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 if (process_sp) {
964 Process::StopLocker stop_locker;
965 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
966 std::lock_guard<std::recursive_mutex> guard(
967 process_sp->GetTarget().GetAPIMutex());
968 bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
969 } else {
970 if (log)
971 log->Printf("SBProcess(%p)::ReadMemory() => error: process is running",
972 static_cast<void *>(process_sp.get()));
973 sb_error.SetErrorString("process is running");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975 } else {
976 sb_error.SetErrorString("SBProcess is invalid");
977 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000978
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979 if (log) {
980 SBStream sstr;
981 sb_error.GetDescription(sstr);
982 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
983 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
984 static_cast<void *>(process_sp.get()), addr,
985 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
986 static_cast<void *>(sb_error.get()), sstr.GetData(),
987 static_cast<uint64_t>(bytes_read));
988 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000989
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 return bytes_read;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000991}
992
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
994 lldb::SBError &sb_error) {
995 size_t bytes_read = 0;
996 ProcessSP process_sp(GetSP());
997 if (process_sp) {
998 Process::StopLocker stop_locker;
999 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1000 std::lock_guard<std::recursive_mutex> guard(
1001 process_sp->GetTarget().GetAPIMutex());
1002 bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
1003 sb_error.ref());
1004 } else {
1005 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1006 if (log)
1007 log->Printf("SBProcess(%p)::ReadCStringFromMemory() => error: process "
1008 "is running",
1009 static_cast<void *>(process_sp.get()));
1010 sb_error.SetErrorString("process is running");
Greg Claytone91b7952011-12-15 03:14:23 +00001011 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 } else {
1013 sb_error.SetErrorString("SBProcess is invalid");
1014 }
1015 return bytes_read;
Greg Claytone91b7952011-12-15 03:14:23 +00001016}
1017
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
1019 lldb::SBError &sb_error) {
1020 uint64_t value = 0;
1021 ProcessSP process_sp(GetSP());
1022 if (process_sp) {
1023 Process::StopLocker stop_locker;
1024 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1025 std::lock_guard<std::recursive_mutex> guard(
1026 process_sp->GetTarget().GetAPIMutex());
1027 value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
1028 sb_error.ref());
1029 } else {
1030 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1031 if (log)
1032 log->Printf("SBProcess(%p)::ReadUnsignedFromMemory() => error: process "
1033 "is running",
1034 static_cast<void *>(process_sp.get()));
1035 sb_error.SetErrorString("process is running");
Greg Claytone91b7952011-12-15 03:14:23 +00001036 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 } else {
1038 sb_error.SetErrorString("SBProcess is invalid");
1039 }
1040 return value;
Greg Claytone91b7952011-12-15 03:14:23 +00001041}
1042
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
1044 lldb::SBError &sb_error) {
1045 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1046 ProcessSP process_sp(GetSP());
1047 if (process_sp) {
1048 Process::StopLocker stop_locker;
1049 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1050 std::lock_guard<std::recursive_mutex> guard(
1051 process_sp->GetTarget().GetAPIMutex());
1052 ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
1053 } else {
1054 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1055 if (log)
1056 log->Printf("SBProcess(%p)::ReadPointerFromMemory() => error: process "
1057 "is running",
1058 static_cast<void *>(process_sp.get()));
1059 sb_error.SetErrorString("process is running");
Greg Claytone91b7952011-12-15 03:14:23 +00001060 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 } else {
1062 sb_error.SetErrorString("SBProcess is invalid");
1063 }
1064 return ptr;
Greg Claytone91b7952011-12-15 03:14:23 +00001065}
1066
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
1068 SBError &sb_error) {
1069 size_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Claytonacdbe812012-01-30 09:04:36 +00001072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 ProcessSP process_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +00001074
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 if (log)
1076 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1077 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1078 static_cast<void *>(process_sp.get()), addr,
1079 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1080 static_cast<void *>(sb_error.get()));
Greg Clayton48381312010-10-30 04:51:46 +00001081
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 if (process_sp) {
1083 Process::StopLocker stop_locker;
1084 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1085 std::lock_guard<std::recursive_mutex> guard(
1086 process_sp->GetTarget().GetAPIMutex());
1087 bytes_written =
1088 process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
1089 } else {
1090 if (log)
1091 log->Printf("SBProcess(%p)::WriteMemory() => error: process is running",
1092 static_cast<void *>(process_sp.get()));
1093 sb_error.SetErrorString("process is running");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 if (log) {
1098 SBStream sstr;
1099 sb_error.GetDescription(sstr);
1100 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1101 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1102 static_cast<void *>(process_sp.get()), addr,
1103 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1104 static_cast<void *>(sb_error.get()), sstr.GetData(),
1105 static_cast<uint64_t>(bytes_written));
1106 }
Greg Clayton48381312010-10-30 04:51:46 +00001107
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109}
1110
Kate Stoneb9c1b512016-09-06 20:57:50 +00001111bool SBProcess::GetDescription(SBStream &description) {
1112 Stream &strm = description.ref();
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001113
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 ProcessSP process_sp(GetSP());
1115 if (process_sp) {
1116 char path[PATH_MAX];
1117 GetTarget().GetExecutable().GetPath(path, sizeof(path));
1118 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1119 const char *exe_name = NULL;
1120 if (exe_module)
1121 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
Greg Clayton1d273162010-10-06 03:09:58 +00001122
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123 strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1124 process_sp->GetID(), lldb_private::StateAsCString(GetState()),
1125 GetNumThreads(), exe_name ? ", executable = " : "",
1126 exe_name ? exe_name : "");
1127 } else
1128 strm.PutCString("No value");
Caroline Ticedde9cff2010-09-20 05:20:02 +00001129
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 return true;
Caroline Ticedde9cff2010-09-20 05:20:02 +00001131}
Greg Clayton8f343b02010-11-04 01:54:29 +00001132
1133uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1135 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Johnny Chenf9ef60d2012-05-23 22:34:34 +00001136
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137 uint32_t num = 0;
1138 ProcessSP process_sp(GetSP());
1139 if (process_sp) {
1140 std::lock_guard<std::recursive_mutex> guard(
1141 process_sp->GetTarget().GetAPIMutex());
1142 sb_error.SetError(process_sp->GetWatchpointSupportInfo(num));
1143 if (log)
1144 log->Printf("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1145 static_cast<void *>(process_sp.get()), num);
1146 } else {
1147 sb_error.SetErrorString("SBProcess is invalid");
1148 }
1149 return num;
Johnny Chenf9ef60d2012-05-23 22:34:34 +00001150}
1151
Kate Stoneb9c1b512016-09-06 20:57:50 +00001152uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
1153 lldb::SBError &sb_error) {
1154 return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001155}
1156
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
1158 const lldb::SBFileSpec &sb_remote_image_spec,
1159 lldb::SBError &sb_error) {
1160 ProcessSP process_sp(GetSP());
1161 if (process_sp) {
1162 Process::StopLocker stop_locker;
1163 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1164 std::lock_guard<std::recursive_mutex> guard(
1165 process_sp->GetTarget().GetAPIMutex());
1166 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1167 return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
1168 *sb_remote_image_spec, sb_error.ref());
1169 } else {
1170 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1171 if (log)
1172 log->Printf("SBProcess(%p)::LoadImage() => error: process is running",
1173 static_cast<void *>(process_sp.get()));
1174 sb_error.SetErrorString("process is running");
Greg Claytonaf67cec2010-12-20 20:49:23 +00001175 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001176 }
1177 return LLDB_INVALID_IMAGE_TOKEN;
Greg Clayton8f343b02010-11-04 01:54:29 +00001178}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001179
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1181 lldb::SBError sb_error;
1182 ProcessSP process_sp(GetSP());
1183 if (process_sp) {
1184 Process::StopLocker stop_locker;
1185 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1186 std::lock_guard<std::recursive_mutex> guard(
1187 process_sp->GetTarget().GetAPIMutex());
1188 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1189 sb_error.SetError(
1190 platform_sp->UnloadImage(process_sp.get(), image_token));
1191 } else {
1192 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1193 if (log)
1194 log->Printf("SBProcess(%p)::UnloadImage() => error: process is running",
1195 static_cast<void *>(process_sp.get()));
1196 sb_error.SetErrorString("process is running");
Greg Claytonaf67cec2010-12-20 20:49:23 +00001197 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001198 } else
1199 sb_error.SetErrorString("invalid process");
1200 return sb_error;
Greg Clayton8f343b02010-11-04 01:54:29 +00001201}
Jason Molenda8c713372013-11-05 11:00:35 +00001202
Kate Stoneb9c1b512016-09-06 20:57:50 +00001203lldb::SBError SBProcess::SendEventData(const char *event_data) {
1204 lldb::SBError sb_error;
1205 ProcessSP process_sp(GetSP());
1206 if (process_sp) {
1207 Process::StopLocker stop_locker;
1208 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1209 std::lock_guard<std::recursive_mutex> guard(
1210 process_sp->GetTarget().GetAPIMutex());
1211 sb_error.SetError(process_sp->SendEventData(event_data));
1212 } else {
1213 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1214 if (log)
1215 log->Printf(
1216 "SBProcess(%p)::SendEventData() => error: process is running",
1217 static_cast<void *>(process_sp.get()));
1218 sb_error.SetErrorString("process is running");
Jason Molendaa3329782014-03-29 18:54:20 +00001219 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220 } else
1221 sb_error.SetErrorString("invalid process");
1222 return sb_error;
Jason Molendaa3329782014-03-29 18:54:20 +00001223}
1224
Kate Stoneb9c1b512016-09-06 20:57:50 +00001225uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1226 ProcessSP process_sp(GetSP());
1227 if (process_sp && process_sp->GetSystemRuntime()) {
1228 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1229 return runtime->GetExtendedBacktraceTypes().size();
1230 }
1231 return 0;
Jason Molenda8c713372013-11-05 11:00:35 +00001232}
1233
Kate Stoneb9c1b512016-09-06 20:57:50 +00001234const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1235 ProcessSP process_sp(GetSP());
1236 if (process_sp && process_sp->GetSystemRuntime()) {
1237 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1238 const std::vector<ConstString> &names =
1239 runtime->GetExtendedBacktraceTypes();
1240 if (idx < names.size()) {
1241 return names[idx].AsCString();
1242 } else {
1243 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1244 if (log)
1245 log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => "
1246 "error: requested extended backtrace name out of bounds",
1247 static_cast<void *>(process_sp.get()));
Jason Molenda8c713372013-11-05 11:00:35 +00001248 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249 }
1250 return NULL;
Jason Molenda8c713372013-11-05 11:00:35 +00001251}
Kuba Breckaa51ea382014-09-06 01:33:13 +00001252
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1254 ProcessSP process_sp(GetSP());
1255 SBThreadCollection threads;
1256 if (process_sp) {
1257 threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1258 }
1259 return threads;
Kuba Breckaa51ea382014-09-06 01:33:13 +00001260}
Kuba Brecka63927542014-10-11 01:59:32 +00001261
Kate Stoneb9c1b512016-09-06 20:57:50 +00001262bool SBProcess::IsInstrumentationRuntimePresent(
1263 InstrumentationRuntimeType type) {
1264 ProcessSP process_sp(GetSP());
1265 if (!process_sp)
1266 return false;
1267
1268 InstrumentationRuntimeSP runtime_sp =
1269 process_sp->GetInstrumentationRuntime(type);
1270
1271 if (!runtime_sp.get())
1272 return false;
1273
1274 return runtime_sp->IsActive();
Kuba Brecka63927542014-10-11 01:59:32 +00001275}
Adrian McCarthyf7d18932015-11-20 23:09:11 +00001276
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277lldb::SBError SBProcess::SaveCore(const char *file_name) {
1278 lldb::SBError error;
1279 ProcessSP process_sp(GetSP());
1280 if (!process_sp) {
1281 error.SetErrorString("SBProcess is invalid");
Adrian McCarthyf7d18932015-11-20 23:09:11 +00001282 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 }
1284
1285 std::lock_guard<std::recursive_mutex> guard(
1286 process_sp->GetTarget().GetAPIMutex());
1287
1288 if (process_sp->GetState() != eStateStopped) {
1289 error.SetErrorString("the process is not stopped");
1290 return error;
1291 }
1292
1293 FileSpec core_file(file_name, false);
1294 error.ref() = PluginManager::SaveCore(process_sp, core_file);
1295 return error;
Adrian McCarthyf7d18932015-11-20 23:09:11 +00001296}
Howard Hellyer260368432016-06-23 08:35:37 +00001297
1298lldb::SBError
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1300 SBMemoryRegionInfo &sb_region_info) {
1301 lldb::SBError sb_error;
1302 ProcessSP process_sp(GetSP());
1303 MemoryRegionInfoSP region_info_sp =
1304 std::make_shared<lldb_private::MemoryRegionInfo>();
1305 if (process_sp) {
1306 Process::StopLocker stop_locker;
1307 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1308 std::lock_guard<std::recursive_mutex> guard(
1309 process_sp->GetTarget().GetAPIMutex());
1310 sb_error.ref() =
1311 process_sp->GetMemoryRegionInfo(load_addr, *region_info_sp);
1312 if (sb_error.Success()) {
1313 sb_region_info.ref() = *region_info_sp;
1314 }
1315 } else {
1316 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1317 if (log)
1318 log->Printf(
1319 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1320 static_cast<void *>(process_sp.get()));
1321 sb_error.SetErrorString("process is running");
Howard Hellyer260368432016-06-23 08:35:37 +00001322 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001323 } else {
1324 sb_error.SetErrorString("SBProcess is invalid");
1325 }
1326 return sb_error;
Howard Hellyer260368432016-06-23 08:35:37 +00001327}
1328
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1330 lldb::SBError sb_error;
1331 lldb::SBMemoryRegionInfoList sb_region_list;
1332 ProcessSP process_sp(GetSP());
1333 if (process_sp) {
1334 Process::StopLocker stop_locker;
1335 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1336 std::lock_guard<std::recursive_mutex> guard(
1337 process_sp->GetTarget().GetAPIMutex());
1338 std::vector<MemoryRegionInfoSP> region_list;
1339 sb_error.ref() = process_sp->GetMemoryRegions(region_list);
1340 if (sb_error.Success()) {
1341 std::vector<MemoryRegionInfoSP>::iterator end = region_list.end();
1342 for (std::vector<MemoryRegionInfoSP>::iterator it = region_list.begin();
1343 it != end; it++) {
1344 SBMemoryRegionInfo sb_region_info(it->get());
1345 sb_region_list.Append(sb_region_info);
Howard Hellyer260368432016-06-23 08:35:37 +00001346 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347 }
1348 } else {
1349 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1350 if (log)
1351 log->Printf(
1352 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1353 static_cast<void *>(process_sp.get()));
1354 sb_error.SetErrorString("process is running");
Howard Hellyer260368432016-06-23 08:35:37 +00001355 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001356 } else {
1357 sb_error.SetErrorString("SBProcess is invalid");
1358 }
1359 return sb_region_list;
Howard Hellyer260368432016-06-23 08:35:37 +00001360}