blob: b3405dda94461554a6477750714e89e0b420e7eb [file] [log] [blame]
Stephen Wilsonf6f40332010-07-24 02:19:04 +00001//===-- ProcessLinux.cpp ----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
Stephen Wilsond1fbbb42011-03-23 02:14:42 +000011#include <errno.h>
12
Stephen Wilsonf6f40332010-07-24 02:19:04 +000013// C++ Includes
14// Other libraries and framework includes
15#include "lldb/Core/PluginManager.h"
16#include "lldb/Host/Host.h"
17#include "lldb/Symbol/ObjectFile.h"
Stephen Wilson92241ef2011-01-16 19:45:39 +000018#include "lldb/Target/DynamicLoader.h"
Stephen Wilsonf6f40332010-07-24 02:19:04 +000019#include "lldb/Target/Target.h"
20
21#include "ProcessLinux.h"
22#include "ProcessMonitor.h"
23#include "LinuxThread.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
28//------------------------------------------------------------------------------
29// Static functions.
30
31Process*
32ProcessLinux::CreateInstance(Target& target, Listener &listener)
33{
34 return new ProcessLinux(target, listener);
35}
36
37void
38ProcessLinux::Initialize()
39{
40 static bool g_initialized = false;
41
42 if (!g_initialized)
43 {
44 PluginManager::RegisterPlugin(GetPluginNameStatic(),
45 GetPluginDescriptionStatic(),
46 CreateInstance);
47 g_initialized = true;
48 }
49}
50
51void
52ProcessLinux::Terminate()
53{
54}
55
56const char *
57ProcessLinux::GetPluginNameStatic()
58{
59 return "plugin.process.linux";
60}
61
62const char *
63ProcessLinux::GetPluginDescriptionStatic()
64{
65 return "Process plugin for Linux";
66}
67
68
69//------------------------------------------------------------------------------
70// Constructors and destructors.
71
72ProcessLinux::ProcessLinux(Target& target, Listener &listener)
73 : Process(target, listener),
74 m_monitor(NULL),
75 m_module(NULL)
76{
77 // FIXME: Putting this code in the ctor and saving the byte order in a
78 // member variable is a hack to avoid const qual issues in GetByteOrder.
79 ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile();
80 m_byte_order = obj_file->GetByteOrder();
81}
82
83ProcessLinux::~ProcessLinux()
84{
85 delete m_monitor;
86}
87
88//------------------------------------------------------------------------------
89// Process protocol.
90
91bool
92ProcessLinux::CanDebug(Target &target)
93{
94 // For now we are just making sure the file exists for a given module
95 ModuleSP exe_module_sp(target.GetExecutableModule());
96 if (exe_module_sp.get())
97 return exe_module_sp->GetFileSpec().Exists();
98 return false;
99}
100
101Error
102ProcessLinux::DoAttachToProcessWithID(lldb::pid_t pid)
103{
104 return Error(1, eErrorTypeGeneric);
105}
106
107Error
Stephen Wilson92241ef2011-01-16 19:45:39 +0000108ProcessLinux::WillLaunch(Module* module)
109{
110 Error error;
Stephen Wilson92241ef2011-01-16 19:45:39 +0000111 return error;
112}
113
114Error
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000115ProcessLinux::DoLaunch(Module *module,
116 char const *argv[],
117 char const *envp[],
Stephen Wilson3a804312011-01-04 21:41:31 +0000118 uint32_t launch_flags,
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000119 const char *stdin_path,
120 const char *stdout_path,
Greg Claytonde915be2011-01-23 05:56:20 +0000121 const char *stderr_path,
122 const char *working_directory)
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000123{
124 Error error;
125 assert(m_monitor == NULL);
126
127 SetPrivateState(eStateLaunching);
128 m_monitor = new ProcessMonitor(this, module,
129 argv, envp,
130 stdin_path, stdout_path, stderr_path,
131 error);
132
133 m_module = module;
134
135 if (!error.Success())
136 return error;
137
Stephen Wilson1f004c62011-01-15 00:13:27 +0000138 SetID(m_monitor->GetPID());
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000139 return error;
140}
141
Stephen Wilson92241ef2011-01-16 19:45:39 +0000142void
143ProcessLinux::DidLaunch()
144{
Stephen Wilson92241ef2011-01-16 19:45:39 +0000145}
146
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000147Error
148ProcessLinux::DoResume()
149{
150 assert(GetPrivateState() == eStateStopped && "Bad state for DoResume!");
151
152 // Set our state to running. This ensures inferior threads do not post a
153 // state change first.
154 SetPrivateState(eStateRunning);
155
156 bool did_resume = false;
157 uint32_t thread_count = m_thread_list.GetSize(false);
158 for (uint32_t i = 0; i < thread_count; ++i)
159 {
160 LinuxThread *thread = static_cast<LinuxThread*>(
161 m_thread_list.GetThreadAtIndex(i, false).get());
162 did_resume = thread->Resume() || did_resume;
163 }
164 assert(did_resume && "Process resume failed!");
165
166 return Error();
167}
168
Stephen Wilson01316422011-01-15 00:10:37 +0000169addr_t
170ProcessLinux::GetImageInfoAddress()
171{
172 Target *target = &GetTarget();
173 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
174 Address addr = obj_file->GetImageInfoAddress();
175
176 if (addr.IsValid())
177 return addr.GetLoadAddress(target);
178 else
179 return LLDB_INVALID_ADDRESS;
180}
181
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000182Error
Stephen Wilson3a804312011-01-04 21:41:31 +0000183ProcessLinux::DoHalt(bool &caused_stop)
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000184{
185 return Error(1, eErrorTypeGeneric);
186}
187
188Error
189ProcessLinux::DoDetach()
190{
191 return Error(1, eErrorTypeGeneric);
192}
193
194Error
195ProcessLinux::DoSignal(int signal)
196{
197 return Error(1, eErrorTypeGeneric);
198}
199
200Error
201ProcessLinux::DoDestroy()
202{
203 Error error;
204
205 if (!HasExited())
206 {
Greg Clayton5d187e52011-01-08 20:28:42 +0000207 // Shut down the private state thread as we will synchronize with events
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000208 // ourselves. Discard all current thread plans.
209 PausePrivateStateThread();
210 GetThreadList().DiscardThreadPlans();
211
212 // Bringing the inferior into limbo will be caught by our monitor
213 // thread, in turn updating the process state.
214 if (!m_monitor->BringProcessIntoLimbo())
215 {
216 error.SetErrorToGenericError();
217 error.SetErrorString("Process termination failed.");
218 return error;
219 }
220
Stephen Wilson83047fc2011-01-19 01:30:44 +0000221 // Wait for the event to arrive. This is guaranteed to be an exit event.
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000222 StateType state;
223 EventSP event;
224 do {
Stephen Wilson83047fc2011-01-19 01:30:44 +0000225 TimeValue timeout_time;
226 timeout_time = TimeValue::Now();
227 timeout_time.OffsetWithSeconds(2);
228 state = WaitForStateChangedEventsPrivate(&timeout_time, event);
229 } while (state != eStateExited && state != eStateInvalid);
230
231 // Check if we timed out waiting for the exit event to arrive.
232 if (state == eStateInvalid)
233 error.SetErrorString("ProcessLinux::DoDestroy timed out.");
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000234
235 // Restart standard event handling and send the process the final kill,
236 // driving it out of limbo.
237 ResumePrivateStateThread();
238 }
239
Stephen Wilson83047fc2011-01-19 01:30:44 +0000240 if (kill(m_monitor->GetPID(), SIGKILL) && error.Success())
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000241 error.SetErrorToErrno();
Stephen Wilson83047fc2011-01-19 01:30:44 +0000242
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000243 return error;
244}
245
246void
247ProcessLinux::SendMessage(const ProcessMessage &message)
248{
249 Mutex::Locker lock(m_message_mutex);
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000250
251 switch (message.GetKind())
252 {
253 default:
254 SetPrivateState(eStateStopped);
255 break;
256
Stephen Wilson07fc7a92011-01-19 01:29:39 +0000257 case ProcessMessage::eInvalidMessage:
258 return;
259
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000260 case ProcessMessage::eExitMessage:
261 SetExitStatus(message.GetExitStatus(), NULL);
262 break;
263
264 case ProcessMessage::eSignalMessage:
265 SetExitStatus(-1, NULL);
266 break;
267 }
Stephen Wilson07fc7a92011-01-19 01:29:39 +0000268
269 m_message_queue.push(message);
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000270}
271
272void
273ProcessLinux::RefreshStateAfterStop()
274{
275 Mutex::Locker lock(m_message_mutex);
276 if (m_message_queue.empty())
277 return;
278
279 ProcessMessage &message = m_message_queue.front();
280
281 // Resolve the thread this message corresponds to.
282 lldb::tid_t tid = message.GetTID();
283 LinuxThread *thread = static_cast<LinuxThread*>(
284 GetThreadList().FindThreadByID(tid, false).get());
285
286 switch (message.GetKind())
287 {
288 default:
289 assert(false && "Unexpected message kind!");
290 break;
291
292 case ProcessMessage::eExitMessage:
293 case ProcessMessage::eSignalMessage:
294 thread->ExitNotify();
295 break;
296
297 case ProcessMessage::eTraceMessage:
298 thread->TraceNotify();
299 break;
300
301 case ProcessMessage::eBreakpointMessage:
302 thread->BreakNotify();
303 break;
304 }
305
306 m_message_queue.pop();
307}
308
309bool
310ProcessLinux::IsAlive()
311{
312 StateType state = GetPrivateState();
313 return state != eStateExited && state != eStateInvalid;
314}
315
316size_t
317ProcessLinux::DoReadMemory(addr_t vm_addr,
318 void *buf, size_t size, Error &error)
319{
320 return m_monitor->ReadMemory(vm_addr, buf, size, error);
321}
322
323size_t
324ProcessLinux::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size,
325 Error &error)
326{
327 return m_monitor->WriteMemory(vm_addr, buf, size, error);
328}
329
330addr_t
331ProcessLinux::DoAllocateMemory(size_t size, uint32_t permissions,
332 Error &error)
333{
334 return 0;
335}
336
337addr_t
338ProcessLinux::AllocateMemory(size_t size, uint32_t permissions, Error &error)
339{
340 return 0;
341}
342
343Error
344ProcessLinux::DoDeallocateMemory(lldb::addr_t ptr)
345{
346 return Error(1, eErrorTypeGeneric);
347}
348
349size_t
350ProcessLinux::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site)
351{
352 static const uint8_t g_i386_opcode[] = { 0xCC };
353
354 ArchSpec arch = GetTarget().GetArchitecture();
355 const uint8_t *opcode = NULL;
356 size_t opcode_size = 0;
357
Stephen Wilsona1f0b722011-02-24 19:17:09 +0000358 switch (arch.GetCore())
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000359 {
360 default:
361 assert(false && "CPU type not supported!");
362 break;
363
Stephen Wilsona1f0b722011-02-24 19:17:09 +0000364 case ArchSpec::eCore_x86_32_i386:
365 case ArchSpec::eCore_x86_64_x86_64:
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000366 opcode = g_i386_opcode;
367 opcode_size = sizeof(g_i386_opcode);
368 break;
369 }
370
371 bp_site->SetTrapOpcode(opcode, opcode_size);
372 return opcode_size;
373}
374
375Error
376ProcessLinux::EnableBreakpoint(BreakpointSite *bp_site)
377{
378 return EnableSoftwareBreakpoint(bp_site);
379}
380
381Error
382ProcessLinux::DisableBreakpoint(BreakpointSite *bp_site)
383{
384 return DisableSoftwareBreakpoint(bp_site);
385}
386
387uint32_t
388ProcessLinux::UpdateThreadListIfNeeded()
389{
390 // Do not allow recursive updates.
391 return m_thread_list.GetSize(false);
392}
393
394ByteOrder
395ProcessLinux::GetByteOrder() const
396{
397 // FIXME: We should be able to extract this value directly. See comment in
398 // ProcessLinux().
399 return m_byte_order;
400}
401
Stephen Wilsond1fbbb42011-03-23 02:14:42 +0000402size_t
403ProcessLinux::PutSTDIN(const char *buf, size_t len, Error &error)
404{
405 ssize_t status;
406 if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0)
407 {
408 error.SetErrorToErrno();
409 return 0;
410 }
411 return status;
412}
413
414size_t
415ProcessLinux::GetSTDOUT(char *buf, size_t len, Error &error)
416{
417 ssize_t bytes_read;
418
419 // The terminal file descriptor is always in non-block mode.
420 if ((bytes_read = read(m_monitor->GetTerminalFD(), buf, len)) < 0)
421 {
422 if (errno != EAGAIN)
423 error.SetErrorToErrno();
424 return 0;
425 }
426 return bytes_read;
427}
428
429size_t
430ProcessLinux::GetSTDERR(char *buf, size_t len, Error &error)
431{
432 return GetSTDOUT(buf, len, error);
433}
434
435
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000436//------------------------------------------------------------------------------
437// ProcessInterface protocol.
438
439const char *
440ProcessLinux::GetPluginName()
441{
442 return "process.linux";
443}
444
445const char *
446ProcessLinux::GetShortPluginName()
447{
448 return "process.linux";
449}
450
451uint32_t
452ProcessLinux::GetPluginVersion()
453{
454 return 1;
455}
456
457void
458ProcessLinux::GetPluginCommandHelp(const char *command, Stream *strm)
459{
460}
461
462Error
463ProcessLinux::ExecutePluginCommand(Args &command, Stream *strm)
464{
465 return Error(1, eErrorTypeGeneric);
466}
467
468Log *
469ProcessLinux::EnablePluginLogging(Stream *strm, Args &command)
470{
471 return NULL;
472}
473
474//------------------------------------------------------------------------------
475// Utility functions.
476
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000477bool
478ProcessLinux::HasExited()
479{
480 switch (GetPrivateState())
481 {
482 default:
483 break;
484
485 case eStateUnloaded:
486 case eStateCrashed:
487 case eStateDetached:
488 case eStateExited:
489 return true;
490 }
491
492 return false;
493}