1 //===-- SoftwareBreakpoint.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/common/SoftwareBreakpoint.h"
12 #include "lldb/Core/Error.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Host/Debug.h"
16 #include "lldb/Host/common/NativeProcessProtocol.h"
18 using namespace lldb_private;
20 // -------------------------------------------------------------------
22 // -------------------------------------------------------------------
25 SoftwareBreakpoint::CreateSoftwareBreakpoint (NativeProcessProtocol &process, lldb::addr_t addr, size_t size_hint, NativeBreakpointSP &breakpoint_sp)
27 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
29 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, addr);
31 // Validate the address.
32 if (addr == LLDB_INVALID_ADDRESS)
33 return Error ("SoftwareBreakpoint::%s invalid load address specified.", __FUNCTION__);
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);
44 log->Printf ("SoftwareBreakpoint::%s failed to retrieve software breakpoint trap opcode: %s", __FUNCTION__, error.AsCString ());
48 // Validate size of trap opcode.
49 if (bp_opcode_size == 0)
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);
56 if (bp_opcode_size > MAX_TRAP_OPCODE_SIZE)
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);
63 // Validate that we received opcodes.
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);
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);
77 log->Printf ("SoftwareBreakpoint::%s: failed to enable new breakpoint at 0x%" PRIx64 ": %s", __FUNCTION__, addr, error.AsCString ());
82 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, addr);
84 // Set the breakpoint and verified it was written properly. Now
85 // create a breakpoint remover that understands how to undo this
87 breakpoint_sp.reset (new SoftwareBreakpoint (process, addr, saved_opcode_bytes, bp_opcode_bytes, bp_opcode_size));
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)
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");
98 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
100 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, addr);
102 // Save the original opcodes by reading them so we can restore later.
103 size_t bytes_read = 0;
105 Error error = process.ReadMemory(addr, saved_opcode_bytes, bp_opcode_size, bytes_read);
109 log->Printf ("SoftwareBreakpoint::%s failed to read memory while attempting to set breakpoint: %s", __FUNCTION__, error.AsCString ());
113 // Ensure we read as many bytes as we expected.
114 if (bytes_read != bp_opcode_size)
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);
125 for (const uint8_t *read_byte = saved_opcode_bytes; read_byte < saved_opcode_bytes + bp_opcode_size; ++read_byte)
127 log->Printf("SoftwareBreakpoint::%s addr = 0x%" PRIx64
128 " ovewriting byte index %d (was 0x%hhx)",
129 __FUNCTION__, addr, i++, *read_byte);
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);
139 log->Printf ("SoftwareBreakpoint::%s failed to write memory while attempting to set breakpoint: %s", __FUNCTION__, error.AsCString ());
143 // Ensure we wrote as many bytes as we expected.
144 if (bytes_written != bp_opcode_size)
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);
148 log->PutCString (error.AsCString ());
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);
158 log->Printf ("SoftwareBreakpoint::%s failed to read memory while attempting to verify the breakpoint set: %s", __FUNCTION__, error.AsCString ());
162 // Ensure we read as many verification bytes as we expected.
163 if (verify_bytes_read != bp_opcode_size)
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);
170 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) != 0)
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);
178 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, addr);
183 // -------------------------------------------------------------------
184 // instance-level members
185 // -------------------------------------------------------------------
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),
192 m_opcode_size (opcode_size)
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");
197 ::memcpy (m_saved_opcodes, saved_opcodes, opcode_size);
198 ::memcpy (m_trap_opcodes, trap_opcodes, opcode_size);
202 SoftwareBreakpoint::DoEnable ()
204 return EnableSoftwareBreakpoint (m_process, m_addr, m_opcode_size, m_trap_opcodes, m_saved_opcodes);
208 SoftwareBreakpoint::DoDisable ()
211 assert (m_addr && (m_addr != LLDB_INVALID_ADDRESS) && "can't remove a software breakpoint for an invalid address");
213 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
215 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, m_addr);
217 assert ( (m_opcode_size > 0) && "cannot restore opcodes when there are no opcodes");
219 if (m_opcode_size > 0)
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));
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)
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);
233 if (error.Success ())
236 // Make sure the breakpoint opcode exists at this address
237 if (::memcmp (curr_break_op, m_trap_opcodes, m_opcode_size) == 0)
239 break_op_found = true;
240 // We found a valid breakpoint opcode at this address, now restore
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)
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);
248 if (error.Success ())
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
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
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)
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);
272 if (error.Success ())
274 // compare the memory we just read with the original opcode
275 if (::memcmp (m_saved_opcodes, verify_opcode, m_opcode_size) == 0)
281 for (const uint8_t *verify_byte = verify_opcode; verify_byte < verify_opcode + m_opcode_size; ++verify_byte)
283 log->Printf("SoftwareBreakpoint::%s addr = 0x%" PRIx64
284 " replaced byte index %d with 0x%hhx",
285 __FUNCTION__, m_addr, i++, *verify_byte);
287 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, m_addr);
294 error.SetErrorString("Failed to restore original opcode.");
298 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
303 if (log && error.Fail ())
304 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- FAILED: %s",
312 SoftwareBreakpoint::IsSoftwareBreakpoint () const