blob: 6d7b5cf0e1459fd179991596b33d67a9eea73fbc [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
130void
131ProcessLinux::DidLaunch()
132{
133 UpdateLoadedSections();
134}
135
136Error
137ProcessLinux::DoResume()
138{
139 assert(GetPrivateState() == eStateStopped && "Bad state for DoResume!");
140
141 // Set our state to running. This ensures inferior threads do not post a
142 // state change first.
143 SetPrivateState(eStateRunning);
144
145 bool did_resume = false;
146 uint32_t thread_count = m_thread_list.GetSize(false);
147 for (uint32_t i = 0; i < thread_count; ++i)
148 {
149 LinuxThread *thread = static_cast<LinuxThread*>(
150 m_thread_list.GetThreadAtIndex(i, false).get());
151 did_resume = thread->Resume() || did_resume;
152 }
153 assert(did_resume && "Process resume failed!");
154
155 return Error();
156}
157
158Error
Stephen Wilson3a804312011-01-04 21:41:31 +0000159ProcessLinux::DoHalt(bool &caused_stop)
Stephen Wilsonf6f40332010-07-24 02:19:04 +0000160{
161 return Error(1, eErrorTypeGeneric);
162}
163
164Error
165ProcessLinux::DoDetach()
166{
167 return Error(1, eErrorTypeGeneric);
168}
169
170Error
171ProcessLinux::DoSignal(int signal)
172{
173 return Error(1, eErrorTypeGeneric);
174}
175
176Error
177ProcessLinux::DoDestroy()
178{
179 Error error;
180
181 if (!HasExited())
182 {
183 // Shut down the private state thread as we will syncronize with events
184 // ourselves. Discard all current thread plans.
185 PausePrivateStateThread();
186 GetThreadList().DiscardThreadPlans();
187
188 // Bringing the inferior into limbo will be caught by our monitor
189 // thread, in turn updating the process state.
190 if (!m_monitor->BringProcessIntoLimbo())
191 {
192 error.SetErrorToGenericError();
193 error.SetErrorString("Process termination failed.");
194 return error;
195 }
196
197 // Wait for the event to arrive. This guaranteed to be an exit event.
198 StateType state;
199 EventSP event;
200 do {
201 state = WaitForStateChangedEventsPrivate(NULL, event);
202 } while (state != eStateExited);
203
204 // Restart standard event handling and send the process the final kill,
205 // driving it out of limbo.
206 ResumePrivateStateThread();
207 }
208
209 if (kill(m_monitor->GetPID(), SIGKILL))
210 error.SetErrorToErrno();
211 return error;
212}
213
214void
215ProcessLinux::SendMessage(const ProcessMessage &message)
216{
217 Mutex::Locker lock(m_message_mutex);
218 m_message_queue.push(message);
219
220 switch (message.GetKind())
221 {
222 default:
223 SetPrivateState(eStateStopped);
224 break;
225
226 case ProcessMessage::eExitMessage:
227 SetExitStatus(message.GetExitStatus(), NULL);
228 break;
229
230 case ProcessMessage::eSignalMessage:
231 SetExitStatus(-1, NULL);
232 break;
233 }
234}
235
236void
237ProcessLinux::RefreshStateAfterStop()
238{
239 Mutex::Locker lock(m_message_mutex);
240 if (m_message_queue.empty())
241 return;
242
243 ProcessMessage &message = m_message_queue.front();
244
245 // Resolve the thread this message corresponds to.
246 lldb::tid_t tid = message.GetTID();
247 LinuxThread *thread = static_cast<LinuxThread*>(
248 GetThreadList().FindThreadByID(tid, false).get());
249
250 switch (message.GetKind())
251 {
252 default:
253 assert(false && "Unexpected message kind!");
254 break;
255
256 case ProcessMessage::eExitMessage:
257 case ProcessMessage::eSignalMessage:
258 thread->ExitNotify();
259 break;
260
261 case ProcessMessage::eTraceMessage:
262 thread->TraceNotify();
263 break;
264
265 case ProcessMessage::eBreakpointMessage:
266 thread->BreakNotify();
267 break;
268 }
269
270 m_message_queue.pop();
271}
272
273bool
274ProcessLinux::IsAlive()
275{
276 StateType state = GetPrivateState();
277 return state != eStateExited && state != eStateInvalid;
278}
279
280size_t
281ProcessLinux::DoReadMemory(addr_t vm_addr,
282 void *buf, size_t size, Error &error)
283{
284 return m_monitor->ReadMemory(vm_addr, buf, size, error);
285}
286
287size_t
288ProcessLinux::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size,
289 Error &error)
290{
291 return m_monitor->WriteMemory(vm_addr, buf, size, error);
292}
293
294addr_t
295ProcessLinux::DoAllocateMemory(size_t size, uint32_t permissions,
296 Error &error)
297{
298 return 0;
299}
300
301addr_t
302ProcessLinux::AllocateMemory(size_t size, uint32_t permissions, Error &error)
303{
304 return 0;
305}
306
307Error
308ProcessLinux::DoDeallocateMemory(lldb::addr_t ptr)
309{
310 return Error(1, eErrorTypeGeneric);
311}
312
313size_t
314ProcessLinux::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site)
315{
316 static const uint8_t g_i386_opcode[] = { 0xCC };
317
318 ArchSpec arch = GetTarget().GetArchitecture();
319 const uint8_t *opcode = NULL;
320 size_t opcode_size = 0;
321
322 switch (arch.GetGenericCPUType())
323 {
324 default:
325 assert(false && "CPU type not supported!");
326 break;
327
328 case ArchSpec::eCPU_i386:
329 case ArchSpec::eCPU_x86_64:
330 opcode = g_i386_opcode;
331 opcode_size = sizeof(g_i386_opcode);
332 break;
333 }
334
335 bp_site->SetTrapOpcode(opcode, opcode_size);
336 return opcode_size;
337}
338
339Error
340ProcessLinux::EnableBreakpoint(BreakpointSite *bp_site)
341{
342 return EnableSoftwareBreakpoint(bp_site);
343}
344
345Error
346ProcessLinux::DisableBreakpoint(BreakpointSite *bp_site)
347{
348 return DisableSoftwareBreakpoint(bp_site);
349}
350
351uint32_t
352ProcessLinux::UpdateThreadListIfNeeded()
353{
354 // Do not allow recursive updates.
355 return m_thread_list.GetSize(false);
356}
357
358ByteOrder
359ProcessLinux::GetByteOrder() const
360{
361 // FIXME: We should be able to extract this value directly. See comment in
362 // ProcessLinux().
363 return m_byte_order;
364}
365
366//------------------------------------------------------------------------------
367// ProcessInterface protocol.
368
369const char *
370ProcessLinux::GetPluginName()
371{
372 return "process.linux";
373}
374
375const char *
376ProcessLinux::GetShortPluginName()
377{
378 return "process.linux";
379}
380
381uint32_t
382ProcessLinux::GetPluginVersion()
383{
384 return 1;
385}
386
387void
388ProcessLinux::GetPluginCommandHelp(const char *command, Stream *strm)
389{
390}
391
392Error
393ProcessLinux::ExecutePluginCommand(Args &command, Stream *strm)
394{
395 return Error(1, eErrorTypeGeneric);
396}
397
398Log *
399ProcessLinux::EnablePluginLogging(Stream *strm, Args &command)
400{
401 return NULL;
402}
403
404//------------------------------------------------------------------------------
405// Utility functions.
406
407void
408ProcessLinux::UpdateLoadedSections()
409{
410 ObjectFile *obj_file = m_module->GetObjectFile();
411 SectionList *sections = obj_file->GetSectionList();
412
413 // FIXME: SectionList provides iterator types, but no begin/end methods.
414 size_t num_sections = sections->GetSize();
415 for (unsigned i = 0; i < num_sections; ++i)
416 {
417 Section *section = sections->GetSectionAtIndex(i).get();
418
419 lldb::addr_t new_load_addr = section->GetFileAddress();
420 lldb::addr_t old_load_addr = GetSectionLoadAddress(section);
421
422 if (old_load_addr == LLDB_INVALID_ADDRESS ||
423 old_load_addr != new_load_addr)
424 SectionLoaded(section, new_load_addr);
425 }
426}
427
428bool
429ProcessLinux::HasExited()
430{
431 switch (GetPrivateState())
432 {
433 default:
434 break;
435
436 case eStateUnloaded:
437 case eStateCrashed:
438 case eStateDetached:
439 case eStateExited:
440 return true;
441 }
442
443 return false;
444}