add unfinished bmpImage implementation
[openc2e.git] / caosVM_debug.cpp
blob703e6d43bafa8fda9cb99717562499224666cd9a
1 /*
2 * caosVM_debug.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Sun Oct 24 2004.
6 * Copyright (c) 2004 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "caosVM.h"
21 #include "openc2e.h"
22 #include "Agent.h"
23 #include "World.h"
24 #include <iostream>
25 #include "cmddata.h"
26 #include <cctype>
27 #include "dialect.h"
28 #include <algorithm>
29 #include "caosScript.h"
31 // #include "malloc.h" <- unportable horror!
32 #include <sstream>
33 #include <boost/format.hpp>
35 using std::cerr;
36 using std::cout;
38 /**
39 DBG: OUTS (command) val (string)
40 %status maybe
41 %pragma variants all
43 Outputs a string to the debug log.
45 void caosVM::c_DBG_OUTS() {
46 VM_PARAM_STRING(val)
48 cout << val << std::endl;
51 /**
52 DBGM (command) val (bareword)
53 %status maybe
54 %pragma variants c1 c2
55 %pragma implementation caosVM::c_DBG_OUTS
58 /**
59 DBG: OUTV (command) val (decimal)
60 %status maybe
61 %pragma variants all
63 Outputs a decimal value to the debug log.
65 void caosVM::c_DBG_OUTV() {
66 VM_VERIFY_SIZE(1)
67 VM_PARAM_VALUE(val)
69 if (val.hasFloat()) {
70 cout << boost::format("%0.06f") % val.getFloat();
71 } else if (val.hasInt()) {
72 cout << val.getInt();
73 } else if (val.hasVector()) {
74 const Vector<float> &v = val.getVector();
75 cout << boost::format("(%0.6f, %0.6f)") % v.x % v.y;
76 } else throw badParamException();
78 cout << std::endl;
81 /**
82 DBGV (command) val (integer)
83 %status maybe
84 %pragma variants c1 c2
85 %pragma implementation caosVM::c_DBG_OUTV
88 /**
89 DBUG (command) val (integer)
90 %status maybe
91 %pragma variants c1 c2
93 void caosVM::c_DBUG() {
94 inst = true;
95 c_DBG_OUTV();
98 /**
99 UNID (integer)
100 %status maybe
101 %pragma variants c3 cv sm
103 Returns the unique ID of the target agent.
104 This is currently no good for persisting.
106 XXX: when serialization support works, this might well become good for
107 persisting :)
109 void caosVM::v_UNID() {
110 VM_VERIFY_SIZE(0)
111 valid_agent(targ);
112 result.setInt(targ->getUNID());
116 UNID (agent)
117 %status maybe
118 %pragma variants c2
119 %pragma implementation caosVM::v_UNID_c2
121 Returns the unique ID of the target agent.
122 This is currently no good for persisting.
124 void caosVM::v_UNID_c2() {
125 VM_VERIFY_SIZE(0)
126 valid_agent(targ);
127 result.setAgent(targ);
131 AGNT (agent) id (integer)
132 %status maybe
134 Returns the agent with the given UNID, or NULL if agent has been deleted.
136 void caosVM::v_AGNT() {
137 VM_VERIFY_SIZE(1)
138 VM_PARAM_INTEGER(id)
140 result.setAgent(world.lookupUNID(id));
144 DBG: MALLOC (command)
145 %status stub
147 Dumps some random memory stats to stderr.
149 void caosVM::c_DBG_MALLOC() {
150 VM_VERIFY_SIZE(0)
152 // more unportable horror!
153 /* struct mallinfo mi = mallinfo();
154 #define MPRINT(name) \
155 fprintf(stderr, "%10s = %d\n", #name, mi. name)
156 MPRINT(arena);
157 MPRINT(ordblks);
158 MPRINT(smblks);
159 MPRINT(hblks);
160 MPRINT(hblkhd);
161 MPRINT(usmblks);
162 MPRINT(fsmblks);
163 MPRINT(uordblks);
164 MPRINT(fordblks);
165 MPRINT(keepcost);
166 malloc_stats(); */
168 /*std::cerr << "caosSlab free=" << caosVarSlab.free_elements() <<
169 " used=" << caosVarSlab.used_elements() <<
170 " total=" << caosVarSlab.total_elements() <<
171 std::endl;*/
175 DBG: DUMP (command)
176 %status ok
177 %pragma variants all
179 Dumps the current script's bytecode to stderr.
181 void caosVM::c_DBG_DUMP() {
182 std::cerr << vm->currentscript->dump();
186 DBG: TRACE (command) level (integer)
187 %status ok
188 %pragma variants all
190 Sets opcode trace level. Zero disables.
192 void caosVM::c_DBG_TRACE() {
193 VM_PARAM_INTEGER(en)
195 std::cerr << "trace: " << en << std::endl;
196 vm->trace = en;
197 if (vm->trace < 0)
198 vm->trace = 0;
202 MANN (command) cmd (string)
203 %status stub
205 Looks up documentation on the given command and spits it on the current output stream.
207 void caosVM::c_MANN() {
208 VM_PARAM_STRING(cmd)
210 caos_assert(outputstream);
212 std::transform(cmd.begin(), cmd.end(), cmd.begin(), toupper);
213 const cmdinfo *i = currentscript->dialect->cmdbase();
215 bool found = false;
216 while (i->lookup_key) {
217 // TODO: this doesn't work for FACE at the moment due to hack elsewhere
218 if (cmd == i->fullname) {
219 found = true;
220 std::string d = i->docs;
221 // TODO: docs should always include name/parameters/etc, so should never be empty
222 if (d.size())
223 *outputstream << std::string(i->docs) << std::endl;
224 else
225 *outputstream << "no documentation for " << cmd << std::endl << std::endl;
228 i++;
231 if (!found) {
232 *outputstream << "didn't find " << cmd << std::endl;
233 return;
238 DBG: DISA (command) family (integer) genus (integer) species (integer) event (integer)
239 %pragma variants all
240 %status ok
242 Dumps the "bytecode" of the indicated script to the current output channel.
243 Note that this isn't really bytecode yet (though that's a possible future
244 improvement).
246 If the script is not found no output will be generated.
248 void caosVM::c_DBG_DISA() {
249 VM_PARAM_INTEGER(event)
250 VM_PARAM_INTEGER(species)
251 VM_PARAM_INTEGER(genus)
252 VM_PARAM_INTEGER(family)
254 caos_assert(outputstream);
256 shared_ptr<script> s = world.scriptorium.getScript(family, genus, species, event);
257 if (s) {
258 if (s->fmly != family || s->gnus != genus || s->spcs != species) {
259 *outputstream << "warning: search resulted in script from " << s->fmly << ", " << s->gnus << ", " << s->spcs << " script" << std::endl;
261 *outputstream << s->dump();
262 } else
263 *outputstream << "no such script" << std::endl;
267 DBG: ASRT (command) condition (condition)
268 %pragma parser new AssertParser()
269 %pragma variants all
270 %status maybe
272 Blows up unless the given condition is true.
274 void caosVM::c_DBG_ASRT() {
275 throw caosException("DBG: ASRT condition failed");
279 DBG: IDNT (string) agent (agent)
280 %status ok
281 %pragma variants all
283 (openc2e-only)
284 Return a nicely-formatted string identifying the classifier of the agent,
285 using the catalogue to find the name if possible.
287 void caosVM::v_DBG_IDNT() {
288 VM_PARAM_AGENT(a)
290 if (!a)
291 result.setString("(null)");
292 else
293 result.setString(a->identify());
297 DBG: PROF (command)
298 %status stub
300 Dumps the current agent profiling information to the output stream, in CSV format.
302 void caosVM::c_DBG_PROF() {
303 // TODO
307 DBG: CPRO (command)
308 %status stub
310 Clears the current agent profiling information.
312 void caosVM::c_DBG_CPRO() {
313 // TODO
317 DBG: STOK (string) bareword (bareword)
318 %status ok
319 %pragma variants all
321 Returns the bare token in 'bareword' as a string.
323 void caosVM::v_DBG_STOK() {
324 VM_PARAM_STRING(bareword)
326 result.setString(bareword);
330 DBG: TSLC (command) timeslice (integer)
331 %status ok
332 %pragma variants all
333 %cost 0
335 Sets the currently executing script's remaining timeslice value. This command
336 affects only the current timeslice; future slices use the normal amount for
337 the dialect in question.
339 void caosVM::c_DBG_TSLC() {
340 VM_PARAM_INTEGER(tslc);
341 timeslice = tslc;
345 DBG: TSLC (integer)
346 %status ok
347 %pragma variants all
349 Returns the number of ticks left in the current script's remaining timeslice.
351 void caosVM::v_DBG_TSLC() {
352 result.setInt(timeslice);
356 /* vim: set noet: */