]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/SoftwareBreakpoint.cpp
Merge ACPICA 20170119.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / common / SoftwareBreakpoint.cpp
1 //===-- SoftwareBreakpoint.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/Host/common/SoftwareBreakpoint.h"
11
12 #include "lldb/Core/Error.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Host/Debug.h"
15
16 #include "lldb/Host/common/NativeProcessProtocol.h"
17
18 using namespace lldb_private;
19
20 // -------------------------------------------------------------------
21 // static members
22 // -------------------------------------------------------------------
23
24 Error
25 SoftwareBreakpoint::CreateSoftwareBreakpoint (NativeProcessProtocol &process, lldb::addr_t addr, size_t size_hint, NativeBreakpointSP &breakpoint_sp)
26 {
27     Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
28     if (log)
29         log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, addr);
30
31     // Validate the address.
32     if (addr == LLDB_INVALID_ADDRESS)
33         return Error ("SoftwareBreakpoint::%s invalid load address specified.", __FUNCTION__);
34
35     // Ask the NativeProcessProtocol subclass to fill in the correct software breakpoint
36     // trap for the breakpoint site.
37     size_t bp_opcode_size = 0;
38     const uint8_t *bp_opcode_bytes = NULL;
39     Error error = process.GetSoftwareBreakpointTrapOpcode (size_hint, bp_opcode_size, bp_opcode_bytes);
40
41     if (error.Fail ())
42     {
43         if (log)
44             log->Printf ("SoftwareBreakpoint::%s failed to retrieve software breakpoint trap opcode: %s", __FUNCTION__, error.AsCString ());
45         return error;
46     }
47
48     // Validate size of trap opcode.
49     if (bp_opcode_size == 0)
50     {
51         if (log)
52             log->Printf ("SoftwareBreakpoint::%s failed to retrieve any trap opcodes", __FUNCTION__);
53         return Error ("SoftwareBreakpoint::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%" PRIx64, addr);
54     }
55
56     if (bp_opcode_size > MAX_TRAP_OPCODE_SIZE)
57     {
58         if (log)
59             log->Printf ("SoftwareBreakpoint::%s cannot support %lu trapcode bytes, max size is %lu", __FUNCTION__, bp_opcode_size, MAX_TRAP_OPCODE_SIZE);
60         return Error ("SoftwareBreakpoint::GetSoftwareBreakpointTrapOpcode() returned too many trap opcode bytes: requires %lu but we only support a max of %lu", bp_opcode_size, MAX_TRAP_OPCODE_SIZE);
61     }
62
63     // Validate that we received opcodes.
64     if (!bp_opcode_bytes)
65     {
66         if (log)
67             log->Printf ("SoftwareBreakpoint::%s failed to retrieve trap opcode bytes", __FUNCTION__);
68         return Error ("SoftwareBreakpoint::GetSoftwareBreakpointTrapOpcode() returned NULL trap opcode bytes, unable to get breakpoint trap for address 0x%" PRIx64, addr);
69     }
70
71     // Enable the breakpoint.
72     uint8_t saved_opcode_bytes [MAX_TRAP_OPCODE_SIZE];
73     error = EnableSoftwareBreakpoint (process, addr, bp_opcode_size, bp_opcode_bytes, saved_opcode_bytes);
74     if (error.Fail ())
75     {
76         if (log)
77             log->Printf ("SoftwareBreakpoint::%s: failed to enable new breakpoint at 0x%" PRIx64 ": %s", __FUNCTION__, addr, error.AsCString ());
78         return error;
79     }
80
81     if (log)
82         log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, addr);
83
84     // Set the breakpoint and verified it was written properly.  Now
85     // create a breakpoint remover that understands how to undo this
86     // breakpoint.
87     breakpoint_sp.reset (new SoftwareBreakpoint (process, addr, saved_opcode_bytes, bp_opcode_bytes, bp_opcode_size));
88     return Error ();
89 }
90
91 Error
92 SoftwareBreakpoint::EnableSoftwareBreakpoint (NativeProcessProtocol &process, lldb::addr_t addr, size_t bp_opcode_size, const uint8_t *bp_opcode_bytes, uint8_t *saved_opcode_bytes)
93 {
94     assert (bp_opcode_size <= MAX_TRAP_OPCODE_SIZE && "bp_opcode_size out of valid range");
95     assert (bp_opcode_bytes && "bp_opcode_bytes is NULL");
96     assert (saved_opcode_bytes && "saved_opcode_bytes is NULL");
97
98     Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
99     if (log)
100         log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, addr);
101
102     // Save the original opcodes by reading them so we can restore later.
103     size_t bytes_read = 0;
104
105     Error error = process.ReadMemory(addr, saved_opcode_bytes, bp_opcode_size, bytes_read);
106     if (error.Fail ())
107     {
108         if (log)
109             log->Printf ("SoftwareBreakpoint::%s failed to read memory while attempting to set breakpoint: %s", __FUNCTION__, error.AsCString ());
110         return error;
111     }
112
113     // Ensure we read as many bytes as we expected.
114     if (bytes_read != bp_opcode_size)
115     {
116         if (log)
117             log->Printf ("SoftwareBreakpoint::%s failed to read memory while attempting to set breakpoint: attempted to read %lu bytes but only read %" PRIu64, __FUNCTION__, bp_opcode_size, (uint64_t)bytes_read);
118         return Error ("SoftwareBreakpoint::%s failed to read memory while attempting to set breakpoint: attempted to read %lu bytes but only read %" PRIu64, __FUNCTION__, bp_opcode_size, (uint64_t)bytes_read);
119     }
120
121     // Log what we read.
122     if (log)
123     {
124         int i = 0;
125         for (const uint8_t *read_byte = saved_opcode_bytes; read_byte < saved_opcode_bytes + bp_opcode_size; ++read_byte)
126         {
127             log->Printf("SoftwareBreakpoint::%s addr = 0x%" PRIx64
128                     " ovewriting byte index %d (was 0x%hhx)",
129                     __FUNCTION__, addr, i++, *read_byte);
130         }
131     }
132
133     // Write a software breakpoint in place of the original opcode.
134     size_t bytes_written = 0;
135     error = process.WriteMemory(addr, bp_opcode_bytes, bp_opcode_size, bytes_written);
136     if (error.Fail ())
137     {
138         if (log)
139             log->Printf ("SoftwareBreakpoint::%s failed to write memory while attempting to set breakpoint: %s", __FUNCTION__, error.AsCString ());
140         return error;
141     }
142
143     // Ensure we wrote as many bytes as we expected.
144     if (bytes_written != bp_opcode_size)
145     {
146         error.SetErrorStringWithFormat("SoftwareBreakpoint::%s failed write memory while attempting to set breakpoint: attempted to write %lu bytes but only wrote %" PRIu64, __FUNCTION__, bp_opcode_size, (uint64_t)bytes_written);
147         if (log)
148             log->PutCString (error.AsCString ());
149         return error;
150     }
151
152     uint8_t verify_bp_opcode_bytes [MAX_TRAP_OPCODE_SIZE];
153     size_t verify_bytes_read = 0;
154     error = process.ReadMemory(addr, verify_bp_opcode_bytes, bp_opcode_size, verify_bytes_read);
155     if (error.Fail ())
156     {
157         if (log)
158             log->Printf ("SoftwareBreakpoint::%s failed to read memory while attempting to verify the breakpoint set: %s", __FUNCTION__, error.AsCString ());
159         return error;
160     }
161
162     // Ensure we read as many verification bytes as we expected.
163     if (verify_bytes_read != bp_opcode_size)
164     {
165         if (log)
166             log->Printf ("SoftwareBreakpoint::%s failed to read memory while attempting to verify breakpoint: attempted to read %lu bytes but only read %" PRIu64, __FUNCTION__, bp_opcode_size, (uint64_t)verify_bytes_read);
167         return Error ("SoftwareBreakpoint::%s failed to read memory while attempting to verify breakpoint: attempted to read %lu bytes but only read %" PRIu64, __FUNCTION__, bp_opcode_size, (uint64_t)verify_bytes_read);
168     }
169
170     if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) != 0)
171     {
172         if (log)
173             log->Printf ("SoftwareBreakpoint::%s: verification of software breakpoint writing failed - trap opcodes not successfully read back after writing when setting breakpoint at 0x%" PRIx64, __FUNCTION__, addr);
174         return Error ("SoftwareBreakpoint::%s: verification of software breakpoint writing failed - trap opcodes not successfully read back after writing when setting breakpoint at 0x%" PRIx64, __FUNCTION__, addr);
175     }
176
177     if (log)
178         log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, addr);
179
180     return Error ();
181 }
182
183 // -------------------------------------------------------------------
184 // instance-level members
185 // -------------------------------------------------------------------
186
187 SoftwareBreakpoint::SoftwareBreakpoint (NativeProcessProtocol &process, lldb::addr_t addr, const uint8_t *saved_opcodes, const uint8_t *trap_opcodes, size_t opcode_size) :
188     NativeBreakpoint (addr),
189     m_process (process),
190     m_saved_opcodes (),
191     m_trap_opcodes (),
192     m_opcode_size (opcode_size)
193 {
194     assert ( opcode_size > 0 && "setting software breakpoint with no trap opcodes");
195     assert ( opcode_size <= MAX_TRAP_OPCODE_SIZE  && "trap opcode size too large");
196
197     ::memcpy (m_saved_opcodes, saved_opcodes, opcode_size);
198     ::memcpy (m_trap_opcodes, trap_opcodes, opcode_size);
199 }
200
201 Error
202 SoftwareBreakpoint::DoEnable ()
203 {
204     return EnableSoftwareBreakpoint (m_process, m_addr, m_opcode_size, m_trap_opcodes, m_saved_opcodes);
205 }
206
207 Error
208 SoftwareBreakpoint::DoDisable ()
209 {
210     Error error;
211     assert (m_addr && (m_addr != LLDB_INVALID_ADDRESS) && "can't remove a software breakpoint for an invalid address");
212
213     Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
214     if (log)
215         log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, m_addr);
216
217     assert ( (m_opcode_size > 0) && "cannot restore opcodes when there are no opcodes");
218
219     if (m_opcode_size > 0)
220     {
221         // Clear a software breakpoint instruction
222         uint8_t curr_break_op [MAX_TRAP_OPCODE_SIZE];
223         bool break_op_found = false;
224         assert (m_opcode_size <= sizeof (curr_break_op));
225
226         // Read the breakpoint opcode
227         size_t bytes_read = 0;
228         error = m_process.ReadMemory (m_addr, curr_break_op, m_opcode_size, bytes_read);
229         if (error.Success() && bytes_read < m_opcode_size)
230         {
231             error.SetErrorStringWithFormat ("SoftwareBreakpointr::%s addr=0x%" PRIx64 ": tried to read %lu bytes but only read %" PRIu64, __FUNCTION__, m_addr, m_opcode_size, (uint64_t)bytes_read);
232         }
233         if (error.Success ())
234         {
235             bool verify = false;
236             // Make sure the breakpoint opcode exists at this address
237             if (::memcmp (curr_break_op, m_trap_opcodes, m_opcode_size) == 0)
238             {
239                 break_op_found = true;
240                 // We found a valid breakpoint opcode at this address, now restore
241                 // the saved opcode.
242                 size_t bytes_written = 0;
243                 error = m_process.WriteMemory (m_addr, m_saved_opcodes, m_opcode_size, bytes_written);
244                 if (error.Success() && bytes_written < m_opcode_size)
245                 {
246                     error.SetErrorStringWithFormat ("SoftwareBreakpoint::%s addr=0x%" PRIx64 ": tried to write %lu bytes but only wrote %" PRIu64, __FUNCTION__, m_addr, m_opcode_size, (uint64_t)bytes_written);
247                 }
248                 if (error.Success ())
249                 {
250                     verify = true;
251                 }
252             }
253             else
254             {
255                 error.SetErrorString("Original breakpoint trap is no longer in memory.");
256                 // Set verify to true and so we can check if the original opcode has already been restored
257                 verify = true;
258             }
259
260             if (verify)
261             {
262                 uint8_t verify_opcode [MAX_TRAP_OPCODE_SIZE];
263                 assert (m_opcode_size <= sizeof (verify_opcode));
264                 // Verify that our original opcode made it back to the inferior
265
266                 size_t verify_bytes_read = 0;
267                 error = m_process.ReadMemory (m_addr, verify_opcode, m_opcode_size, verify_bytes_read);
268                 if (error.Success() && verify_bytes_read < m_opcode_size)
269                 {
270                     error.SetErrorStringWithFormat ("SoftwareBreakpoint::%s addr=0x%" PRIx64 ": tried to read %lu verification bytes but only read %" PRIu64, __FUNCTION__, m_addr, m_opcode_size, (uint64_t)verify_bytes_read);
271                 }
272                 if (error.Success ())
273                 {
274                     // compare the memory we just read with the original opcode
275                     if (::memcmp (m_saved_opcodes, verify_opcode, m_opcode_size) == 0)
276                     {
277                         // SUCCESS
278                         if (log)
279                         {
280                             int i = 0;
281                             for (const uint8_t *verify_byte = verify_opcode; verify_byte < verify_opcode + m_opcode_size; ++verify_byte)
282                             {
283                                 log->Printf("SoftwareBreakpoint::%s addr = 0x%" PRIx64
284                                         " replaced byte index %d with 0x%hhx",
285                                         __FUNCTION__, m_addr, i++, *verify_byte);
286                             }
287                             log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, m_addr);
288                         }
289                         return error;
290                     }
291                     else
292                     {
293                         if (break_op_found)
294                             error.SetErrorString("Failed to restore original opcode.");
295                     }
296                 }
297                 else
298                     error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
299             }
300         }
301     }
302
303     if (log && error.Fail ())
304         log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- FAILED: %s",
305                 __FUNCTION__,
306                 m_addr,
307                 error.AsCString());
308     return error;
309 }
310
311 bool
312 SoftwareBreakpoint::IsSoftwareBreakpoint () const
313 {
314     return true;
315 }
316