1 /* 2 * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <DebugContext.h> 8 9 10 BDebugContext::BDebugContext() 11 { 12 fContext.team = -1; 13 } 14 15 16 BDebugContext::~BDebugContext() 17 { 18 Uninit(); 19 } 20 21 22 status_t 23 BDebugContext::Init(team_id team, port_id nubPort) 24 { 25 Uninit(); 26 27 status_t error = init_debug_context(&fContext, team, nubPort); 28 if (error != B_OK) { 29 fContext.team = -1; 30 return error; 31 } 32 33 return B_OK; 34 } 35 36 37 void 38 BDebugContext::Uninit() 39 { 40 if (fContext.team >= 0) { 41 destroy_debug_context(&fContext); 42 fContext.team = -1; 43 } 44 } 45 46 47 status_t 48 BDebugContext::SendDebugMessage(int32 messageCode, const void *message, 49 size_t messageSize, void* reply, size_t replySize) 50 { 51 return send_debug_message(&fContext, messageCode, message, messageSize, 52 reply, replySize); 53 } 54 55 56 status_t 57 BDebugContext::SetTeamDebuggingFlags(int32 flags) 58 { 59 debug_nub_set_team_flags message; 60 message.flags = flags; 61 62 return SendDebugMessage(B_DEBUG_MESSAGE_SET_TEAM_FLAGS, &message, 63 sizeof(message), NULL, 0); 64 } 65 66 67 ssize_t 68 BDebugContext::ReadMemoryPartial(const void* address, void* buffer, size_t size) 69 { 70 return debug_read_memory_partial(&fContext, address, buffer, size); 71 } 72 73 74 ssize_t 75 BDebugContext::ReadMemory(const void* address, void* buffer, size_t size) 76 { 77 return debug_read_memory(&fContext, address, buffer, size); 78 } 79 80 81 ssize_t 82 BDebugContext::ReadString(const void* address, char* buffer, size_t size) 83 { 84 return debug_read_string(&fContext, address, buffer, size); 85 } 86 87 88 status_t 89 BDebugContext::SetBreakpoint(void* address) 90 { 91 debug_nub_set_breakpoint message; 92 message.reply_port = fContext.reply_port; 93 message.address = address; 94 95 debug_nub_set_breakpoint_reply reply; 96 status_t error = SendDebugMessage(B_DEBUG_MESSAGE_SET_BREAKPOINT, &message, 97 sizeof(message), &reply, sizeof(reply)); 98 99 return error == B_OK ? reply.error : error; 100 } 101 102 103 status_t 104 BDebugContext::ClearBreakpoint(void* address) 105 { 106 debug_nub_clear_breakpoint message; 107 message.address = address; 108 109 return SendDebugMessage(B_DEBUG_MESSAGE_CLEAR_BREAKPOINT, &message, 110 sizeof(message), NULL, 0); 111 } 112 113 114 status_t 115 BDebugContext::SetWatchpoint(void* address, uint32 type, int32 length) 116 { 117 debug_nub_set_watchpoint message; 118 message.reply_port = fContext.reply_port; 119 message.address = address; 120 message.type = type; 121 message.length = length; 122 123 debug_nub_set_watchpoint_reply reply; 124 status_t error = SendDebugMessage(B_DEBUG_MESSAGE_SET_WATCHPOINT, &message, 125 sizeof(message), &reply, sizeof(reply)); 126 127 return error == B_OK ? reply.error : error; 128 } 129 130 131 status_t 132 BDebugContext::ClearWatchpoint(void* address) 133 { 134 debug_nub_clear_watchpoint message; 135 message.address = address; 136 137 return SendDebugMessage(B_DEBUG_MESSAGE_CLEAR_WATCHPOINT, &message, 138 sizeof(message), NULL, 0); 139 } 140 141 142 status_t 143 BDebugContext::ContinueThread(thread_id thread, bool singleStep) 144 { 145 debug_nub_continue_thread message; 146 message.thread = thread; 147 message.handle_event = B_THREAD_DEBUG_HANDLE_EVENT; 148 message.single_step = singleStep; 149 150 return SendDebugMessage(B_DEBUG_MESSAGE_CONTINUE_THREAD, &message, 151 sizeof(message), NULL, 0); 152 } 153 154 155 status_t 156 BDebugContext::SetThreadDebuggingFlags(thread_id thread, int32 flags) 157 { 158 debug_nub_set_thread_flags message; 159 message.thread = thread; 160 message.flags = flags; 161 162 return SendDebugMessage(B_DEBUG_MESSAGE_SET_THREAD_FLAGS, &message, 163 sizeof(message), NULL, 0); 164 } 165 166 167 status_t 168 BDebugContext::GetThreadCpuState(thread_id thread, 169 debug_debugger_message* _messageCode, debug_cpu_state* cpuState) 170 { 171 return debug_get_cpu_state(&fContext, thread, _messageCode, cpuState); 172 } 173