1 //===-- ThreadElfCore.h ----------------------------------------*- 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 #ifndef liblldb_ThreadElfCore_h_
11 #define liblldb_ThreadElfCore_h_
12 
13 #include <string>
14 
15 #include "lldb/Target/Thread.h"
16 #include "lldb/Core/DataExtractor.h"
17 
18 struct compat_timeval
19 {
20     int64_t tv_sec;
21     int32_t tv_usec;
22 };
23 
24 // PRSTATUS structure's size differs based on architecture.
25 // Currently parsing done only for x86-64 architecture by
26 // simply reading data from the buffer.
27 // The following macros are used to specify the size.
28 // Calculating size using sizeof() wont work because of padding.
29 #define ELFLINUXPRSTATUS64_SIZE (112)
30 #define ELFLINUXPRPSINFO64_SIZE (132)
31 
32 #undef si_signo
33 #undef si_code
34 #undef si_errno
35 
36 struct ELFLinuxPrStatus
37 {
38     int32_t         si_signo;
39     int32_t         si_code;
40     int32_t         si_errno;
41 
42     int16_t         pr_cursig;
43 
44     uint64_t        pr_sigpend;
45     uint64_t        pr_sighold;
46 
47     uint32_t        pr_pid;
48     uint32_t        pr_ppid;
49     uint32_t        pr_pgrp;
50     uint32_t        pr_sid;
51 
52     compat_timeval  pr_utime;
53     compat_timeval  pr_stime;
54     compat_timeval  pr_cutime;
55     compat_timeval  pr_cstime;
56 
57     ELFLinuxPrStatus();
58 
59     bool
60     Parse(lldb_private::DataExtractor &data, lldb_private::ArchSpec &arch);
61 
62     static size_t
63     GetSize(lldb_private::ArchSpec &arch)
64     {
65         switch(arch.GetCore())
66         {
67             case lldb_private::ArchSpec::eCore_x86_64_x86_64:
68                 return ELFLINUXPRSTATUS64_SIZE;
69             default:
70                 return 0;
71         }
72     }
73 };
74 
75 struct ELFLinuxPrPsInfo
76 {
77     char        pr_state;
78     char        pr_sname;
79     char        pr_zomb;
80     char        pr_nice;
81     uint64_t    pr_flag;
82     uint32_t    pr_uid;
83     uint32_t    pr_gid;
84     int32_t     pr_pid;
85     int32_t     pr_ppid;
86     int32_t     pr_pgrp;
87     int32_t     pr_sid;
88     char        pr_fname[16];
89     char        pr_psargs[80];
90 
91     ELFLinuxPrPsInfo();
92 
93     bool
94     Parse(lldb_private::DataExtractor &data, lldb_private::ArchSpec &arch);
95 
96     static size_t
97     GetSize(lldb_private::ArchSpec &arch)
98     {
99         switch(arch.GetCore())
100         {
101             case lldb_private::ArchSpec::eCore_x86_64_x86_64:
102                 return ELFLINUXPRPSINFO64_SIZE;
103             default:
104                 return 0;
105         }
106     }
107 
108 };
109 
110 struct ThreadData
111 {
112     lldb_private::DataExtractor gpregset;
113     lldb_private::DataExtractor fpregset;
114     lldb_private::DataExtractor vregset;
115     lldb::tid_t tid;
116     int signo;
117     std::string name;
118 };
119 
120 class ThreadElfCore : public lldb_private::Thread
121 {
122 public:
123     ThreadElfCore (lldb_private::Process &process, const ThreadData &td);
124 
125     virtual
126     ~ThreadElfCore ();
127 
128     virtual void
129     RefreshStateAfterStop();
130 
131     virtual lldb::RegisterContextSP
132     GetRegisterContext ();
133 
134     virtual lldb::RegisterContextSP
135     CreateRegisterContextForFrame (lldb_private::StackFrame *frame);
136 
137     virtual void
138     ClearStackFrames ();
139 
140     static bool
141     ThreadIDIsValid (lldb::tid_t thread)
142     {
143         return thread != 0;
144     }
145 
146     virtual const char *
147     GetName ()
148     {
149         if (m_thread_name.empty())
150             return NULL;
151         return m_thread_name.c_str();
152     }
153 
154     void
155     SetName (const char *name)
156     {
157         if (name && name[0])
158             m_thread_name.assign (name);
159         else
160             m_thread_name.clear();
161     }
162 
163 protected:
164     //------------------------------------------------------------------
165     // Member variables.
166     //------------------------------------------------------------------
167     std::string m_thread_name;
168     lldb::RegisterContextSP m_thread_reg_ctx_sp;
169 
170     int m_signo;
171 
172     lldb_private::DataExtractor m_gpregset_data;
173     lldb_private::DataExtractor m_fpregset_data;
174     lldb_private::DataExtractor m_vregset_data;
175 
176     virtual bool CalculateStopInfo();
177 
178 };
179 
180 #endif  // liblldb_ThreadElfCore_h_
181