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