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