blob: 6e9c0fb311bf4ed2f0663ffc8ce4f7f032d60298 [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
11// C++ Includes
12// Other libraries and framework includes
13#include "lldb/Core/PluginManager.h"
14#include "lldb/Host/Host.h"
15#include "lldb/Symbol/ObjectFile.h"
16#include "lldb/Target/Target.h"
17
18#include "ProcessLinux.h"
19#include "ProcessMonitor.h"
20#include "LinuxThread.h"
21
22using namespace lldb;
23using namespace lldb_private;
24
25//------------------------------------------------------------------------------
26// Static functions.
27
28Process*
29ProcessLinux::CreateInstance(Target& target, Listener &listener)
30{
31 return new ProcessLinux(target, listener);
32}
33
34void
35ProcessLinux::Initialize()
36{
37 static bool g_initialized = false;
38
39 if (!g_initialized)
40 {
41 PluginManager::RegisterPlugin(GetPluginNameStatic(),
42 GetPluginDescriptionStatic(),
43 CreateInstance);
44 g_initialized = true;
45 }
46}
47
48void
49ProcessLinux::Terminate()
50{
51}
52
53const char *
54ProcessLinux::GetPluginNameStatic()
55{
56 return "plugin.process.linux";
57}
58
59const char *
60ProcessLinux::GetPluginDescriptionStatic()
61{
62 return "Process plugin for Linux";
63}
64
65
66//------------------------------------------------------------------------------
67// Constructors and destructors.
68
69ProcessLinux::ProcessLinux(Target& target, Listener &listener)
70 : Process(target, listener),
71 m_monitor(NULL),
72 m_module(NULL)
73{
74 // FIXME: Putting this code in the ctor and saving the byte order in a
75 // member variable is a hack to avoid const qual issues in GetByteOrder.
76 ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile();
77 m_byte_order = obj_file->GetByteOrder();
78}
79
80ProcessLinux::~ProcessLinux()
81{
82 delete m_monitor;
83}
84
85//------------------------------------------------------------------------------
86// Process protocol.
87
88bool
89ProcessLinux::CanDebug(Target &target)
90{
91 // For now we are just making sure the file exists for a given module
92 ModuleSP exe_module_sp(target.GetExecutableModule());
93 if (exe_module_sp.get())
94 return exe_module_sp->GetFileSpec().Exists();
95 return false;
96}
97
98Error
99ProcessLinux::DoAttachToProcessWithID(lldb::pid_t pid)
100{
101 return Error(1, eErrorTypeGeneric);
102}
103
104Error
105ProcessLinux::DoLaunch(Module *module,
106 char const *argv[],
107 char const *envp[],
Stephen Wilson3a804312011-01-04 21:41:31 +0000108 uint32_t launch_flags,
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000109 const char *stdin_path,
110 const char *stdout_path,
111 const char *stderr_path)
112{
113 Error error;
114 assert(m_monitor == NULL);
115
116 SetPrivateState(eStateLaunching);
117 m_monitor = new ProcessMonitor(this, module,
118 argv, envp,
119 stdin_path, stdout_path, stderr_path,
120 error);
121
122 m_module = module;
123
124 if (!error.Success())
125 return error;
126
127 return error;
128}
129
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000130Error
131ProcessLinux::DoResume()
132{
133 assert(GetPrivateState() == eStateStopped && "Bad state for DoResume!");
134
135 // Set our state to running. This ensures inferior threads do not post a
136 // state change first.
137 SetPrivateState(eStateRunning);
138
139 bool did_resume = false;
140 uint32_t thread_count = m_thread_list.GetSize(false);
141 for (uint32_t i = 0; i < thread_count; ++i)
142 {
143 LinuxThread *thread = static_cast<LinuxThread*>(
144 m_thread_list.GetThreadAtIndex(i, false).get());
145 did_resume = thread->Resume() || did_resume;
146 }
147 assert(did_resume && "Process resume failed!");
148
149 return Error();
150}
151
Stephen Wilson01316422011-01-15 00:10:37 +0000152addr_t
153ProcessLinux::GetImageInfoAddress()
154{
155 Target *target = &GetTarget();
156 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
157 Address addr = obj_file->GetImageInfoAddress();
158
159 if (addr.IsValid())
160 return addr.GetLoadAddress(target);
161 else
162 return LLDB_INVALID_ADDRESS;
163}
164
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000165Error
Stephen Wilson3a804312011-01-04 21:41:31 +0000166ProcessLinux::DoHalt(bool &caused_stop)
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000167{
168 return Error(1, eErrorTypeGeneric);
169}
170
171Error
172ProcessLinux::DoDetach()
173{
174 return Error(1, eErrorTypeGeneric);
175}
176
177Error
178ProcessLinux::DoSignal(int signal)
179{
180 return Error(1, eErrorTypeGeneric);
181}
182
183Error
184ProcessLinux::DoDestroy()
185{
186 Error error;
187
188 if (!HasExited())
189 {
Greg Clayton5d187e52011-01-08 20:28:42 +0000190 // Shut down the private state thread as we will synchronize with events
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000191 // ourselves. Discard all current thread plans.
192 PausePrivateStateThread();
193 GetThreadList().DiscardThreadPlans();
194
195 // Bringing the inferior into limbo will be caught by our monitor
196 // thread, in turn updating the process state.
197 if (!m_monitor->BringProcessIntoLimbo())
198 {
199 error.SetErrorToGenericError();
200 error.SetErrorString("Process termination failed.");
201 return error;
202 }
203
204 // Wait for the event to arrive. This guaranteed to be an exit event.
205 StateType state;
206 EventSP event;
207 do {
208 state = WaitForStateChangedEventsPrivate(NULL, event);
209 } while (state != eStateExited);
210
211 // Restart standard event handling and send the process the final kill,
212 // driving it out of limbo.
213 ResumePrivateStateThread();
214 }
215
216 if (kill(m_monitor->GetPID(), SIGKILL))
217 error.SetErrorToErrno();
218 return error;
219}
220
221void
222ProcessLinux::SendMessage(const ProcessMessage &message)
223{
224 Mutex::Locker lock(m_message_mutex);
225 m_message_queue.push(message);
226
227 switch (message.GetKind())
228 {
229 default:
230 SetPrivateState(eStateStopped);
231 break;
232
233 case ProcessMessage::eExitMessage:
234 SetExitStatus(message.GetExitStatus(), NULL);
235 break;
236
237 case ProcessMessage::eSignalMessage:
238 SetExitStatus(-1, NULL);
239 break;
240 }
241}
242
243void
244ProcessLinux::RefreshStateAfterStop()
245{
246 Mutex::Locker lock(m_message_mutex);
247 if (m_message_queue.empty())
248 return;
249
250 ProcessMessage &message = m_message_queue.front();
251
252 // Resolve the thread this message corresponds to.
253 lldb::tid_t tid = message.GetTID();
254 LinuxThread *thread = static_cast<LinuxThread*>(
255 GetThreadList().FindThreadByID(tid, false).get());
256
257 switch (message.GetKind())
258 {
259 default:
260 assert(false && "Unexpected message kind!");
261 break;
262
263 case ProcessMessage::eExitMessage:
264 case ProcessMessage::eSignalMessage:
265 thread->ExitNotify();
266 break;
267
268 case ProcessMessage::eTraceMessage:
269 thread->TraceNotify();
270 break;
271
272 case ProcessMessage::eBreakpointMessage:
273 thread->BreakNotify();
274 break;
275 }
276
277 m_message_queue.pop();
278}
279
280bool
281ProcessLinux::IsAlive()
282{
283 StateType state = GetPrivateState();
284 return state != eStateExited && state != eStateInvalid;
285}
286
287size_t
288ProcessLinux::DoReadMemory(addr_t vm_addr,
289 void *buf, size_t size, Error &error)
290{
291 return m_monitor->ReadMemory(vm_addr, buf, size, error);
292}
293
294size_t
295ProcessLinux::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size,
296 Error &error)
297{
298 return m_monitor->WriteMemory(vm_addr, buf, size, error);
299}
300
301addr_t
302ProcessLinux::DoAllocateMemory(size_t size, uint32_t permissions,
303 Error &error)
304{
305 return 0;
306}
307
308addr_t
309ProcessLinux::AllocateMemory(size_t size, uint32_t permissions, Error &error)
310{
311 return 0;
312}
313
314Error
315ProcessLinux::DoDeallocateMemory(lldb::addr_t ptr)
316{
317 return Error(1, eErrorTypeGeneric);
318}
319
320size_t
321ProcessLinux::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site)
322{
323 static const uint8_t g_i386_opcode[] = { 0xCC };
324
325 ArchSpec arch = GetTarget().GetArchitecture();
326 const uint8_t *opcode = NULL;
327 size_t opcode_size = 0;
328
329 switch (arch.GetGenericCPUType())
330 {
331 default:
332 assert(false && "CPU type not supported!");
333 break;
334
335 case ArchSpec::eCPU_i386:
336 case ArchSpec::eCPU_x86_64:
337 opcode = g_i386_opcode;
338 opcode_size = sizeof(g_i386_opcode);
339 break;
340 }
341
342 bp_site->SetTrapOpcode(opcode, opcode_size);
343 return opcode_size;
344}
345
346Error
347ProcessLinux::EnableBreakpoint(BreakpointSite *bp_site)
348{
349 return EnableSoftwareBreakpoint(bp_site);
350}
351
352Error
353ProcessLinux::DisableBreakpoint(BreakpointSite *bp_site)
354{
355 return DisableSoftwareBreakpoint(bp_site);
356}
357
358uint32_t
359ProcessLinux::UpdateThreadListIfNeeded()
360{
361 // Do not allow recursive updates.
362 return m_thread_list.GetSize(false);
363}
364
365ByteOrder
366ProcessLinux::GetByteOrder() const
367{
368 // FIXME: We should be able to extract this value directly. See comment in
369 // ProcessLinux().
370 return m_byte_order;
371}
372
373//------------------------------------------------------------------------------
374// ProcessInterface protocol.
375
376const char *
377ProcessLinux::GetPluginName()
378{
379 return "process.linux";
380}
381
382const char *
383ProcessLinux::GetShortPluginName()
384{
385 return "process.linux";
386}
387
388uint32_t
389ProcessLinux::GetPluginVersion()
390{
391 return 1;
392}
393
394void
395ProcessLinux::GetPluginCommandHelp(const char *command, Stream *strm)
396{
397}
398
399Error
400ProcessLinux::ExecutePluginCommand(Args &command, Stream *strm)
401{
402 return Error(1, eErrorTypeGeneric);
403}
404
405Log *
406ProcessLinux::EnablePluginLogging(Stream *strm, Args &command)
407{
408 return NULL;
409}
410
411//------------------------------------------------------------------------------
412// Utility functions.
413
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000414bool
415ProcessLinux::HasExited()
416{
417 switch (GetPrivateState())
418 {
419 default:
420 break;
421
422 case eStateUnloaded:
423 case eStateCrashed:
424 case eStateDetached:
425 case eStateExited:
426 return true;
427 }
428
429 return false;
430}