blob: fe2f504ebc719016e7506e7885f498a3d67d512d [file] [log] [blame]
Todd Fialaaf245d12014-06-30 21:05:18 +00001//===-- 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 "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 "NativeProcessProtocol.h"
18
19using namespace lldb_private;
20
21// -------------------------------------------------------------------
22// static members
23// -------------------------------------------------------------------
24
25Error
26SoftwareBreakpoint::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
92Error
93SoftwareBreakpoint::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 lldb::addr_t bytes_read = 0;
105
106 Error error = process.ReadMemory(addr, saved_opcode_bytes, static_cast<lldb::addr_t> (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 != static_cast<lldb::addr_t> (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, 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, bytes_read);
120 }
121
122 // Write a software breakpoint in place of the original opcode.
123 lldb::addr_t bytes_written = 0;
124 error = process.WriteMemory (addr, bp_opcode_bytes, static_cast<lldb::addr_t> (bp_opcode_size), bytes_written);
125 if (error.Fail ())
126 {
127 if (log)
128 log->Printf ("SoftwareBreakpoint::%s failed to write memory while attempting to set breakpoint: %s", __FUNCTION__, error.AsCString ());
129 return error;
130 }
131
132 // Ensure we wrote as many bytes as we expected.
133 if (bytes_written != static_cast<lldb::addr_t> (bp_opcode_size))
134 {
135 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, bytes_written);
136 if (log)
137 log->PutCString (error.AsCString ());
138 return error;
139 }
140
141 uint8_t verify_bp_opcode_bytes [MAX_TRAP_OPCODE_SIZE];
142 lldb::addr_t verify_bytes_read = 0;
143 error = process.ReadMemory(addr, verify_bp_opcode_bytes, static_cast<lldb::addr_t> (bp_opcode_size), verify_bytes_read);
144 if (error.Fail ())
145 {
146 if (log)
147 log->Printf ("SoftwareBreakpoint::%s failed to read memory while attempting to verify the breakpoint set: %s", __FUNCTION__, error.AsCString ());
148 return error;
149 }
150
151 // Ensure we read as many verification bytes as we expected.
152 if (verify_bytes_read != static_cast<lldb::addr_t> (bp_opcode_size))
153 {
154 if (log)
155 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, verify_bytes_read);
156 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, verify_bytes_read);
157 }
158
159 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) != 0)
160 {
161 if (log)
162 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);
163 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);
164 }
165
166 if (log)
167 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, addr);
168
169 return Error ();
170}
171
172// -------------------------------------------------------------------
173// instance-level members
174// -------------------------------------------------------------------
175
176SoftwareBreakpoint::SoftwareBreakpoint (NativeProcessProtocol &process, lldb::addr_t addr, const uint8_t *saved_opcodes, const uint8_t *trap_opcodes, size_t opcode_size) :
177 NativeBreakpoint (addr),
178 m_process (process),
179 m_saved_opcodes (),
180 m_trap_opcodes (),
181 m_opcode_size (opcode_size)
182{
183 assert ( opcode_size > 0 && "setting software breakpoint with no trap opcodes");
184 assert ( opcode_size <= MAX_TRAP_OPCODE_SIZE && "trap opcode size too large");
185
186 ::memcpy (m_saved_opcodes, saved_opcodes, opcode_size);
187 ::memcpy (m_trap_opcodes, trap_opcodes, opcode_size);
188}
189
190Error
191SoftwareBreakpoint::DoEnable ()
192{
193 return EnableSoftwareBreakpoint (m_process, m_addr, m_opcode_size, m_trap_opcodes, m_saved_opcodes);
194}
195
196Error
197SoftwareBreakpoint::DoDisable ()
198{
199 Error error;
200 assert (m_addr && (m_addr != LLDB_INVALID_ADDRESS) && "can't remove a software breakpoint for an invalid address");
201
202 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
203 if (log)
204 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, m_addr);
205
206 assert ( (m_opcode_size > 0) && "cannot restore opcodes when there are no opcodes");
207
208 if (m_opcode_size > 0)
209 {
210 // Clear a software breakoint instruction
211 uint8_t curr_break_op [MAX_TRAP_OPCODE_SIZE];
212 bool break_op_found = false;
213 assert (m_opcode_size <= sizeof (curr_break_op));
214
215 // Read the breakpoint opcode
216 lldb::addr_t bytes_read = 0;
217 error = m_process.ReadMemory (m_addr, curr_break_op, m_opcode_size, bytes_read);
218 if (error.Success () && (bytes_read < static_cast<lldb::addr_t> (m_opcode_size)))
219 {
220 error.SetErrorStringWithFormat ("SoftwareBreakpointr::%s addr=0x%" PRIx64 ": tried to read %lu bytes but only read %" PRIu64, __FUNCTION__, m_addr, m_opcode_size, bytes_read);
221 }
222 if (error.Success ())
223 {
224 bool verify = false;
225 // Make sure we have the a breakpoint opcode exists at this address
226 if (::memcmp (curr_break_op, m_trap_opcodes, m_opcode_size) == 0)
227 {
228 break_op_found = true;
229 // We found a valid breakpoint opcode at this address, now restore
230 // the saved opcode.
231 lldb::addr_t bytes_written = 0;
232 error = m_process.WriteMemory (m_addr, m_saved_opcodes, m_opcode_size, bytes_written);
233 if (error.Success () && (bytes_written < static_cast<lldb::addr_t> (m_opcode_size)))
234 {
235 error.SetErrorStringWithFormat ("SoftwareBreakpoint::%s addr=0x%" PRIx64 ": tried to write %lu bytes but only wrote %" PRIu64, __FUNCTION__, m_addr, m_opcode_size, bytes_written);
236 }
237 if (error.Success ())
238 {
239 verify = true;
240 }
241 }
242 else
243 {
244 error.SetErrorString("Original breakpoint trap is no longer in memory.");
245 // Set verify to true and so we can check if the original opcode has already been restored
246 verify = true;
247 }
248
249 if (verify)
250 {
251 uint8_t verify_opcode [MAX_TRAP_OPCODE_SIZE];
252 assert (m_opcode_size <= sizeof (verify_opcode));
253 // Verify that our original opcode made it back to the inferior
254
255 lldb::addr_t verify_bytes_read = 0;
256 error = m_process.ReadMemory (m_addr, verify_opcode, m_opcode_size, verify_bytes_read);
257 if (error.Success () && (verify_bytes_read < static_cast<lldb::addr_t> (m_opcode_size)))
258 {
259 error.SetErrorStringWithFormat ("SoftwareBreakpoint::%s addr=0x%" PRIx64 ": tried to read %lu verification bytes but only read %" PRIu64, __FUNCTION__, m_addr, m_opcode_size, verify_bytes_read);
260 }
261 if (error.Success ())
262 {
263 // compare the memory we just read with the original opcode
264 if (::memcmp (m_saved_opcodes, verify_opcode, m_opcode_size) == 0)
265 {
266 // SUCCESS
267 if (log)
268 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, m_addr);
269 return error;
270 }
271 else
272 {
273 if (break_op_found)
274 error.SetErrorString("Failed to restore original opcode.");
275 }
276 }
277 else
278 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
279 }
280 }
281 }
282
283 if (log && error.Fail ())
284 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- FAILED: %s",
285 __FUNCTION__,
286 m_addr,
287 error.AsCString());
288 return error;
289}
290
291bool
292SoftwareBreakpoint::IsSoftwareBreakpoint () const
293{
294 return true;
295}
296