1 //===-- POSIXThread.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 #include "lldb/lldb-python.h"
11
12 // C Includes
13 #include <errno.h>
14
15 // C++ Includes
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/Breakpoint/Watchpoint.h"
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Core/Debugger.h"
21 #include "lldb/Core/State.h"
22 #include "lldb/Host/Host.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/StopInfo.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Target/ThreadSpec.h"
27 #include "POSIXStopInfo.h"
28 #include "POSIXThread.h"
29 #include "ProcessPOSIX.h"
30 #include "ProcessPOSIXLog.h"
31 #include "ProcessMonitor.h"
32 #include "RegisterContextPOSIXProcessMonitor_mips64.h"
33 #include "RegisterContextPOSIXProcessMonitor_x86.h"
34 #include "RegisterContextLinux_i386.h"
35 #include "RegisterContextLinux_x86_64.h"
36 #include "RegisterContextFreeBSD_i386.h"
37 #include "RegisterContextFreeBSD_mips64.h"
38 #include "RegisterContextFreeBSD_x86_64.h"
39
40 #include "UnwindLLDB.h"
41
42 using namespace lldb;
43 using namespace lldb_private;
44
45
POSIXThread(Process & process,lldb::tid_t tid)46 POSIXThread::POSIXThread(Process &process, lldb::tid_t tid)
47 : Thread(process, tid),
48 m_frame_ap (),
49 m_breakpoint (),
50 m_thread_name_valid (false),
51 m_thread_name (),
52 m_posix_thread(NULL)
53 {
54 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
55 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
56 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ")", __FUNCTION__, tid);
57
58 // Set the current watchpoints for this thread.
59 Target &target = GetProcess()->GetTarget();
60 const WatchpointList &wp_list = target.GetWatchpointList();
61 size_t wp_size = wp_list.GetSize();
62
63 for (uint32_t wp_idx = 0; wp_idx < wp_size; wp_idx++)
64 {
65 lldb::WatchpointSP wp = wp_list.GetByIndex(wp_idx);
66 if (wp.get() && wp->IsEnabled())
67 {
68 // This watchpoint as been enabled; obviously this "new" thread
69 // has been created since that watchpoint was enabled. Since
70 // the POSIXBreakpointProtocol has yet to be initialized, its
71 // m_watchpoints_initialized member will be FALSE. Attempting to
72 // read the debug status register to determine if a watchpoint
73 // has been hit would result in the zeroing of that register.
74 // Since the active debug registers would have been cloned when
75 // this thread was created, simply force the m_watchpoints_initized
76 // member to TRUE and avoid resetting dr6 and dr7.
77 GetPOSIXBreakpointProtocol()->ForceWatchpointsInitialized();
78 }
79 }
80 }
81
~POSIXThread()82 POSIXThread::~POSIXThread()
83 {
84 DestroyThread();
85 }
86
87 ProcessMonitor &
GetMonitor()88 POSIXThread::GetMonitor()
89 {
90 ProcessSP base = GetProcess();
91 ProcessPOSIX &process = static_cast<ProcessPOSIX&>(*base);
92 return process.GetMonitor();
93 }
94
95 // Overridden by FreeBSDThread; this is used only on Linux.
96 void
RefreshStateAfterStop()97 POSIXThread::RefreshStateAfterStop()
98 {
99 // Invalidate all registers in our register context. We don't set "force" to
100 // true because the stop reply packet might have had some register values
101 // that were expedited and these will already be copied into the register
102 // context by the time this function gets called. The KDPRegisterContext
103 // class has been made smart enough to detect when it needs to invalidate
104 // which registers are valid by putting hooks in the register read and
105 // register supply functions where they check the process stop ID and do
106 // the right thing.
107 //if (StateIsStoppedState(GetState())
108 {
109 const bool force = false;
110 GetRegisterContext()->InvalidateIfNeeded (force);
111 }
112 // FIXME: This should probably happen somewhere else.
113 SetResumeState(eStateRunning);
114 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
115 if (log)
116 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ") setting thread resume state to running", __FUNCTION__, GetID());
117 }
118
119 const char *
GetInfo()120 POSIXThread::GetInfo()
121 {
122 return NULL;
123 }
124
125 void
SetName(const char * name)126 POSIXThread::SetName (const char *name)
127 {
128 m_thread_name_valid = (name && name[0]);
129 if (m_thread_name_valid)
130 m_thread_name.assign (name);
131 else
132 m_thread_name.clear();
133 }
134
135 const char *
GetName()136 POSIXThread::GetName ()
137 {
138 if (!m_thread_name_valid)
139 {
140 SetName(Host::GetThreadName(GetProcess()->GetID(), GetID()).c_str());
141 m_thread_name_valid = true;
142 }
143
144 if (m_thread_name.empty())
145 return NULL;
146 return m_thread_name.c_str();
147 }
148
149 lldb::RegisterContextSP
GetRegisterContext()150 POSIXThread::GetRegisterContext()
151 {
152 if (!m_reg_context_sp)
153 {
154 m_posix_thread = NULL;
155
156 RegisterInfoInterface *reg_interface = NULL;
157 const ArchSpec &target_arch = GetProcess()->GetTarget().GetArchitecture();
158
159 switch (target_arch.GetCore())
160 {
161 case ArchSpec::eCore_mips64:
162 {
163 switch (target_arch.GetTriple().getOS())
164 {
165 case llvm::Triple::FreeBSD:
166 reg_interface = new RegisterContextFreeBSD_mips64(target_arch);
167 break;
168 default:
169 assert(false && "OS not supported");
170 break;
171 }
172
173 if (reg_interface)
174 {
175 RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
176 m_posix_thread = reg_ctx;
177 m_reg_context_sp.reset(reg_ctx);
178 }
179 break;
180 }
181
182 case ArchSpec::eCore_x86_32_i386:
183 case ArchSpec::eCore_x86_32_i486:
184 case ArchSpec::eCore_x86_32_i486sx:
185 case ArchSpec::eCore_x86_64_x86_64:
186 {
187 switch (target_arch.GetTriple().getOS())
188 {
189 case llvm::Triple::FreeBSD:
190 reg_interface = new RegisterContextFreeBSD_x86_64(target_arch);
191 break;
192 case llvm::Triple::Linux:
193 reg_interface = new RegisterContextLinux_x86_64(target_arch);
194 break;
195 default:
196 assert(false && "OS not supported");
197 break;
198 }
199
200 if (reg_interface)
201 {
202 RegisterContextPOSIXProcessMonitor_x86_64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_x86_64(*this, 0, reg_interface);
203 m_posix_thread = reg_ctx;
204 m_reg_context_sp.reset(reg_ctx);
205 }
206 break;
207 }
208
209 default:
210 assert(false && "CPU type not supported!");
211 break;
212 }
213 }
214 return m_reg_context_sp;
215 }
216
217 lldb::RegisterContextSP
CreateRegisterContextForFrame(lldb_private::StackFrame * frame)218 POSIXThread::CreateRegisterContextForFrame(lldb_private::StackFrame *frame)
219 {
220 lldb::RegisterContextSP reg_ctx_sp;
221 uint32_t concrete_frame_idx = 0;
222
223 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
224 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
225 log->Printf ("POSIXThread::%s ()", __FUNCTION__);
226
227 if (frame)
228 concrete_frame_idx = frame->GetConcreteFrameIndex();
229
230 if (concrete_frame_idx == 0)
231 reg_ctx_sp = GetRegisterContext();
232 else
233 {
234 assert(GetUnwinder());
235 reg_ctx_sp = GetUnwinder()->CreateRegisterContextForFrame(frame);
236 }
237
238 return reg_ctx_sp;
239 }
240
241 lldb::addr_t
GetThreadPointer()242 POSIXThread::GetThreadPointer ()
243 {
244 ProcessMonitor &monitor = GetMonitor();
245 addr_t addr;
246 if (monitor.ReadThreadPointer (GetID(), addr))
247 return addr;
248 else
249 return LLDB_INVALID_ADDRESS;
250 }
251
252 bool
CalculateStopInfo()253 POSIXThread::CalculateStopInfo()
254 {
255 SetStopInfo (m_stop_info_sp);
256 return true;
257 }
258
259 Unwind *
GetUnwinder()260 POSIXThread::GetUnwinder()
261 {
262 if (m_unwinder_ap.get() == NULL)
263 m_unwinder_ap.reset(new UnwindLLDB(*this));
264
265 return m_unwinder_ap.get();
266 }
267
268 // Overridden by FreeBSDThread; this is used only on Linux.
269 void
WillResume(lldb::StateType resume_state)270 POSIXThread::WillResume(lldb::StateType resume_state)
271 {
272 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
273 if (log)
274 log->Printf ("POSIXThread::%s (tid = %" PRIi64 ") setting thread resume state to %s", __FUNCTION__, GetID(), StateAsCString(resume_state));
275 // TODO: the line below shouldn't really be done, but
276 // the POSIXThread might rely on this so I will leave this in for now
277 SetResumeState(resume_state);
278 }
279
280 void
DidStop()281 POSIXThread::DidStop()
282 {
283 // Don't set the thread state to stopped unless we really stopped.
284 }
285
286 bool
Resume()287 POSIXThread::Resume()
288 {
289 lldb::StateType resume_state = GetResumeState();
290 ProcessMonitor &monitor = GetMonitor();
291 bool status;
292
293 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
294 if (log)
295 log->Printf ("POSIXThread::%s (), resume_state = %s", __FUNCTION__,
296 StateAsCString(resume_state));
297
298 switch (resume_state)
299 {
300 default:
301 assert(false && "Unexpected state for resume!");
302 status = false;
303 break;
304
305 case lldb::eStateRunning:
306 SetState(resume_state);
307 status = monitor.Resume(GetID(), GetResumeSignal());
308 break;
309
310 case lldb::eStateStepping:
311 SetState(resume_state);
312 status = monitor.SingleStep(GetID(), GetResumeSignal());
313 break;
314 case lldb::eStateStopped:
315 case lldb::eStateSuspended:
316 status = true;
317 break;
318 }
319
320 return status;
321 }
322
323 void
Notify(const ProcessMessage & message)324 POSIXThread::Notify(const ProcessMessage &message)
325 {
326 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
327 if (log)
328 log->Printf ("POSIXThread::%s () message kind = '%s' for tid %" PRIu64,
329 __FUNCTION__, message.PrintKind(), GetID());
330
331 switch (message.GetKind())
332 {
333 default:
334 assert(false && "Unexpected message kind!");
335 break;
336
337 case ProcessMessage::eExitMessage:
338 // Nothing to be done.
339 break;
340
341 case ProcessMessage::eLimboMessage:
342 LimboNotify(message);
343 break;
344
345 case ProcessMessage::eSignalMessage:
346 SignalNotify(message);
347 break;
348
349 case ProcessMessage::eSignalDeliveredMessage:
350 SignalDeliveredNotify(message);
351 break;
352
353 case ProcessMessage::eTraceMessage:
354 TraceNotify(message);
355 break;
356
357 case ProcessMessage::eBreakpointMessage:
358 BreakNotify(message);
359 break;
360
361 case ProcessMessage::eWatchpointMessage:
362 WatchNotify(message);
363 break;
364
365 case ProcessMessage::eCrashMessage:
366 CrashNotify(message);
367 break;
368
369 case ProcessMessage::eNewThreadMessage:
370 ThreadNotify(message);
371 break;
372
373 case ProcessMessage::eExecMessage:
374 ExecNotify(message);
375 break;
376 }
377 }
378
379 bool
EnableHardwareWatchpoint(Watchpoint * wp)380 POSIXThread::EnableHardwareWatchpoint(Watchpoint *wp)
381 {
382 bool wp_set = false;
383 if (wp)
384 {
385 addr_t wp_addr = wp->GetLoadAddress();
386 size_t wp_size = wp->GetByteSize();
387 bool wp_read = wp->WatchpointRead();
388 bool wp_write = wp->WatchpointWrite();
389 uint32_t wp_hw_index = wp->GetHardwareIndex();
390 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
391 if (reg_ctx)
392 wp_set = reg_ctx->SetHardwareWatchpointWithIndex(wp_addr, wp_size,
393 wp_read, wp_write,
394 wp_hw_index);
395 }
396 return wp_set;
397 }
398
399 bool
DisableHardwareWatchpoint(Watchpoint * wp)400 POSIXThread::DisableHardwareWatchpoint(Watchpoint *wp)
401 {
402 bool result = false;
403 if (wp)
404 {
405 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
406 if (reg_ctx_sp.get())
407 result = reg_ctx_sp->ClearHardwareWatchpoint(wp->GetHardwareIndex());
408 }
409 return result;
410 }
411
412 uint32_t
NumSupportedHardwareWatchpoints()413 POSIXThread::NumSupportedHardwareWatchpoints()
414 {
415 lldb::RegisterContextSP reg_ctx_sp = GetRegisterContext();
416 if (reg_ctx_sp.get())
417 return reg_ctx_sp->NumSupportedHardwareWatchpoints();
418 return 0;
419 }
420
421 uint32_t
FindVacantWatchpointIndex()422 POSIXThread::FindVacantWatchpointIndex()
423 {
424 uint32_t hw_index = LLDB_INVALID_INDEX32;
425 uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
426 uint32_t wp_idx;
427 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
428 if (reg_ctx)
429 {
430 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
431 {
432 if (reg_ctx->IsWatchpointVacant(wp_idx))
433 {
434 hw_index = wp_idx;
435 break;
436 }
437 }
438 }
439 return hw_index;
440 }
441
442 void
BreakNotify(const ProcessMessage & message)443 POSIXThread::BreakNotify(const ProcessMessage &message)
444 {
445 bool status;
446 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
447
448 assert(GetRegisterContext());
449 status = GetPOSIXBreakpointProtocol()->UpdateAfterBreakpoint();
450 assert(status && "Breakpoint update failed!");
451
452 // With our register state restored, resolve the breakpoint object
453 // corresponding to our current PC.
454 assert(GetRegisterContext());
455 lldb::addr_t pc = GetRegisterContext()->GetPC();
456 if (log)
457 log->Printf ("POSIXThread::%s () PC=0x%8.8" PRIx64, __FUNCTION__, pc);
458 lldb::BreakpointSiteSP bp_site(GetProcess()->GetBreakpointSiteList().FindByAddress(pc));
459
460 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
461 // we create a stop reason with should_stop=false. If there is no breakpoint location, then report
462 // an invalid stop reason. We don't need to worry about stepping over the breakpoint here, that will
463 // be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
464 if (bp_site)
465 {
466 lldb::break_id_t bp_id = bp_site->GetID();
467 if (bp_site->ValidForThisThread(this))
468 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id));
469 else
470 {
471 const bool should_stop = false;
472 SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID(*this, bp_id, should_stop));
473 }
474 }
475 else
476 SetStopInfo(StopInfoSP());
477 }
478
479 void
WatchNotify(const ProcessMessage & message)480 POSIXThread::WatchNotify(const ProcessMessage &message)
481 {
482 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
483
484 lldb::addr_t halt_addr = message.GetHWAddress();
485 if (log)
486 log->Printf ("POSIXThread::%s () Hardware Watchpoint Address = 0x%8.8"
487 PRIx64, __FUNCTION__, halt_addr);
488
489 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
490 if (reg_ctx)
491 {
492 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
493 uint32_t wp_idx;
494 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
495 {
496 if (reg_ctx->IsWatchpointHit(wp_idx))
497 {
498 // Clear the watchpoint hit here
499 reg_ctx->ClearWatchpointHits();
500 break;
501 }
502 }
503
504 if (wp_idx == num_hw_wps)
505 return;
506
507 Target &target = GetProcess()->GetTarget();
508 lldb::addr_t wp_monitor_addr = reg_ctx->GetWatchpointAddress(wp_idx);
509 const WatchpointList &wp_list = target.GetWatchpointList();
510 lldb::WatchpointSP wp_sp = wp_list.FindByAddress(wp_monitor_addr);
511
512 assert(wp_sp.get() && "No watchpoint found");
513 SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID(*this,
514 wp_sp->GetID()));
515 }
516 }
517
518 void
TraceNotify(const ProcessMessage & message)519 POSIXThread::TraceNotify(const ProcessMessage &message)
520 {
521 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
522 if (reg_ctx)
523 {
524 uint32_t num_hw_wps = reg_ctx->NumSupportedHardwareWatchpoints();
525 uint32_t wp_idx;
526 for (wp_idx = 0; wp_idx < num_hw_wps; wp_idx++)
527 {
528 if (reg_ctx->IsWatchpointHit(wp_idx))
529 {
530 WatchNotify(message);
531 return;
532 }
533 }
534 }
535
536 SetStopInfo (StopInfo::CreateStopReasonToTrace(*this));
537 }
538
539 void
LimboNotify(const ProcessMessage & message)540 POSIXThread::LimboNotify(const ProcessMessage &message)
541 {
542 SetStopInfo (lldb::StopInfoSP(new POSIXLimboStopInfo(*this)));
543 }
544
545 void
SignalNotify(const ProcessMessage & message)546 POSIXThread::SignalNotify(const ProcessMessage &message)
547 {
548 int signo = message.GetSignal();
549
550 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
551 SetResumeSignal(signo);
552 }
553
554 void
SignalDeliveredNotify(const ProcessMessage & message)555 POSIXThread::SignalDeliveredNotify(const ProcessMessage &message)
556 {
557 int signo = message.GetSignal();
558
559 SetStopInfo (StopInfo::CreateStopReasonWithSignal(*this, signo));
560 SetResumeSignal(signo);
561 }
562
563 void
CrashNotify(const ProcessMessage & message)564 POSIXThread::CrashNotify(const ProcessMessage &message)
565 {
566 // FIXME: Update stop reason as per bugzilla 14598
567 int signo = message.GetSignal();
568
569 assert(message.GetKind() == ProcessMessage::eCrashMessage);
570
571 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
572 if (log)
573 log->Printf ("POSIXThread::%s () signo = %i, reason = '%s'",
574 __FUNCTION__, signo, message.PrintCrashReason());
575
576 SetStopInfo (lldb::StopInfoSP(new POSIXCrashStopInfo(*this, signo,
577 message.GetCrashReason(),
578 message.GetFaultAddress())));
579 SetResumeSignal(signo);
580 }
581
582 void
ThreadNotify(const ProcessMessage & message)583 POSIXThread::ThreadNotify(const ProcessMessage &message)
584 {
585 SetStopInfo (lldb::StopInfoSP(new POSIXNewThreadStopInfo(*this)));
586 }
587
588 unsigned
GetRegisterIndexFromOffset(unsigned offset)589 POSIXThread::GetRegisterIndexFromOffset(unsigned offset)
590 {
591 unsigned reg = LLDB_INVALID_REGNUM;
592 ArchSpec arch = Host::GetArchitecture();
593
594 switch (arch.GetCore())
595 {
596 default:
597 llvm_unreachable("CPU type not supported!");
598 break;
599
600 case ArchSpec::eCore_mips64:
601 case ArchSpec::eCore_x86_32_i386:
602 case ArchSpec::eCore_x86_32_i486:
603 case ArchSpec::eCore_x86_32_i486sx:
604 case ArchSpec::eCore_x86_64_x86_64:
605 {
606 POSIXBreakpointProtocol* reg_ctx = GetPOSIXBreakpointProtocol();
607 reg = reg_ctx->GetRegisterIndexFromOffset(offset);
608 }
609 break;
610 }
611 return reg;
612 }
613
614 void
ExecNotify(const ProcessMessage & message)615 POSIXThread::ExecNotify(const ProcessMessage &message)
616 {
617 SetStopInfo (StopInfo::CreateStopReasonWithExec(*this));
618 }
619
620 const char *
GetRegisterName(unsigned reg)621 POSIXThread::GetRegisterName(unsigned reg)
622 {
623 const char * name = nullptr;
624 ArchSpec arch = Host::GetArchitecture();
625
626 switch (arch.GetCore())
627 {
628 default:
629 assert(false && "CPU type not supported!");
630 break;
631
632 case ArchSpec::eCore_mips64:
633 case ArchSpec::eCore_x86_32_i386:
634 case ArchSpec::eCore_x86_32_i486:
635 case ArchSpec::eCore_x86_32_i486sx:
636 case ArchSpec::eCore_x86_64_x86_64:
637 name = GetRegisterContext()->GetRegisterName(reg);
638 break;
639 }
640 return name;
641 }
642
643 const char *
GetRegisterNameFromOffset(unsigned offset)644 POSIXThread::GetRegisterNameFromOffset(unsigned offset)
645 {
646 return GetRegisterName(GetRegisterIndexFromOffset(offset));
647 }
648
649