1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/*
* debugserver.cc: handle usecode debugging messages from client
*
* Copyright (C) 2002-2022 The Exult Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#if (defined(USE_EXULTSTUDIO) && defined(USECODE_DEBUGGER))
# include "debugmsg.h"
# include "debugserver.h"
# include "endianio.h"
# include "gamewin.h"
# include "servemsg.h"
# include "server.h"
# include "stackframe.h"
# include "ucdebugging.h"
# include "ucfunction.h"
# include "ucinternal.h"
# include "ucserial.h"
# include "useval.h"
# include <algorithm>
# include <iomanip>
# include <iostream>
# include <sstream>
using std::ios;
using std::stringstream;
// message handler that only handles debugging messages.
void Handle_client_debug_message(int& fd) {
unsigned char data[Exult_server::maxlength];
Exult_server::Msg_type id;
const int datalen = Exult_server::Receive_data(fd, id, data, sizeof(data));
if (datalen < 0) {
return;
}
switch (id) {
case Exult_server::usecode_debugging:
Handle_debug_message(&data[0], datalen);
break;
default:
// maybe reply something like "unable to handle message" ?
break;
}
}
void Handle_debug_message(unsigned char* data, int datalen) {
ignore_unused_variable_warning(datalen);
Usecode_machine* ucm = Game_window::get_instance()->get_usecode();
auto* uci = dynamic_cast<Usecode_internal*>(ucm);
if (uci == nullptr) {
return; // huh?
}
auto id = static_cast<Exult_server::Debug_msg_type>(data[0]);
const unsigned char* ptr = data;
if (uci->is_on_breakpoint()) {
// accept extra messages now
switch (id) {
case Exult_server::dbg_continue:
uci->set_breakpoint_action(0);
break;
case Exult_server::dbg_stepinto:
uci->set_breakpoint_action(0);
uci->set_breakpoint();
break;
case Exult_server::dbg_stepover:
uci->set_breakpoint_action(0);
uci->dbg_stepover();
break;
case Exult_server::dbg_finish:
uci->set_breakpoint_action(0);
uci->dbg_finish();
break;
case Exult_server::dbg_get_callstack: {
unsigned char d[3];
d[0] = static_cast<unsigned char>(Exult_server::dbg_callstack);
unsigned char* ptr = &d[1];
const int callstacksize = uci->get_callstack_size();
little_endian::Write2(ptr, callstacksize);
Exult_server::Send_data(
client_socket, Exult_server::usecode_debugging, d, 3);
for (int i = 0; i < callstacksize; i++) {
Stack_frame* frame = uci->get_stackframe(i);
Stack_frame_out(
client_socket, frame->function->id,
static_cast<int>(frame->ip - frame->code),
frame->call_chain, frame->call_depth, frame->eventid,
reinterpret_cast<uintptr>(frame->caller_item.get()),
frame->num_args, frame->num_vars, frame->locals);
}
break;
}
case Exult_server::dbg_get_stack: {
const Usecode_value zeroval(0);
stringstream dataio(ios::in | ios::out | ios::binary);
OStreamDataSource ds(&dataio);
ds.write1(static_cast<unsigned char>(Exult_server::dbg_stack));
const int stacksize = uci->get_stack_size();
ds.write2(stacksize);
for (int i = 0; i < stacksize; i++) {
Usecode_value* val = uci->peek_stack(i);
if (val && !val->save(&ds)) {
std::cerr
<< "Error: Could not save usecode value to message!"
<< std::endl;
}
if (ds.getSize() >= Exult_server::maxlength) {
break;
}
}
if (ds.getSize() > Exult_server::maxlength) {
std::cerr << "Error: stack larger than max. message!"
<< std::endl;
}
std::string data(dataio.str());
const auto* dptr
= reinterpret_cast<const unsigned char*>(data.data());
const int datalen = std::min(
static_cast<int>(data.size()), Exult_server::maxlength);
Exult_server::Send_data(
client_socket, Exult_server::usecode_debugging, dptr,
datalen);
break;
}
default:
break;
}
}
// these messages can always be handled
switch (id) {
case Exult_server::dbg_break:
uci->set_breakpoint();
break;
case Exult_server::dbg_get_status: {
unsigned char c;
if (uci->is_on_breakpoint()) {
c = static_cast<unsigned char>(Exult_server::dbg_on_breakpoint);
} else {
c = static_cast<unsigned char>(Exult_server::dbg_continuing);
}
Exult_server::Send_data(
client_socket, Exult_server::usecode_debugging, &c, 1);
break;
}
case Exult_server::dbg_set_location_bp: {
ptr++;
const int funcid = little_endian::Read4(ptr);
const int ip = little_endian::Read4(ptr);
std::cout << "Setting breakpoint at " << std::hex << std::setfill('0')
<< std::setw(4) << funcid << ", " << std::setw(4) << ip
<< std::setfill(' ') << std::dec << std::endl;
// +++++ check for duplicates?
const int breakpoint_id = uci->set_location_breakpoint(funcid, ip);
unsigned char d[13];
d[0] = static_cast<unsigned char>(Exult_server::dbg_set_location_bp);
unsigned char* dptr = &d[1];
little_endian::Write4(dptr, funcid);
little_endian::Write4(dptr, ip);
little_endian::Write4(dptr, breakpoint_id);
Exult_server::Send_data(
client_socket, Exult_server::usecode_debugging, d, 13);
break;
}
case Exult_server::dbg_clear_breakpoint: {
ptr++;
const int breakpoint_id = little_endian::Read4(ptr);
const bool ok = uci->clear_breakpoint(breakpoint_id);
if (ok) {
// reply
Exult_server::Send_data(
client_socket, Exult_server::usecode_debugging, data, 5);
}
break;
}
case Exult_server::dbg_get_breakpoints: {
uci->transmit_breakpoints(client_socket);
break;
}
default:
break;
}
}
#endif
|