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