File: ucdebugging.cc

package info (click to toggle)
exult 1.12.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,608 kB
  • sloc: cpp: 169,917; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 539; objc: 416
file content (150 lines) | stat: -rw-r--r-- 4,097 bytes parent folder | download | duplicates (2)
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
/*
 *  ucdebugging.cc - Debugging-related functions for usecode
 *
 *  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

#include "ucdebugging.h"

#include "debugmsg.h"
#include "servemsg.h"
#include "stackframe.h"
#include "ucfunction.h"
#include "ucinternal.h"

Breakpoint::Breakpoint(bool once) {
	this->once = once;
	id         = Breakpoints::getNewID();
}

AnywhereBreakpoint::AnywhereBreakpoint() : Breakpoint(true) {}

LocationBreakpoint::LocationBreakpoint(int functionid, int ip, bool once)
		: Breakpoint(once) {
	this->functionid = functionid;
	this->ip         = ip;
}

bool LocationBreakpoint::check(Stack_frame* frame) const {
	return (frame->function->id == functionid)
		   && (static_cast<int>(frame->ip - frame->code) == ip);
}

void LocationBreakpoint::serialize(int fd) const {
	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, functionid);
	little_endian::Write4(dptr, ip);
	little_endian::Write4(dptr, id);
	Exult_server::Send_data(fd, Exult_server::usecode_debugging, d, 13);
}

StepoverBreakpoint::StepoverBreakpoint(Stack_frame* frame) : Breakpoint(true) {
	call_chain = frame->call_chain;
	call_depth = frame->call_depth;
}

bool StepoverBreakpoint::check(Stack_frame* frame) const {
	return (frame->call_chain == call_chain && frame->call_depth <= call_depth)
		   || (frame->call_chain < call_chain);
}

FinishBreakpoint::FinishBreakpoint(Stack_frame* frame) : Breakpoint(true) {
	call_chain = frame->call_chain;
	call_depth = frame->call_depth;
}

bool FinishBreakpoint::check(Stack_frame* frame) const {
	return (frame->call_chain == call_chain && frame->call_depth < call_depth)
		   || (frame->call_chain < call_chain);
}

int Breakpoints::lastID = 0;

Breakpoints::~Breakpoints() {
	// clear queue
	for (auto* brk : breaks) {
		delete brk;
	}
}

// returns ID of a (any) breakpoint encountered, or -1 if no breakpoints
int Breakpoints::check(Stack_frame* frame) {
	// check all breakpoints (always check all of them, even if match found)
	// and delete the matching ones with 'once' set

	// question: do we really want to delete a breakpoint (with once set)
	// as soon as it is triggered? (or wait a while until it has been
	// processed properly?)
	// also, it might be necessary to return a list of all matching and/or
	// deleted breakpoints
	// OTOH, which 'once' breakpoint is hit is generally not interesting,
	// and also, only one of these will be set, usually.

	int breakID = -1;

	for (auto*& brk : breaks) {
		if (brk->check(frame)) {
			breakID = brk->id;
			if (brk->once) {
				delete brk;
				brk = nullptr;
			}
		}
	}

	breaks.remove(nullptr);    // delete all nullptr from the list

	return breakID;
}

void Breakpoints::add(Breakpoint* breakpoint) {
	breaks.remove(breakpoint);    // avoid double occurences

	breaks.push_back(breakpoint);
}

void Breakpoints::remove(Breakpoint* breakpoint) {
	breaks.remove(breakpoint);
}

bool Breakpoints::remove(int id) {
	bool found = false;

	for (auto*& brk : breaks) {
		if (brk->id == id) {
			found = true;
			delete brk;
			brk = nullptr;
		}
	}

	breaks.remove(nullptr);    // delete all nullptr from the list

	return found;
}

void Breakpoints::transmit(int fd) {
	for (auto* brk : breaks) {
		brk->serialize(fd);
	}
}