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 "SoftwareBreakpoint.h"
12 #include "lldb/Core/Error.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Host/Debug.h"
15 #include "lldb/Host/Mutex.h"
17 #include "NativeProcessProtocol.h"
19 using namespace lldb_private;
21 // -------------------------------------------------------------------
23 // -------------------------------------------------------------------
26 SoftwareBreakpoint::CreateSoftwareBreakpoint (NativeProcessProtocol &process, lldb::addr_t addr, size_t size_hint, NativeBreakpointSP &breakpoint_sp)
28 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
30 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, addr);
32 // Validate the address.
33 if (addr == LLDB_INVALID_ADDRESS)
34 return Error ("SoftwareBreakpoint::%s invalid load address specified.", __FUNCTION__);
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);
45 log->Printf ("SoftwareBreakpoint::%s failed to retrieve software breakpoint trap opcode: %s", __FUNCTION__, error.AsCString ());
49 // Validate size of trap opcode.
50 if (bp_opcode_size == 0)
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);
57 if (bp_opcode_size > MAX_TRAP_OPCODE_SIZE)
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);
64 // Validate that we received opcodes.
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);
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);
78 log->Printf ("SoftwareBreakpoint::%s: failed to enable new breakpoint at 0x%" PRIx64 ": %s", __FUNCTION__, addr, error.AsCString ());
83 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, addr);
85 // Set the breakpoint and verified it was written properly. Now
86 // create a breakpoint remover that understands how to undo this
88 breakpoint_sp.reset (new SoftwareBreakpoint (process, addr, saved_opcode_bytes, bp_opcode_bytes, bp_opcode_size));
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)
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");
99 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
101 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, addr);
103 // Save the original opcodes by reading them so we can restore later.
104 lldb::addr_t bytes_read = 0;
106 Error error = process.ReadMemory(addr, saved_opcode_bytes, static_cast<lldb::addr_t> (bp_opcode_size), bytes_read);
110 log->Printf ("SoftwareBreakpoint::%s failed to read memory while attempting to set breakpoint: %s", __FUNCTION__, error.AsCString ());
114 // Ensure we read as many bytes as we expected.
115 if (bytes_read != static_cast<lldb::addr_t> (bp_opcode_size))
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);
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);
128 log->Printf ("SoftwareBreakpoint::%s failed to write memory while attempting to set breakpoint: %s", __FUNCTION__, error.AsCString ());
132 // Ensure we wrote as many bytes as we expected.
133 if (bytes_written != static_cast<lldb::addr_t> (bp_opcode_size))
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);
137 log->PutCString (error.AsCString ());
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);
147 log->Printf ("SoftwareBreakpoint::%s failed to read memory while attempting to verify the breakpoint set: %s", __FUNCTION__, error.AsCString ());
151 // Ensure we read as many verification bytes as we expected.
152 if (verify_bytes_read != static_cast<lldb::addr_t> (bp_opcode_size))
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);
159 if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) != 0)
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);
167 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, addr);
172 // -------------------------------------------------------------------
173 // instance-level members
174 // -------------------------------------------------------------------
176 SoftwareBreakpoint::SoftwareBreakpoint (NativeProcessProtocol &process, lldb::addr_t addr, const uint8_t *saved_opcodes, const uint8_t *trap_opcodes, size_t opcode_size) :
177 NativeBreakpoint (addr),
181 m_opcode_size (opcode_size)
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");
186 ::memcpy (m_saved_opcodes, saved_opcodes, opcode_size);
187 ::memcpy (m_trap_opcodes, trap_opcodes, opcode_size);
191 SoftwareBreakpoint::DoEnable ()
193 return EnableSoftwareBreakpoint (m_process, m_addr, m_opcode_size, m_trap_opcodes, m_saved_opcodes);
197 SoftwareBreakpoint::DoDisable ()
200 assert (m_addr && (m_addr != LLDB_INVALID_ADDRESS) && "can't remove a software breakpoint for an invalid address");
202 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
204 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64, __FUNCTION__, m_addr);
206 assert ( (m_opcode_size > 0) && "cannot restore opcodes when there are no opcodes");
208 if (m_opcode_size > 0)
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));
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)))
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);
222 if (error.Success ())
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)
228 break_op_found = true;
229 // We found a valid breakpoint opcode at this address, now restore
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)))
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);
237 if (error.Success ())
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
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
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)))
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);
261 if (error.Success ())
263 // compare the memory we just read with the original opcode
264 if (::memcmp (m_saved_opcodes, verify_opcode, m_opcode_size) == 0)
268 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- SUCCESS", __FUNCTION__, m_addr);
274 error.SetErrorString("Failed to restore original opcode.");
278 error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored.");
283 if (log && error.Fail ())
284 log->Printf ("SoftwareBreakpoint::%s addr = 0x%" PRIx64 " -- FAILED: %s",
292 SoftwareBreakpoint::IsSoftwareBreakpoint () const