File: ucdebugging.cc

package info (click to toggle)
exult 1.2-4
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 9,040 kB
  • ctags: 10,543
  • sloc: cpp: 99,373; sh: 8,333; ansic: 4,659; makefile: 988; yacc: 769; lex: 313; xml: 19
file content (190 lines) | stat: -rw-r--r-- 4,395 bytes parent folder | download | duplicates (8)
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
/*
 *	ucdebugging.cc - Debugging-related functions for usecode
 *
 *  Copyright (C) 2002  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 "ucinternal.h"
#include "stackframe.h"
#include "ucfunction.h"
#include "utils.h"

#include "servemsg.h"
#include "debugmsg.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) &&
		((int)(frame->ip - frame->code) == ip);
}

void LocationBreakpoint::serialize(int fd) const
{
	unsigned char d[13];
	d[0] = (unsigned char)(Exult_server::dbg_set_location_bp);
	unsigned char *dptr = &d[1];
	Write4(dptr, functionid);
	Write4(dptr, ip);
	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()
{
}

Breakpoints::~Breakpoints()
{
	std::list<Breakpoint*>::iterator iter;

	// clear queue
	for (iter = breaks.begin(); iter != breaks.end(); ++iter)
	{
		delete (*iter);
	}
}


// 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;

	std::list<Breakpoint*>::iterator iter;

	for (iter = breaks.begin(); iter != breaks.end(); ++iter)
	{
		if ((*iter)->check(frame)) {
			breakID = (*iter)->id;
			if ((*iter)->once) {
				delete (*iter);
				(*iter) = 0;
			}
		}
	}

	breaks.remove((Breakpoint*)0); // delete all NULLs 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;

	std::list<Breakpoint*>::iterator iter;

	for (iter = breaks.begin(); iter != breaks.end(); ++iter)
	{
		if ((*iter)->id == id) {
			found = true;
			delete (*iter);
			(*iter) = 0;
		}
	}

	breaks.remove((Breakpoint*)0); // delete all NULLs from the list

	return found;
}

void Breakpoints::transmit(int fd)
{
	std::list<Breakpoint*>::iterator iter;

	for (iter = breaks.begin(); iter != breaks.end(); ++iter)
		(*iter)->serialize(fd);
}