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