blob: 37ceb5edcae19f02f5b0a7cd4471b93dab301a87 [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"
Caroline Ticeceb6b132010-10-26 03:11:13 +000019#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Core/Module.h"
Adrian McCarthyf7d18932015-11-20 23:09:11 +000021#include "lldb/Core/PluginManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/StreamFile.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "lldb/Interpreter/Args.h"
Howard Hellyer260368432016-06-23 08:35:37 +000025#include "lldb/Target/MemoryRegionInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/Process.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Target/RegisterContext.h"
Jason Molenda8c713372013-11-05 11:00:35 +000028#include "lldb/Target/SystemRuntime.h"
Greg Clayton66111032010-06-23 01:19:29 +000029#include "lldb/Target/Target.h"
30#include "lldb/Target/Thread.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"
Todd Fiala802dc4022014-06-23 19:30:49 +000047#include "lldb/API/SBUnixSignals.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
49using namespace lldb;
50using namespace lldb_private;
51
Kate Stoneb9c1b512016-09-06 20:57:50 +000052SBProcess::SBProcess() : m_opaque_wp() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54//----------------------------------------------------------------------
55// SBProcess constructor
56//----------------------------------------------------------------------
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060SBProcess::SBProcess(const lldb::ProcessSP &process_sp)
61 : m_opaque_wp(process_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063const SBProcess &SBProcess::operator=(const SBProcess &rhs) {
64 if (this != &rhs)
65 m_opaque_wp = rhs.m_opaque_wp;
66 return *this;
Greg Claytonefabb122010-11-05 23:17:00 +000067}
68
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069//----------------------------------------------------------------------
70// Destructor
71//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000072SBProcess::~SBProcess() {}
73
74const char *SBProcess::GetBroadcasterClassName() {
75 return Process::GetStaticBroadcasterClass().AsCString();
76}
77
78const char *SBProcess::GetPluginName() {
79 ProcessSP process_sp(GetSP());
80 if (process_sp) {
81 return process_sp->GetPluginName().GetCString();
82 }
83 return "<Unknown>";
84}
85
86const char *SBProcess::GetShortPluginName() {
87 ProcessSP process_sp(GetSP());
88 if (process_sp) {
89 return process_sp->GetPluginName().GetCString();
90 }
91 return "<Unknown>";
92}
93
94lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }
95
96void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
97
98void SBProcess::Clear() { m_opaque_wp.reset(); }
99
100bool SBProcess::IsValid() const {
101 ProcessSP process_sp(m_opaque_wp.lock());
102 return ((bool)process_sp && process_sp->IsValid());
103}
104
105bool SBProcess::RemoteLaunch(char const **argv, char const **envp,
106 const char *stdin_path, const char *stdout_path,
107 const char *stderr_path,
108 const char *working_directory,
109 uint32_t launch_flags, bool stop_at_entry,
110 lldb::SBError &error) {
111 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
112 if (log)
113 log->Printf("SBProcess(%p)::RemoteLaunch (argv=%p, envp=%p, stdin=%s, "
114 "stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, "
115 "stop_at_entry=%i, &error (%p))...",
116 static_cast<void *>(m_opaque_wp.lock().get()),
117 static_cast<void *>(argv), static_cast<void *>(envp),
118 stdin_path ? stdin_path : "NULL",
119 stdout_path ? stdout_path : "NULL",
120 stderr_path ? stderr_path : "NULL",
121 working_directory ? working_directory : "NULL", launch_flags,
122 stop_at_entry, static_cast<void *>(error.get()));
123
124 ProcessSP process_sp(GetSP());
125 if (process_sp) {
126 std::lock_guard<std::recursive_mutex> guard(
127 process_sp->GetTarget().GetAPIMutex());
128 if (process_sp->GetState() == eStateConnected) {
129 if (stop_at_entry)
130 launch_flags |= eLaunchFlagStopAtEntry;
131 ProcessLaunchInfo launch_info(
132 FileSpec{stdin_path, false}, FileSpec{stdout_path, false},
133 FileSpec{stderr_path, false}, FileSpec{working_directory, false},
134 launch_flags);
135 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
136 if (exe_module)
137 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
138 if (argv)
139 launch_info.GetArguments().AppendArguments(argv);
140 if (envp)
141 launch_info.GetEnvironmentEntries().SetArguments(envp);
142 error.SetError(process_sp->Launch(launch_info));
143 } else {
144 error.SetErrorString("must be in eStateConnected to call RemoteLaunch");
145 }
146 } else {
147 error.SetErrorString("unable to attach pid");
148 }
149
150 if (log) {
151 SBStream sstr;
152 error.GetDescription(sstr);
153 log->Printf("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s",
154 static_cast<void *>(process_sp.get()),
155 static_cast<void *>(error.get()), sstr.GetData());
156 }
157
158 return error.Success();
159}
160
161bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
162 lldb::SBError &error) {
163 ProcessSP process_sp(GetSP());
164 if (process_sp) {
165 std::lock_guard<std::recursive_mutex> guard(
166 process_sp->GetTarget().GetAPIMutex());
167 if (process_sp->GetState() == eStateConnected) {
168 ProcessAttachInfo attach_info;
169 attach_info.SetProcessID(pid);
170 error.SetError(process_sp->Attach(attach_info));
171 } else {
172 error.SetErrorString(
173 "must be in eStateConnected to call RemoteAttachToProcessWithID");
174 }
175 } else {
176 error.SetErrorString("unable to attach pid");
177 }
178
179 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
180 if (log) {
181 SBStream sstr;
182 error.GetDescription(sstr);
183 log->Printf("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64
184 ") => SBError (%p): %s",
185 static_cast<void *>(process_sp.get()), pid,
186 static_cast<void *>(error.get()), sstr.GetData());
187 }
188
189 return error.Success();
190}
191
192uint32_t SBProcess::GetNumThreads() {
193 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
194
195 uint32_t num_threads = 0;
196 ProcessSP process_sp(GetSP());
197 if (process_sp) {
198 Process::StopLocker stop_locker;
199
200 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
201 std::lock_guard<std::recursive_mutex> guard(
202 process_sp->GetTarget().GetAPIMutex());
203 num_threads = process_sp->GetThreadList().GetSize(can_update);
204 }
205
206 if (log)
207 log->Printf("SBProcess(%p)::GetNumThreads () => %d",
208 static_cast<void *>(process_sp.get()), num_threads);
209
210 return num_threads;
211}
212
213SBThread SBProcess::GetSelectedThread() const {
214 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
215
216 SBThread sb_thread;
217 ThreadSP thread_sp;
218 ProcessSP process_sp(GetSP());
219 if (process_sp) {
220 std::lock_guard<std::recursive_mutex> guard(
221 process_sp->GetTarget().GetAPIMutex());
222 thread_sp = process_sp->GetThreadList().GetSelectedThread();
223 sb_thread.SetThread(thread_sp);
224 }
225
226 if (log)
227 log->Printf("SBProcess(%p)::GetSelectedThread () => SBThread(%p)",
228 static_cast<void *>(process_sp.get()),
229 static_cast<void *>(thread_sp.get()));
230
231 return sb_thread;
232}
233
234SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
235 lldb::addr_t context) {
236 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
237
238 SBThread sb_thread;
239 ThreadSP thread_sp;
240 ProcessSP process_sp(GetSP());
241 if (process_sp) {
242 std::lock_guard<std::recursive_mutex> guard(
243 process_sp->GetTarget().GetAPIMutex());
244 thread_sp = process_sp->CreateOSPluginThread(tid, context);
245 sb_thread.SetThread(thread_sp);
246 }
247
248 if (log)
249 log->Printf("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64
250 ", context=0x%" PRIx64 ") => SBThread(%p)",
251 static_cast<void *>(process_sp.get()), tid, context,
252 static_cast<void *>(thread_sp.get()));
253
254 return sb_thread;
255}
256
257SBTarget SBProcess::GetTarget() const {
258 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
259
260 SBTarget sb_target;
261 TargetSP target_sp;
262 ProcessSP process_sp(GetSP());
263 if (process_sp) {
264 target_sp = process_sp->GetTarget().shared_from_this();
265 sb_target.SetSP(target_sp);
266 }
267
268 if (log)
269 log->Printf("SBProcess(%p)::GetTarget () => SBTarget(%p)",
270 static_cast<void *>(process_sp.get()),
271 static_cast<void *>(target_sp.get()));
272
273 return sb_target;
274}
275
276size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
277 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
278
279 size_t ret_val = 0;
280 ProcessSP process_sp(GetSP());
281 if (process_sp) {
282 Error error;
283 ret_val = process_sp->PutSTDIN(src, src_len, error);
284 }
285
286 if (log)
287 log->Printf("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%" PRIu64
288 ") => %" PRIu64,
289 static_cast<void *>(process_sp.get()), src,
290 static_cast<uint64_t>(src_len), static_cast<uint64_t>(ret_val));
291
292 return ret_val;
293}
294
295size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
296 size_t bytes_read = 0;
297 ProcessSP process_sp(GetSP());
298 if (process_sp) {
299 Error error;
300 bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);
301 }
302
303 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
304 if (log)
305 log->Printf(
306 "SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64
307 ") => %" PRIu64,
308 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
309 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
310
311 return bytes_read;
312}
313
314size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
315 size_t bytes_read = 0;
316 ProcessSP process_sp(GetSP());
317 if (process_sp) {
318 Error error;
319 bytes_read = process_sp->GetSTDERR(dst, dst_len, error);
320 }
321
322 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
323 if (log)
324 log->Printf(
325 "SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64
326 ") => %" PRIu64,
327 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
328 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
329
330 return bytes_read;
331}
332
333size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
334 size_t bytes_read = 0;
335 ProcessSP process_sp(GetSP());
336 if (process_sp) {
337 Error error;
338 bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);
339 }
340
341 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
342 if (log)
343 log->Printf(
344 "SBProcess(%p)::GetAsyncProfileData (dst=\"%.*s\", dst_len=%" PRIu64
345 ") => %" PRIu64,
346 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
347 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
348
349 return bytes_read;
350}
351
352void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
353 if (out == NULL)
354 return;
355
356 ProcessSP process_sp(GetSP());
357 if (process_sp) {
358 const StateType event_state = SBProcess::GetStateFromEvent(event);
359 char message[1024];
360 int message_len = ::snprintf(
361 message, sizeof(message), "Process %" PRIu64 " %s\n",
362 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
363
364 if (message_len > 0)
365 ::fwrite(message, 1, message_len, out);
366 }
367}
368
369void SBProcess::AppendEventStateReport(const SBEvent &event,
370 SBCommandReturnObject &result) {
371 ProcessSP process_sp(GetSP());
372 if (process_sp) {
373 const StateType event_state = SBProcess::GetStateFromEvent(event);
374 char message[1024];
375 ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",
376 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
377
378 result.AppendMessage(message);
379 }
380}
381
382bool SBProcess::SetSelectedThread(const SBThread &thread) {
383 ProcessSP process_sp(GetSP());
384 if (process_sp) {
385 std::lock_guard<std::recursive_mutex> guard(
386 process_sp->GetTarget().GetAPIMutex());
387 return process_sp->GetThreadList().SetSelectedThreadByID(
388 thread.GetThreadID());
389 }
390 return false;
391}
392
393bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
394 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
395
396 bool ret_val = false;
397 ProcessSP process_sp(GetSP());
398 if (process_sp) {
399 std::lock_guard<std::recursive_mutex> guard(
400 process_sp->GetTarget().GetAPIMutex());
401 ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);
402 }
403
404 if (log)
405 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64
406 ") => %s",
407 static_cast<void *>(process_sp.get()), tid,
408 (ret_val ? "true" : "false"));
409
410 return ret_val;
411}
412
413bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
414 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
415
416 bool ret_val = false;
417 ProcessSP process_sp(GetSP());
418 if (process_sp) {
419 std::lock_guard<std::recursive_mutex> guard(
420 process_sp->GetTarget().GetAPIMutex());
421 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);
422 }
423
424 if (log)
425 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
426 static_cast<void *>(process_sp.get()), index_id,
427 (ret_val ? "true" : "false"));
428
429 return ret_val;
430}
431
432SBThread SBProcess::GetThreadAtIndex(size_t index) {
433 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
434
435 SBThread sb_thread;
436 ThreadSP thread_sp;
437 ProcessSP process_sp(GetSP());
438 if (process_sp) {
439 Process::StopLocker stop_locker;
440 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
441 std::lock_guard<std::recursive_mutex> guard(
442 process_sp->GetTarget().GetAPIMutex());
443 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
444 sb_thread.SetThread(thread_sp);
445 }
446
447 if (log)
448 log->Printf("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
449 static_cast<void *>(process_sp.get()),
450 static_cast<uint32_t>(index),
451 static_cast<void *>(thread_sp.get()));
452
453 return sb_thread;
454}
455
456uint32_t SBProcess::GetNumQueues() {
457 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
458
459 uint32_t num_queues = 0;
460 ProcessSP process_sp(GetSP());
461 if (process_sp) {
462 Process::StopLocker stop_locker;
463 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
464 std::lock_guard<std::recursive_mutex> guard(
465 process_sp->GetTarget().GetAPIMutex());
466 num_queues = process_sp->GetQueueList().GetSize();
467 }
468 }
469
470 if (log)
471 log->Printf("SBProcess(%p)::GetNumQueues () => %d",
472 static_cast<void *>(process_sp.get()), num_queues);
473
474 return num_queues;
475}
476
477SBQueue SBProcess::GetQueueAtIndex(size_t index) {
478 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
479
480 SBQueue sb_queue;
481 QueueSP queue_sp;
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 queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
489 sb_queue.SetQueue(queue_sp);
490 }
491 }
492
493 if (log)
494 log->Printf("SBProcess(%p)::GetQueueAtIndex (index=%d) => SBQueue(%p)",
495 static_cast<void *>(process_sp.get()),
496 static_cast<uint32_t>(index),
497 static_cast<void *>(queue_sp.get()));
498
499 return sb_queue;
500}
501
502uint32_t SBProcess::GetStopID(bool include_expression_stops) {
503 ProcessSP process_sp(GetSP());
504 if (process_sp) {
505 std::lock_guard<std::recursive_mutex> guard(
506 process_sp->GetTarget().GetAPIMutex());
507 if (include_expression_stops)
508 return process_sp->GetStopID();
509 else
510 return process_sp->GetLastNaturalStopID();
511 }
512 return 0;
513}
514
515SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
516 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
517
518 SBEvent sb_event;
519 EventSP event_sp;
520 ProcessSP process_sp(GetSP());
521 if (process_sp) {
522 std::lock_guard<std::recursive_mutex> guard(
523 process_sp->GetTarget().GetAPIMutex());
524 event_sp = process_sp->GetStopEventForStopID(stop_id);
525 sb_event.reset(event_sp);
526 }
527
528 if (log)
529 log->Printf("SBProcess(%p)::GetStopEventForStopID (stop_id=%" PRIu32
530 ") => SBEvent(%p)",
531 static_cast<void *>(process_sp.get()), stop_id,
532 static_cast<void *>(event_sp.get()));
533
534 return sb_event;
535}
536
537StateType SBProcess::GetState() {
538
539 StateType ret_val = eStateInvalid;
540 ProcessSP process_sp(GetSP());
541 if (process_sp) {
542 std::lock_guard<std::recursive_mutex> guard(
543 process_sp->GetTarget().GetAPIMutex());
544 ret_val = process_sp->GetState();
545 }
546
547 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
548 if (log)
549 log->Printf("SBProcess(%p)::GetState () => %s",
550 static_cast<void *>(process_sp.get()),
551 lldb_private::StateAsCString(ret_val));
552
553 return ret_val;
554}
555
556int SBProcess::GetExitStatus() {
557 int exit_status = 0;
558 ProcessSP process_sp(GetSP());
559 if (process_sp) {
560 std::lock_guard<std::recursive_mutex> guard(
561 process_sp->GetTarget().GetAPIMutex());
562 exit_status = process_sp->GetExitStatus();
563 }
564 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
565 if (log)
566 log->Printf("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
567 static_cast<void *>(process_sp.get()), exit_status,
568 exit_status);
569
570 return exit_status;
571}
572
573const char *SBProcess::GetExitDescription() {
574 const char *exit_desc = NULL;
575 ProcessSP process_sp(GetSP());
576 if (process_sp) {
577 std::lock_guard<std::recursive_mutex> guard(
578 process_sp->GetTarget().GetAPIMutex());
579 exit_desc = process_sp->GetExitDescription();
580 }
581 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
582 if (log)
583 log->Printf("SBProcess(%p)::GetExitDescription () => %s",
584 static_cast<void *>(process_sp.get()), exit_desc);
585 return exit_desc;
586}
587
588lldb::pid_t SBProcess::GetProcessID() {
589 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
590 ProcessSP process_sp(GetSP());
591 if (process_sp)
592 ret_val = process_sp->GetID();
593
594 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
595 if (log)
596 log->Printf("SBProcess(%p)::GetProcessID () => %" PRIu64,
597 static_cast<void *>(process_sp.get()), ret_val);
598
599 return ret_val;
600}
601
602uint32_t SBProcess::GetUniqueID() {
603 uint32_t ret_val = 0;
604 ProcessSP process_sp(GetSP());
605 if (process_sp)
606 ret_val = process_sp->GetUniqueID();
607 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
608 if (log)
609 log->Printf("SBProcess(%p)::GetUniqueID () => %" PRIu32,
610 static_cast<void *>(process_sp.get()), ret_val);
611 return ret_val;
612}
613
614ByteOrder SBProcess::GetByteOrder() const {
615 ByteOrder byteOrder = eByteOrderInvalid;
616 ProcessSP process_sp(GetSP());
617 if (process_sp)
618 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
619
620 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
621 if (log)
622 log->Printf("SBProcess(%p)::GetByteOrder () => %d",
623 static_cast<void *>(process_sp.get()), byteOrder);
624
625 return byteOrder;
626}
627
628uint32_t SBProcess::GetAddressByteSize() const {
629 uint32_t size = 0;
630 ProcessSP process_sp(GetSP());
631 if (process_sp)
632 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
633
634 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
635 if (log)
636 log->Printf("SBProcess(%p)::GetAddressByteSize () => %d",
637 static_cast<void *>(process_sp.get()), size);
638
639 return size;
640}
641
642SBError SBProcess::Continue() {
643 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
644
645 SBError sb_error;
646 ProcessSP process_sp(GetSP());
647
648 if (log)
649 log->Printf("SBProcess(%p)::Continue ()...",
650 static_cast<void *>(process_sp.get()));
651
652 if (process_sp) {
653 std::lock_guard<std::recursive_mutex> guard(
654 process_sp->GetTarget().GetAPIMutex());
655
656 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
657 sb_error.ref() = process_sp->Resume();
658 else
659 sb_error.ref() = process_sp->ResumeSynchronous(NULL);
660 } else
661 sb_error.SetErrorString("SBProcess is invalid");
662
663 if (log) {
664 SBStream sstr;
665 sb_error.GetDescription(sstr);
666 log->Printf("SBProcess(%p)::Continue () => SBError (%p): %s",
667 static_cast<void *>(process_sp.get()),
668 static_cast<void *>(sb_error.get()), sstr.GetData());
669 }
670
671 return sb_error;
672}
673
674SBError SBProcess::Destroy() {
675 SBError sb_error;
676 ProcessSP process_sp(GetSP());
677 if (process_sp) {
678 std::lock_guard<std::recursive_mutex> guard(
679 process_sp->GetTarget().GetAPIMutex());
680 sb_error.SetError(process_sp->Destroy(false));
681 } else
682 sb_error.SetErrorString("SBProcess is invalid");
683
684 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
685 if (log) {
686 SBStream sstr;
687 sb_error.GetDescription(sstr);
688 log->Printf("SBProcess(%p)::Destroy () => 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::Stop() {
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->Halt());
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)::Stop () => 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::Kill() {
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->Destroy(true));
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)::Kill () => 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::Detach() {
741 // FIXME: This should come from a process default.
742 bool keep_stopped = false;
743 return Detach(keep_stopped);
744}
745
746SBError SBProcess::Detach(bool keep_stopped) {
747 SBError sb_error;
748 ProcessSP process_sp(GetSP());
749 if (process_sp) {
750 std::lock_guard<std::recursive_mutex> guard(
751 process_sp->GetTarget().GetAPIMutex());
752 sb_error.SetError(process_sp->Detach(keep_stopped));
753 } else
754 sb_error.SetErrorString("SBProcess is invalid");
755
756 return sb_error;
757}
758
759SBError SBProcess::Signal(int signo) {
760 SBError sb_error;
761 ProcessSP process_sp(GetSP());
762 if (process_sp) {
763 std::lock_guard<std::recursive_mutex> guard(
764 process_sp->GetTarget().GetAPIMutex());
765 sb_error.SetError(process_sp->Signal(signo));
766 } else
767 sb_error.SetErrorString("SBProcess is invalid");
768 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
769 if (log) {
770 SBStream sstr;
771 sb_error.GetDescription(sstr);
772 log->Printf("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
773 static_cast<void *>(process_sp.get()), signo,
774 static_cast<void *>(sb_error.get()), sstr.GetData());
775 }
776 return sb_error;
777}
778
779SBUnixSignals SBProcess::GetUnixSignals() {
780 if (auto process_sp = GetSP())
781 return SBUnixSignals{process_sp};
782
783 return {};
784}
785
786void SBProcess::SendAsyncInterrupt() {
787 ProcessSP process_sp(GetSP());
788 if (process_sp) {
789 process_sp->SendAsyncInterrupt();
790 }
791}
792
793SBThread SBProcess::GetThreadByID(tid_t tid) {
794 SBThread sb_thread;
795 ThreadSP thread_sp;
796 ProcessSP process_sp(GetSP());
797 if (process_sp) {
798 Process::StopLocker stop_locker;
799 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
800 std::lock_guard<std::recursive_mutex> guard(
801 process_sp->GetTarget().GetAPIMutex());
802 thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);
803 sb_thread.SetThread(thread_sp);
804 }
805
806 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
807 if (log)
808 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64
809 ") => SBThread (%p)",
810 static_cast<void *>(process_sp.get()), tid,
811 static_cast<void *>(thread_sp.get()));
812
813 return sb_thread;
814}
815
816SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
817 SBThread sb_thread;
818 ThreadSP thread_sp;
819 ProcessSP process_sp(GetSP());
820 if (process_sp) {
821 Process::StopLocker stop_locker;
822 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
823 std::lock_guard<std::recursive_mutex> guard(
824 process_sp->GetTarget().GetAPIMutex());
825 thread_sp =
826 process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);
827 sb_thread.SetThread(thread_sp);
828 }
829
830 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
831 if (log)
832 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
833 static_cast<void *>(process_sp.get()), index_id,
834 static_cast<void *>(thread_sp.get()));
835
836 return sb_thread;
837}
838
839StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
840 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
841
842 StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
843
844 if (log)
845 log->Printf("SBProcess::GetStateFromEvent (event.sp=%p) => %s",
846 static_cast<void *>(event.get()),
847 lldb_private::StateAsCString(ret_val));
848
849 return ret_val;
850}
851
852bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
853 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
854
855 bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
856
857 if (log)
858 log->Printf("SBProcess::%s (event.sp=%p) => %d", __FUNCTION__,
859 static_cast<void *>(event.get()), ret_val);
860
861 return ret_val;
862}
863
864size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
865 return Process::ProcessEventData::GetNumRestartedReasons(event.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866}
867
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000868const char *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
870 size_t idx) {
871 return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000872}
873
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
875 ProcessSP process_sp =
876 Process::ProcessEventData::GetProcessFromEvent(event.get());
877 if (!process_sp) {
878 // StructuredData events also know the process they come from.
879 // Try that.
880 process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
881 }
882
883 return SBProcess(process_sp);
Jim Inghamd7b30ef2012-10-26 19:18:04 +0000884}
885
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
887 return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
Ilia K06d28552015-05-15 09:29:09 +0000888}
889
Todd Fiala75930012016-08-19 04:21:48 +0000890lldb::SBStructuredData
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
892 return SBStructuredData(event.GetSP());
Todd Fiala75930012016-08-19 04:21:48 +0000893}
894
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
896 return (event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass()) &&
897 !EventIsStructuredDataEvent(event);
Todd Fiala75930012016-08-19 04:21:48 +0000898}
899
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
901 EventSP event_sp = event.GetSP();
902 EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
903 return event_data && (event_data->GetFlavor() ==
904 EventDataStructuredData::GetFlavorString());
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000905}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907SBBroadcaster SBProcess::GetBroadcaster() const {
908 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000909
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910 ProcessSP process_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +0000911
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912 SBBroadcaster broadcaster(process_sp.get(), false);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000913
Kate Stoneb9c1b512016-09-06 20:57:50 +0000914 if (log)
915 log->Printf("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",
916 static_cast<void *>(process_sp.get()),
917 static_cast<void *>(broadcaster.get()));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000918
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919 return broadcaster;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000920}
921
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922const char *SBProcess::GetBroadcasterClass() {
923 return Process::GetStaticBroadcasterClass().AsCString();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000924}
925
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
927 SBError &sb_error) {
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 size_t bytes_read = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +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 if (log)
935 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
936 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
937 static_cast<void *>(process_sp.get()), addr,
938 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
939 static_cast<void *>(sb_error.get()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000940
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 if (process_sp) {
942 Process::StopLocker stop_locker;
943 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
944 std::lock_guard<std::recursive_mutex> guard(
945 process_sp->GetTarget().GetAPIMutex());
946 bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
947 } else {
948 if (log)
949 log->Printf("SBProcess(%p)::ReadMemory() => error: process is running",
950 static_cast<void *>(process_sp.get()));
951 sb_error.SetErrorString("process is running");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 } else {
954 sb_error.SetErrorString("SBProcess is invalid");
955 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956
Kate Stoneb9c1b512016-09-06 20:57:50 +0000957 if (log) {
958 SBStream sstr;
959 sb_error.GetDescription(sstr);
960 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
961 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
962 static_cast<void *>(process_sp.get()), addr,
963 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
964 static_cast<void *>(sb_error.get()), sstr.GetData(),
965 static_cast<uint64_t>(bytes_read));
966 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000967
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 return bytes_read;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000969}
970
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
972 lldb::SBError &sb_error) {
973 size_t bytes_read = 0;
974 ProcessSP process_sp(GetSP());
975 if (process_sp) {
976 Process::StopLocker stop_locker;
977 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
978 std::lock_guard<std::recursive_mutex> guard(
979 process_sp->GetTarget().GetAPIMutex());
980 bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
981 sb_error.ref());
982 } else {
983 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
984 if (log)
985 log->Printf("SBProcess(%p)::ReadCStringFromMemory() => error: process "
986 "is running",
987 static_cast<void *>(process_sp.get()));
988 sb_error.SetErrorString("process is running");
Greg Claytone91b7952011-12-15 03:14:23 +0000989 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 } else {
991 sb_error.SetErrorString("SBProcess is invalid");
992 }
993 return bytes_read;
Greg Claytone91b7952011-12-15 03:14:23 +0000994}
995
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
997 lldb::SBError &sb_error) {
998 uint64_t value = 0;
999 ProcessSP process_sp(GetSP());
1000 if (process_sp) {
1001 Process::StopLocker stop_locker;
1002 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1003 std::lock_guard<std::recursive_mutex> guard(
1004 process_sp->GetTarget().GetAPIMutex());
1005 value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
1006 sb_error.ref());
1007 } else {
1008 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1009 if (log)
1010 log->Printf("SBProcess(%p)::ReadUnsignedFromMemory() => error: process "
1011 "is running",
1012 static_cast<void *>(process_sp.get()));
1013 sb_error.SetErrorString("process is running");
Greg Claytone91b7952011-12-15 03:14:23 +00001014 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015 } else {
1016 sb_error.SetErrorString("SBProcess is invalid");
1017 }
1018 return value;
Greg Claytone91b7952011-12-15 03:14:23 +00001019}
1020
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
1022 lldb::SBError &sb_error) {
1023 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1024 ProcessSP process_sp(GetSP());
1025 if (process_sp) {
1026 Process::StopLocker stop_locker;
1027 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1028 std::lock_guard<std::recursive_mutex> guard(
1029 process_sp->GetTarget().GetAPIMutex());
1030 ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
1031 } else {
1032 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1033 if (log)
1034 log->Printf("SBProcess(%p)::ReadPointerFromMemory() => error: process "
1035 "is running",
1036 static_cast<void *>(process_sp.get()));
1037 sb_error.SetErrorString("process is running");
Greg Claytone91b7952011-12-15 03:14:23 +00001038 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039 } else {
1040 sb_error.SetErrorString("SBProcess is invalid");
1041 }
1042 return ptr;
Greg Claytone91b7952011-12-15 03:14:23 +00001043}
1044
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
1046 SBError &sb_error) {
1047 size_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001048
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Greg Claytonacdbe812012-01-30 09:04:36 +00001050
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051 ProcessSP process_sp(GetSP());
Greg Claytonacdbe812012-01-30 09:04:36 +00001052
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053 if (log)
1054 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1055 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1056 static_cast<void *>(process_sp.get()), addr,
1057 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1058 static_cast<void *>(sb_error.get()));
Greg Clayton48381312010-10-30 04:51:46 +00001059
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 if (process_sp) {
1061 Process::StopLocker stop_locker;
1062 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1063 std::lock_guard<std::recursive_mutex> guard(
1064 process_sp->GetTarget().GetAPIMutex());
1065 bytes_written =
1066 process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
1067 } else {
1068 if (log)
1069 log->Printf("SBProcess(%p)::WriteMemory() => error: process is running",
1070 static_cast<void *>(process_sp.get()));
1071 sb_error.SetErrorString("process is running");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 if (log) {
1076 SBStream sstr;
1077 sb_error.GetDescription(sstr);
1078 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1079 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1080 static_cast<void *>(process_sp.get()), addr,
1081 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1082 static_cast<void *>(sb_error.get()), sstr.GetData(),
1083 static_cast<uint64_t>(bytes_written));
1084 }
Greg Clayton48381312010-10-30 04:51:46 +00001085
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087}
1088
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089bool SBProcess::GetDescription(SBStream &description) {
1090 Stream &strm = description.ref();
Greg Claytonda7bc7d2011-11-13 06:57:31 +00001091
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 ProcessSP process_sp(GetSP());
1093 if (process_sp) {
1094 char path[PATH_MAX];
1095 GetTarget().GetExecutable().GetPath(path, sizeof(path));
1096 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1097 const char *exe_name = NULL;
1098 if (exe_module)
1099 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
Greg Clayton1d273162010-10-06 03:09:58 +00001100
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1102 process_sp->GetID(), lldb_private::StateAsCString(GetState()),
1103 GetNumThreads(), exe_name ? ", executable = " : "",
1104 exe_name ? exe_name : "");
1105 } else
1106 strm.PutCString("No value");
Caroline Ticedde9cff2010-09-20 05:20:02 +00001107
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 return true;
Caroline Ticedde9cff2010-09-20 05:20:02 +00001109}
Greg Clayton8f343b02010-11-04 01:54:29 +00001110
1111uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1113 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Johnny Chenf9ef60d2012-05-23 22:34:34 +00001114
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 uint32_t num = 0;
1116 ProcessSP process_sp(GetSP());
1117 if (process_sp) {
1118 std::lock_guard<std::recursive_mutex> guard(
1119 process_sp->GetTarget().GetAPIMutex());
1120 sb_error.SetError(process_sp->GetWatchpointSupportInfo(num));
1121 if (log)
1122 log->Printf("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1123 static_cast<void *>(process_sp.get()), num);
1124 } else {
1125 sb_error.SetErrorString("SBProcess is invalid");
1126 }
1127 return num;
Johnny Chenf9ef60d2012-05-23 22:34:34 +00001128}
1129
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
1131 lldb::SBError &sb_error) {
1132 return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001133}
1134
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
1136 const lldb::SBFileSpec &sb_remote_image_spec,
1137 lldb::SBError &sb_error) {
1138 ProcessSP process_sp(GetSP());
1139 if (process_sp) {
1140 Process::StopLocker stop_locker;
1141 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1142 std::lock_guard<std::recursive_mutex> guard(
1143 process_sp->GetTarget().GetAPIMutex());
1144 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1145 return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
1146 *sb_remote_image_spec, sb_error.ref());
1147 } else {
1148 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1149 if (log)
1150 log->Printf("SBProcess(%p)::LoadImage() => error: process is running",
1151 static_cast<void *>(process_sp.get()));
1152 sb_error.SetErrorString("process is running");
Greg Claytonaf67cec2010-12-20 20:49:23 +00001153 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154 }
1155 return LLDB_INVALID_IMAGE_TOKEN;
Greg Clayton8f343b02010-11-04 01:54:29 +00001156}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001157
Kate Stoneb9c1b512016-09-06 20:57:50 +00001158lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
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 sb_error.SetError(
1168 platform_sp->UnloadImage(process_sp.get(), image_token));
1169 } else {
1170 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1171 if (log)
1172 log->Printf("SBProcess(%p)::UnloadImage() => 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 } else
1177 sb_error.SetErrorString("invalid process");
1178 return sb_error;
Greg Clayton8f343b02010-11-04 01:54:29 +00001179}
Jason Molenda8c713372013-11-05 11:00:35 +00001180
Kate Stoneb9c1b512016-09-06 20:57:50 +00001181lldb::SBError SBProcess::SendEventData(const char *event_data) {
1182 lldb::SBError sb_error;
1183 ProcessSP process_sp(GetSP());
1184 if (process_sp) {
1185 Process::StopLocker stop_locker;
1186 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1187 std::lock_guard<std::recursive_mutex> guard(
1188 process_sp->GetTarget().GetAPIMutex());
1189 sb_error.SetError(process_sp->SendEventData(event_data));
1190 } else {
1191 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1192 if (log)
1193 log->Printf(
1194 "SBProcess(%p)::SendEventData() => error: process is running",
1195 static_cast<void *>(process_sp.get()));
1196 sb_error.SetErrorString("process is running");
Jason Molendaa3329782014-03-29 18:54:20 +00001197 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001198 } else
1199 sb_error.SetErrorString("invalid process");
1200 return sb_error;
Jason Molendaa3329782014-03-29 18:54:20 +00001201}
1202
Kate Stoneb9c1b512016-09-06 20:57:50 +00001203uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1204 ProcessSP process_sp(GetSP());
1205 if (process_sp && process_sp->GetSystemRuntime()) {
1206 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1207 return runtime->GetExtendedBacktraceTypes().size();
1208 }
1209 return 0;
Jason Molenda8c713372013-11-05 11:00:35 +00001210}
1211
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1213 ProcessSP process_sp(GetSP());
1214 if (process_sp && process_sp->GetSystemRuntime()) {
1215 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1216 const std::vector<ConstString> &names =
1217 runtime->GetExtendedBacktraceTypes();
1218 if (idx < names.size()) {
1219 return names[idx].AsCString();
1220 } else {
1221 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1222 if (log)
1223 log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => "
1224 "error: requested extended backtrace name out of bounds",
1225 static_cast<void *>(process_sp.get()));
Jason Molenda8c713372013-11-05 11:00:35 +00001226 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001227 }
1228 return NULL;
Jason Molenda8c713372013-11-05 11:00:35 +00001229}
Kuba Breckaa51ea382014-09-06 01:33:13 +00001230
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1232 ProcessSP process_sp(GetSP());
1233 SBThreadCollection threads;
1234 if (process_sp) {
1235 threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1236 }
1237 return threads;
Kuba Breckaa51ea382014-09-06 01:33:13 +00001238}
Kuba Brecka63927542014-10-11 01:59:32 +00001239
Kate Stoneb9c1b512016-09-06 20:57:50 +00001240bool SBProcess::IsInstrumentationRuntimePresent(
1241 InstrumentationRuntimeType type) {
1242 ProcessSP process_sp(GetSP());
1243 if (!process_sp)
1244 return false;
1245
1246 InstrumentationRuntimeSP runtime_sp =
1247 process_sp->GetInstrumentationRuntime(type);
1248
1249 if (!runtime_sp.get())
1250 return false;
1251
1252 return runtime_sp->IsActive();
Kuba Brecka63927542014-10-11 01:59:32 +00001253}
Adrian McCarthyf7d18932015-11-20 23:09:11 +00001254
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255lldb::SBError SBProcess::SaveCore(const char *file_name) {
1256 lldb::SBError error;
1257 ProcessSP process_sp(GetSP());
1258 if (!process_sp) {
1259 error.SetErrorString("SBProcess is invalid");
Adrian McCarthyf7d18932015-11-20 23:09:11 +00001260 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261 }
1262
1263 std::lock_guard<std::recursive_mutex> guard(
1264 process_sp->GetTarget().GetAPIMutex());
1265
1266 if (process_sp->GetState() != eStateStopped) {
1267 error.SetErrorString("the process is not stopped");
1268 return error;
1269 }
1270
1271 FileSpec core_file(file_name, false);
1272 error.ref() = PluginManager::SaveCore(process_sp, core_file);
1273 return error;
Adrian McCarthyf7d18932015-11-20 23:09:11 +00001274}
Howard Hellyer260368432016-06-23 08:35:37 +00001275
1276lldb::SBError
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1278 SBMemoryRegionInfo &sb_region_info) {
1279 lldb::SBError sb_error;
1280 ProcessSP process_sp(GetSP());
1281 MemoryRegionInfoSP region_info_sp =
1282 std::make_shared<lldb_private::MemoryRegionInfo>();
1283 if (process_sp) {
1284 Process::StopLocker stop_locker;
1285 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1286 std::lock_guard<std::recursive_mutex> guard(
1287 process_sp->GetTarget().GetAPIMutex());
1288 sb_error.ref() =
1289 process_sp->GetMemoryRegionInfo(load_addr, *region_info_sp);
1290 if (sb_error.Success()) {
1291 sb_region_info.ref() = *region_info_sp;
1292 }
1293 } else {
1294 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1295 if (log)
1296 log->Printf(
1297 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1298 static_cast<void *>(process_sp.get()));
1299 sb_error.SetErrorString("process is running");
Howard Hellyer260368432016-06-23 08:35:37 +00001300 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301 } else {
1302 sb_error.SetErrorString("SBProcess is invalid");
1303 }
1304 return sb_error;
Howard Hellyer260368432016-06-23 08:35:37 +00001305}
1306
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1308 lldb::SBError sb_error;
1309 lldb::SBMemoryRegionInfoList sb_region_list;
1310 ProcessSP process_sp(GetSP());
1311 if (process_sp) {
1312 Process::StopLocker stop_locker;
1313 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1314 std::lock_guard<std::recursive_mutex> guard(
1315 process_sp->GetTarget().GetAPIMutex());
1316 std::vector<MemoryRegionInfoSP> region_list;
1317 sb_error.ref() = process_sp->GetMemoryRegions(region_list);
1318 if (sb_error.Success()) {
1319 std::vector<MemoryRegionInfoSP>::iterator end = region_list.end();
1320 for (std::vector<MemoryRegionInfoSP>::iterator it = region_list.begin();
1321 it != end; it++) {
1322 SBMemoryRegionInfo sb_region_info(it->get());
1323 sb_region_list.Append(sb_region_info);
Howard Hellyer260368432016-06-23 08:35:37 +00001324 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325 }
1326 } else {
1327 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1328 if (log)
1329 log->Printf(
1330 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1331 static_cast<void *>(process_sp.get()));
1332 sb_error.SetErrorString("process is running");
Howard Hellyer260368432016-06-23 08:35:37 +00001333 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001334 } else {
1335 sb_error.SetErrorString("SBProcess is invalid");
1336 }
1337 return sb_region_list;
Howard Hellyer260368432016-06-23 08:35:37 +00001338}