/[gxemul]/trunk/src/debugger/debugger.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/src/debugger/debugger.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations)
Mon Oct 8 16:20:58 2007 UTC (11 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 19650 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1421 2006/11/06 05:32:37 debug Exp $
20060816	Adding a framework for emulated/virtual timers (src/timer.c),
		using only setitimer().
		Rewriting the mc146818 to use the new timer framework.
20060817	Adding a call to gettimeofday() every now and then (once every
		second, at the moment) to resynch the timer if it drifts.
		Beginning to convert the ISA timer interrupt mechanism (8253
		and 8259) to use the new timer framework.
		Removing the -I command line option.
20060819	Adding the -I command line option again, with new semantics.
		Working on Footbridge timer interrupts; NetBSD/NetWinder and
		NetBSD/CATS now run at correct speed, but unfortunately with
		HUGE delays during bootup.
20060821	Some minor m68k updates. Adding the first instruction: nop. :)
		Minor Alpha emulation updates.
20060822	Adding a FreeBSD development specific YAMON environment
		variable ("khz") (as suggested by Bruce M. Simpson).
		Moving YAMON environment variable initialization from
		machine_evbmips.c into promemul/yamon.c, and adding some more
		variables.
		Continuing on the LCA PCI bus controller (for Alpha machines).
20060823	Continuing on the timer stuff: experimenting with MIPS count/
		compare interrupts connected to the timer framework.
20060825	Adding bogus SCSI commands 0x51 (SCSICDROM_READ_DISCINFO) and
		0x52 (SCSICDROM_READ_TRACKINFO) to the SCSI emulation layer,
		to allow NetBSD/pmax 4.0_BETA to be installed from CDROM.
		Minor updates to the LCA PCI controller.
20060827	Implementing a CHIP8 cpu mode, and a corresponding CHIP8
		machine, for fun. Disassembly support for all instructions,
		and most of the common instructions have been implemented: mvi,
		mov_imm, add_imm, jmp, rand, cls, sprite, skeq_imm, jsr,
		skne_imm, bcd, rts, ldr, str, mov, or, and, xor, add, sub,
		font, ssound, sdelay, gdelay, bogus skup/skpr, skeq, skne.
20060828	Beginning to convert the CHIP8 cpu in the CHIP8 machine to a
		(more correct) RCA 180x cpu. (Disassembly for all 1802
		instructions has been implemented, but no execution yet, and
		no 1805 extended instructions.)
20060829	Minor Alpha emulation updates.
20060830	Beginning to experiment a little with PCI IDE for SGI O2.
		Fixing the cursor key mappings for MobilePro 770 emulation.
		Fixing the LK201 warning caused by recent NetBSD/pmax.
		The MIPS R41xx standby, suspend, and hibernate instructions now
		behave like the RM52xx/MIPS32/MIPS64 wait instruction.
		Fixing dev_wdc so it calculates correct (64-bit) offsets before
		giving them to diskimage_access().
20060831	Continuing on Alpha emulation (OSF1 PALcode).
20060901	Minor Alpha updates; beginning on virtual memory pagetables.
		Removed the limit for max nr of devices (in preparation for
		allowing devices' base addresses to be changed during runtime).
		Adding a hack for MIPS [d]mfc0 select 0 (except the count
		register), so that the coproc register is simply copied.
		The MIPS suspend instruction now exits the emulator, instead
		of being treated as a wait instruction (this causes NetBSD/
		hpcmips to get correct 'halt' behavior).
		The VR41xx RTC now returns correct time.
		Connecting the VR41xx timer to the timer framework (fixed at
		128 Hz, for now).
		Continuing on SPARC emulation, adding more instructions:
		restore, ba_xcc, ble. The rectangle drawing demo works :)
		Removing the last traces of the old ENABLE_CACHE_EMULATION
		MIPS stuff (not usable with dyntrans anyway).
20060902	Splitting up src/net.c into several smaller files in its own
		subdirectory (src/net/).
20060903	Cleanup of the files in src/net/, to make them less ugly.
20060904	Continuing on the 'settings' subsystem.
		Minor progress on the SPARC emulation mode.
20060905	Cleanup of various things, and connecting the settings
		infrastructure to various subsystems (emul, machine, cpu, etc).
		Changing the lk201 mouse update routine to not rely on any
		emulated hardware framebuffer cursor coordinates, but instead
		always do (semi-usable) relative movements.
20060906	Continuing on the lk201 mouse stuff. Mouse behaviour with
		multiple framebuffers (which was working in Ultrix) is now
		semi-broken (but it still works, in a way).
		Moving the documentation about networking into its own file
		(networking.html), and refreshing it a bit. Adding an example
		of how to use ethernet frame direct-access (udp_snoop).
20060907	Continuing on the settings infrastructure.
20060908	Minor updates to SH emulation: for 32-bit emulation: delay
		slots and the 'jsr @Rn' instruction. I'm putting 64-bit SH5 on
		ice, for now.
20060909-10	Implementing some more 32-bit SH instructions. Removing the
		64-bit mode completely. Enough has now been implemented to run
		the rectangle drawing demo. :-)
20060912	Adding more SH instructions.
20060916	Continuing on SH emulation (some more instructions: div0u,
		div1, rotcl/rotcr, more mov instructions, dt, braf, sets, sett,
		tst_imm, dmuls.l, subc, ldc_rm_vbr, movt, clrt, clrs, clrmac).
		Continuing on the settings subsystem (beginning on reading/
		writing settings, removing bugs, and connecting more cpus to
		the framework).
20060919	More work on SH emulation; adding an ldc banked instruction,
		and attaching a 640x480 framebuffer to the Dreamcast machine
		mode (NetBSD/dreamcast prints the NetBSD copyright banner :-),
		and then panics).
20060920	Continuing on the settings subsystem.
20060921	Fixing the Footbridge timer stuff so that NetBSD/cats and
		NetBSD/netwinder boot up without the delays.
20060922	Temporarily hardcoding MIPS timer interrupt to 100 Hz. With
		'wait' support disabled, NetBSD/malta and Linux/malta run at
		correct speed.
20060923	Connecting dev_gt to the timer framework, so that NetBSD/cobalt
		runs at correct speed.
		Moving SH4-specific memory mapped registers into its own
		device (dev_sh4.c).
		Running with -N now prints "idling" instead of bogus nr of
		instrs/second (which isn't valid anyway) while idling.
20060924	Algor emulation should now run at correct speed.
		Adding disassembly support for some MIPS64 revision 2
		instructions: ext, dext, dextm, dextu.
20060926	The timer framework now works also when the MIPS wait
		instruction is used.
20060928	Re-implementing checks for coprocessor availability for MIPS
		cop0 instructions. (Thanks to Carl van Schaik for noticing the
		lack of cop0 availability checks.)
20060929	Implementing an instruction combination hack which treats
		NetBSD/pmax' idle loop as a wait-like instruction.
20060930	The ENTRYHI_R_MASK was missing in (at least) memory_mips_v2p.c,
		causing TLB lookups to sometimes succeed when they should have
		failed. (A big thank you to Juli Mallett for noticing the
		problem.)
		Adding disassembly support for more MIPS64 revision 2 opcodes
		(seb, seh, wsbh, jalr.hb, jr.hb, synci, ins, dins, dinsu,
		dinsm, dsbh, dshd, ror, dror, rorv, drorv, dror32). Also
		implementing seb, seh, dsbh, dshd, and wsbh.
		Implementing an instruction combination hack for Linux/pmax'
		idle loop, similar to the NetBSD/pmax case.
20061001	Changing the NetBSD/sgimips install instructions to extract
		files from an iso image, instead of downloading them via ftp.
20061002	More-than-31-bit userland addresses in memory_mips_v2p.c were
		not actually working; applying a fix from Carl van Schaik to
		enable them to work + making some other updates (adding kuseg
		support).
		Fixing hpcmips (vr41xx) timer initialization.
		Experimenting with O(n)->O(1) reduction in the MIPS TLB lookup
		loop. Seems to work both for R3000 and non-R3000.
20061003	Continuing a little on SH emulation (adding more control
		registers; mini-cleanup of memory_sh.c).
20061004	Beginning on a dev_rtc, a clock/timer device for the test
		machines; also adding a demo, and some documentation.
		Fixing a bug in SH "mov.w @(disp,pc),Rn" (the result wasn't
		sign-extended), and adding the addc and ldtlb instructions.
20061005	Contining on SH emulation: virtual to physical address
		translation, and a skeleton exception mechanism.
20061006	Adding more SH instructions (various loads and stores, rte,
		negc, muls.w, various privileged register-move instructions).
20061007	More SH instructions: various move instructions, trapa, div0s,
		float, fdiv, ftrc.
		Continuing on dev_rtc; removing the rtc demo.
20061008	Adding a dummy Dreamcast PROM module. (Homebrew Dreamcast
		programs using KOS libs need this.)
		Adding more SH instructions: "stc vbr,rn", rotl, rotr, fsca,
		fmul, fadd, various floating-point moves, etc. A 256-byte
		demo for Dreamcast runs :-)
20061012	Adding the SH "lds Rm,pr" and bsr instructions.
20061013	More SH instructions: "sts fpscr,rn", tas.b, and some more
		floating point instructions, cmp/str, and more moves.
		Adding a dummy dev_pvr (Dreamcast graphics controller).
20061014	Generalizing the expression evaluator (used in the built-in
		debugger) to support parentheses and +-*/%^&|.
20061015	Removing the experimental tlb index hint code in
		mips_memory_v2p.c, since it didn't really have any effect.
20061017	Minor SH updates; adding the "sts pr,Rn", fcmp/gt, fneg,
		frchg, and some other instructions. Fixing missing sign-
		extension in an 8-bit load instruction.
20061019	Adding a simple dev_dreamcast_rtc.
		Implementing memory-mapped access to the SH ITLB/UTLB arrays.
20061021	Continuing on various SH and Dreamcast things: sh4 timers,
		debug messages for dev_pvr, fixing some virtual address
		translation bugs, adding the bsrf instruction.
		The NetBSD/dreamcast GENERIC_MD kernel now reaches userland :)
		Adding a dummy dev_dreamcast_asic.c (not really useful yet).
		Implementing simple support for Store Queues.
		Beginning on the PVR Tile Accelerator.
20061022	Generalizing the PVR framebuffer to support off-screen drawing,
		multiple bit-depths, etc. (A small speed penalty, but most
		likely worth it.)
		Adding more SH instructions (mulu.w, fcmp/eq, fsub, fmac,
		fschg, and some more); correcting bugs in "fsca" and "float".
20061024	Adding the SH ftrv (matrix * vector) instruction. Marcus
		Comstedt's "tatest" example runs :) (wireframe only).
		Correcting disassembly for SH floating point instructions that
		use the xd* registers.
		Adding the SH fsts instruction.
		In memory_device_dyntrans_access(), only the currently used
		range is now invalidated, and not the entire device range.
20061025	Adding a dummy AVR32 cpu mode skeleton.
20061026	Various Dreamcast updates; beginning on a Maple bus controller.
20061027	Continuing on the Maple bus. A bogus Controller, Keyboard, and
		Mouse can now be detected by NetBSD and KOS homebrew programs.
		Cleaning up the SH4 Timer Management Unit, and beginning on
		SH4 interrupts.
		Implementing the Dreamcast SYSASIC.
20061028	Continuing on the SYSASIC.
		Adding the SH fsqrt instruction.
		memory_sh.c now actually scans the ITLB.
		Fixing a bug in dev_sh4.c, related to associative writes into
		the memory-mapped UTLB array. NetBSD/dreamcast now reaches
		userland stably, and prints the "Terminal type?" message :-]
		Implementing enough of the Dreamcast keyboard to make NetBSD
		accept it for input.
		Enabling SuperH for stable (non-development) builds.
		Adding NetBSD/dreamcast to the documentation, although it
		doesn't support root-on-nfs yet.
20061029	Changing usleep(1) calls in the debugger to to usleep(10000)
		(according to Brian Foley, this makes GXemul run better on
		MacOS X).
		Making the Maple "Controller" do something (enough to barely
		interact with dcircus.elf).
20061030-31	Some progress on the PVR. More test programs start running (but
		with strange output).
		Various other SH4-related updates.
20061102	Various Dreamcast and SH4 updates; more KOS demos run now.
20061104	Adding a skeleton dev_mb8696x.c (the Dreamcast's LAN adapter).
20061105	Continuing on the MB8696x; NetBSD/dreamcast detects it as mbe0.
		Testing for the release.

==============  RELEASE 0.4.3  ==============


1 /*
2 * Copyright (C) 2004-2006 Anders Gavare. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *
28 * $Id: debugger.c,v 1.20 2006/10/29 05:17:21 debug Exp $
29 *
30 * Single-step debugger.
31 *
32 *
33 * TODO:
34 *
35 * This entire module is very much non-reentrant. :-/
36 *
37 * Add more functionality that already exists elsewhere in the emulator.
38 *
39 * Call stack display (back-trace)?
40 *
41 * Nicer looking output of register dumps, floating point registers,
42 * etc. Warn about weird/invalid register contents.
43 *
44 * Ctrl-C doesn't enter the debugger on some OSes (HP-UX?)...
45 *
46 * Many other TODOs.
47 */
48
49 #include <ctype.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "console.h"
57 #include "cpu.h"
58 #include "device.h"
59 #include "debugger.h"
60 #include "debugger_gdb.h"
61 #include "diskimage.h"
62 #include "emul.h"
63 #include "machine.h"
64 #include "memory.h"
65 #include "misc.h"
66 #include "net.h"
67 #include "settings.h"
68 #include "timer.h"
69 #include "x11.h"
70
71
72 extern int extra_argc;
73 extern char **extra_argv;
74 extern struct settings *global_settings;
75 extern int quiet_mode;
76
77
78 /*
79 * Global debugger variables:
80 *
81 * TODO: Some of these should be moved to some other place!
82 */
83
84 volatile int single_step = NOT_SINGLE_STEPPING;
85 volatile int exit_debugger;
86 int force_debugger_at_exit = 0;
87
88 volatile int single_step_breakpoint = 0;
89 int debugger_n_steps_left_before_interaction = 0;
90
91 int old_instruction_trace = 0;
92 int old_quiet_mode = 0;
93 int old_show_trace_tree = 0;
94
95
96 /*
97 * Private (global) debugger variables:
98 */
99
100 static volatile int ctrl_c;
101
102 static int debugger_n_emuls;
103 static struct emul **debugger_emuls;
104
105 /* Currently focused CPU, machine, and emulation: */
106 int debugger_cur_cpu;
107 int debugger_cur_machine;
108 int debugger_cur_emul;
109 static struct machine *debugger_machine;
110 static struct emul *debugger_emul;
111
112 #define MAX_CMD_BUFLEN 72
113 #define N_PREVIOUS_CMDS 150
114 static char *last_cmd[N_PREVIOUS_CMDS];
115 static int last_cmd_index;
116
117 static char repeat_cmd[MAX_CMD_BUFLEN];
118
119 #define MAGIC_UNTOUCHED 0x98ca76c2ffcc0011ULL
120
121 static uint64_t last_dump_addr = MAGIC_UNTOUCHED;
122 static uint64_t last_unasm_addr = MAGIC_UNTOUCHED;
123
124
125 /*
126 * debugger_readchar():
127 */
128 char debugger_readchar(void)
129 {
130 int ch, i, j;
131
132 while ((ch = console_readchar(MAIN_CONSOLE)) < 0 && !exit_debugger) {
133 /* Check for X11 events: */
134 x11_check_event(debugger_emuls, debugger_n_emuls);
135
136 /* Check for incoming GDB packets: */
137 for (i=0; i<debugger_n_emuls; i++) {
138 struct emul *e = debugger_emuls[i];
139 if (e == NULL)
140 continue;
141
142 for (j=0; j<e->n_machines; j++) {
143 if (e->machines[j]->gdb.port > 0)
144 debugger_gdb_check_incoming(
145 e->machines[j]);
146 }
147 }
148
149 /* TODO: The X11 and GDB checks above should probably
150 be factored out... */
151
152 /* Give up some CPU time: */
153 usleep(10000);
154 }
155 return ch;
156 }
157
158
159 /*
160 * debugger_activate():
161 *
162 * This is a signal handler for CTRL-C. It shouldn't be called directly,
163 * but setup code in emul.c sets the CTRL-C signal handler to use this
164 * function.
165 */
166 void debugger_activate(int x)
167 {
168 ctrl_c = 1;
169
170 if (single_step != NOT_SINGLE_STEPPING) {
171 /* Already in the debugger. Do nothing. */
172 int i;
173 for (i=0; i<MAX_CMD_BUFLEN; i++)
174 console_makeavail(MAIN_CONSOLE, '\b');
175 console_makeavail(MAIN_CONSOLE, ' ');
176 console_makeavail(MAIN_CONSOLE, '\n');
177 printf("^C");
178 fflush(stdout);
179 } else {
180 /* Enter the single step debugger. */
181 single_step = ENTER_SINGLE_STEPPING;
182
183 /* Discard any chars in the input queue: */
184 while (console_charavail(MAIN_CONSOLE))
185 console_readchar(MAIN_CONSOLE);
186 }
187
188 /* Clear the repeat-command buffer: */
189 repeat_cmd[0] = '\0';
190
191 /* Reactivate the signal handler: */
192 signal(SIGINT, debugger_activate);
193 }
194
195
196 /*
197 * show_breakpoint():
198 */
199 static void show_breakpoint(struct machine *m, int i)
200 {
201 printf("%3i: 0x", i);
202 if (m->cpus[0]->is_32bit)
203 printf("%08"PRIx32, (uint32_t) m->breakpoint_addr[i]);
204 else
205 printf("%016"PRIx64, (uint64_t) m->breakpoint_addr[i]);
206 if (m->breakpoint_string[i] != NULL)
207 printf(" (%s)", m->breakpoint_string[i]);
208 if (m->breakpoint_flags[i])
209 printf(": flags=0x%x", m->breakpoint_flags[i]);
210 printf("\n");
211 }
212
213
214 /****************************************************************************/
215
216
217 #include "debugger_cmds.c"
218
219
220 /****************************************************************************/
221
222
223 /*
224 * debugger_assignment():
225 *
226 * cmd contains something like "pc=0x80001000", or "r31=memcpy+0x40".
227 */
228 void debugger_assignment(struct machine *m, char *cmd)
229 {
230 char *left, *right;
231 int res_left, res_right;
232 uint64_t tmp;
233 uint64_t old_pc = m->cpus[0]->pc; /* TODO: multiple cpus? */
234
235 left = malloc(MAX_CMD_BUFLEN);
236 if (left == NULL) {
237 fprintf(stderr, "out of memory in debugger_assignment()\n");
238 exit(1);
239 }
240 strlcpy(left, cmd, MAX_CMD_BUFLEN);
241 right = strchr(left, '=');
242 if (right == NULL) {
243 fprintf(stderr, "internal error in the debugger\n");
244 exit(1);
245 }
246 *right = '\0';
247
248 /* Remove trailing spaces in left: */
249 while (strlen(left) >= 1 && left[strlen(left)-1] == ' ')
250 left[strlen(left)-1] = '\0';
251
252 /* Remove leading spaces in right: */
253 right++;
254 while (*right == ' ' && *right != '\0')
255 right++;
256
257 /* printf("left = '%s'\nright = '%s'\n", left, right); */
258
259 res_right = debugger_parse_expression(m, right, 0, &tmp);
260 switch (res_right) {
261 case PARSE_NOMATCH:
262 printf("No match for the right-hand side of the assignment.\n");
263 break;
264 case PARSE_MULTIPLE:
265 printf("Multiple matches for the right-hand side of the "
266 "assignment.\n");
267 break;
268 default:
269 res_left = debugger_parse_expression(m, left, 1, &tmp);
270 switch (res_left) {
271 case PARSE_NOMATCH:
272 printf("No match for the left-hand side of the "
273 "assignment.\n");
274 break;
275 case PARSE_MULTIPLE:
276 printf("Multiple matches for the left-hand side "
277 "of the assignment.\n");
278 break;
279 default:
280 debugger_cmd_print(m, left);
281 }
282 }
283
284 /*
285 * If the PC has changed, then release any breakpoint we were
286 * currently stopped at.
287 *
288 * TODO: multiple cpus?
289 */
290 if (old_pc != m->cpus[0]->pc)
291 single_step_breakpoint = 0;
292
293 free(left);
294 }
295
296
297 /*
298 * debugger_execute_cmd():
299 */
300 void debugger_execute_cmd(char *cmd, int cmd_len)
301 {
302 int i, n, i_match, matchlen;
303
304 /*
305 * Is there a '=' on the command line? Then try to do an
306 * assignment. (Only if there is just one word, followed
307 * by the '=' sign. This makes it possible to use commands
308 * such as "device add name addr=xyz".)
309 */
310 if (strchr(cmd, '=') != NULL) {
311 /* Count the nr of words: */
312 int nw = 0, inword = 0;
313 char *p = cmd;
314 while (*p) {
315 if (*p == '=')
316 break;
317 if (*p != ' ') {
318 if (!inword)
319 nw ++;
320 inword = 1;
321 } else
322 inword = 0;
323 p++;
324 }
325
326 if (nw == 1) {
327 debugger_assignment(debugger_machine, cmd);
328 return;
329 }
330 }
331
332 i = 0;
333 while (cmds[i].name != NULL)
334 cmds[i++].tmp_flag = 0;
335
336 /* How many chars in cmd to match against: */
337 matchlen = 0;
338 while (isalpha((int)cmd[matchlen]))
339 matchlen ++;
340
341 /* Check for a command name match: */
342 n = i = i_match = 0;
343 while (cmds[i].name != NULL) {
344 if (strncasecmp(cmds[i].name, cmd, matchlen) == 0
345 && cmds[i].f != NULL) {
346 cmds[i].tmp_flag = 1;
347 i_match = i;
348 n++;
349 }
350 i++;
351 }
352
353 /* No match? */
354 if (n == 0) {
355 printf("Unknown command '%s'. Type 'help' for help.\n", cmd);
356 return;
357 }
358
359 /* More than one match? */
360 if (n > 1) {
361 printf("Ambiguous command '%s': ", cmd);
362 i = 0;
363 while (cmds[i].name != NULL) {
364 if (cmds[i].tmp_flag)
365 printf(" %s", cmds[i].name);
366 i++;
367 }
368 printf("\n");
369 return;
370 }
371
372 /* Exactly one match: */
373 if (cmds[i_match].f != NULL) {
374 char *p = cmd + matchlen;
375 /* Remove leading whitespace from the args... */
376 while (*p != '\0' && *p == ' ')
377 p++;
378
379 /* ... and run the command: */
380 cmds[i_match].f(debugger_machine, p);
381 } else
382 printf("FATAL ERROR: internal error in debugger.c:"
383 " no handler for this command?\n");
384 }
385
386
387 /*
388 * debugger_readline():
389 *
390 * Read a line from the terminal.
391 */
392 static char *debugger_readline(void)
393 {
394 int ch, i, j, n, i_match, reallen, cmd_len, cursor_pos;
395 int read_from_index = last_cmd_index;
396 char *cmd = last_cmd[last_cmd_index];
397
398 cmd_len = 0; cmd[0] = '\0';
399 printf("GXemul> ");
400 fflush(stdout);
401
402 ch = '\0';
403 cmd_len = 0;
404 cursor_pos = 0;
405
406 while (ch != '\n' && !exit_debugger) {
407 ch = debugger_readchar();
408
409 if ((ch == '\b' || ch == 127) && cursor_pos > 0) {
410 /* Backspace. */
411 cursor_pos --;
412 cmd_len --;
413 memmove(cmd + cursor_pos, cmd + cursor_pos + 1,
414 cmd_len);
415 cmd[cmd_len] = '\0';
416 printf("\b");
417 for (i=cursor_pos; i<cmd_len; i++)
418 printf("%c", cmd[i]);
419 printf(" \b");
420 for (i=cursor_pos; i<cmd_len; i++)
421 printf("\b");
422 } else if (ch == 4 && cmd_len > 0 && cursor_pos < cmd_len) {
423 /* CTRL-D: Delete. */
424 cmd_len --;
425 memmove(cmd + cursor_pos, cmd + cursor_pos + 1,
426 cmd_len);
427 cmd[cmd_len] = '\0';
428 for (i=cursor_pos; i<cmd_len; i++)
429 printf("%c", cmd[i]);
430 printf(" \b");
431 for (i=cursor_pos; i<cmd_len; i++)
432 printf("\b");
433 } else if (ch == 1) {
434 /* CTRL-A: Start of line. */
435 while (cursor_pos > 0) {
436 cursor_pos --;
437 printf("\b");
438 }
439 } else if (ch == 2) {
440 /* CTRL-B: Backwards one character. */
441 if (cursor_pos > 0) {
442 printf("\b");
443 cursor_pos --;
444 }
445 } else if (ch == 5) {
446 /* CTRL-E: End of line. */
447 while (cursor_pos < cmd_len) {
448 printf("%c", cmd[cursor_pos]);
449 cursor_pos ++;
450 }
451 } else if (ch == 6) {
452 /* CTRL-F: Forward one character. */
453 if (cursor_pos < cmd_len) {
454 printf("%c",
455 cmd[cursor_pos]);
456 cursor_pos ++;
457 }
458 } else if (ch == 11) {
459 /* CTRL-K: Kill to end of line. */
460 for (i=0; i<MAX_CMD_BUFLEN; i++)
461 console_makeavail(MAIN_CONSOLE, 4); /* :-) */
462 } else if (ch == 14 || ch == 16) {
463 /* CTRL-P: Previous line in the command history,
464 CTRL-N: next line */
465 do {
466 if (ch == 14 &&
467 read_from_index == last_cmd_index)
468 break;
469 if (ch == 16)
470 i = read_from_index - 1;
471 else
472 i = read_from_index + 1;
473
474 if (i < 0)
475 i = N_PREVIOUS_CMDS - 1;
476 if (i >= N_PREVIOUS_CMDS)
477 i = 0;
478
479 /* Special case: pressing 'down'
480 to reach last_cmd_index: */
481 if (i == last_cmd_index) {
482 read_from_index = i;
483 for (i=cursor_pos; i<cmd_len;
484 i++)
485 printf(" ");
486 for (i=cmd_len-1; i>=0; i--)
487 printf("\b \b");
488 cmd[0] = '\0';
489 cmd_len = cursor_pos = 0;
490 } else if (last_cmd[i][0] != '\0') {
491 /* Copy from old line: */
492 read_from_index = i;
493 for (i=cursor_pos; i<cmd_len;
494 i++)
495 printf(" ");
496 for (i=cmd_len-1; i>=0; i--)
497 printf("\b \b");
498 strlcpy(cmd,
499 last_cmd[read_from_index],
500 MAX_CMD_BUFLEN);
501 cmd_len = strlen(cmd);
502 printf("%s", cmd);
503 cursor_pos = cmd_len;
504 }
505 } while (0);
506 } else if (ch >= ' ' && cmd_len < MAX_CMD_BUFLEN-1) {
507 /* Visible character: */
508 memmove(cmd + cursor_pos + 1, cmd + cursor_pos,
509 cmd_len - cursor_pos);
510 cmd[cursor_pos] = ch;
511 cmd_len ++;
512 cursor_pos ++;
513 cmd[cmd_len] = '\0';
514 printf("%c", ch);
515 for (i=cursor_pos; i<cmd_len; i++)
516 printf("%c", cmd[i]);
517 for (i=cursor_pos; i<cmd_len; i++)
518 printf("\b");
519 } else if (ch == '\r' || ch == '\n') {
520 ch = '\n';
521 printf("\n");
522 } else if (ch == '\t') {
523 /* Super-simple tab-completion: */
524 i = 0;
525 while (cmds[i].name != NULL)
526 cmds[i++].tmp_flag = 0;
527
528 /* Check for a (partial) command match: */
529 n = i = i_match = 0;
530 while (cmds[i].name != NULL) {
531 if (strncasecmp(cmds[i].name, cmd,
532 cmd_len) == 0) {
533 cmds[i].tmp_flag = 1;
534 i_match = i;
535 n++;
536 }
537 i++;
538 }
539
540 switch (n) {
541 case 0: /* Beep. */
542 printf("\a");
543 break;
544 case 1: /* Add the rest of the command: */
545 reallen = strlen(cmds[i_match].name);
546 for (i=cmd_len; i<reallen; i++)
547 console_makeavail(MAIN_CONSOLE,
548 cmds[i_match].name[i]);
549 /* ... and a space, if the command takes
550 any arguments: */
551 if (cmds[i_match].args != NULL &&
552 cmds[i_match].args[0] != '\0')
553 console_makeavail(MAIN_CONSOLE, ' ');
554 break;
555 default:
556 /* Show all possible commands: */
557 printf("\a\n"); /* Beep. :-) */
558 i = 0; /* i = cmds index */
559 j = 0; /* j = # of cmds printed */
560 while (cmds[i].name != NULL) {
561 if (cmds[i].tmp_flag) {
562 size_t q;
563 if (j == 0)
564 printf(" ");
565 printf("%s",
566 cmds[i].name);
567 j++;
568 if (j != 6)
569 for (q=0; q<13-strlen(
570 cmds[i].name); q++)
571 printf(" ");
572 if (j == 6) {
573 printf("\n");
574 j = 0;
575 }
576 }
577 i++;
578 }
579 if (j != 0)
580 printf("\n");
581 printf("GXemul> ");
582 for (i=0; i<cmd_len; i++)
583 printf("%c", cmd[i]);
584 }
585 } else if (ch == 27) {
586 /* Escape codes: (cursor keys etc) */
587 while ((ch = console_readchar(MAIN_CONSOLE)) < 0)
588 usleep(10000);
589 if (ch == '[' || ch == 'O') {
590 while ((ch = console_readchar(MAIN_CONSOLE))
591 < 0)
592 usleep(10000);
593 switch (ch) {
594 case '2': /* 2~ = ins */
595 case '5': /* 5~ = pgup */
596 case '6': /* 6~ = pgdn */
597 /* TODO: Ugly hack, but might work. */
598 while ((ch = console_readchar(
599 MAIN_CONSOLE)) < 0)
600 usleep(10000);
601 /* Do nothing for these keys. */
602 break;
603 case '3': /* 3~ = delete */
604 /* TODO: Ugly hack, but might work. */
605 while ((ch = console_readchar(
606 MAIN_CONSOLE)) < 0)
607 usleep(10000);
608 console_makeavail(MAIN_CONSOLE, '\b');
609 break;
610 case 'A': /* Up. */
611 /* Up cursor ==> CTRL-P */
612 console_makeavail(MAIN_CONSOLE, 16);
613 break;
614 case 'B': /* Down. */
615 /* Down cursor ==> CTRL-N */
616 console_makeavail(MAIN_CONSOLE, 14);
617 break;
618 case 'C':
619 /* Right cursor ==> CTRL-F */
620 console_makeavail(MAIN_CONSOLE, 6);
621 break;
622 case 'D': /* Left */
623 /* Left cursor ==> CTRL-B */
624 console_makeavail(MAIN_CONSOLE, 2);
625 break;
626 case 'F':
627 /* End ==> CTRL-E */
628 console_makeavail(MAIN_CONSOLE, 5);
629 break;
630 case 'H':
631 /* Home ==> CTRL-A */
632 console_makeavail(MAIN_CONSOLE, 1);
633 break;
634 }
635 }
636 }
637
638 fflush(stdout);
639 }
640
641 if (exit_debugger)
642 cmd[0] = '\0';
643
644 return cmd;
645 }
646
647
648 /*
649 * debugger():
650 *
651 * This is a loop, which reads a command from the terminal, and executes it.
652 */
653 void debugger(void)
654 {
655 int i, cmd_len;
656 char *cmd;
657
658 if (debugger_n_steps_left_before_interaction > 0) {
659 debugger_n_steps_left_before_interaction --;
660 return;
661 }
662
663 /*
664 * Clear all dyntrans translations, because otherwise things would
665 * become to complex to keep in sync.
666 */
667 /* TODO: In all machines */
668 for (i=0; i<debugger_machine->ncpus; i++)
669 if (debugger_machine->cpus[i]->translation_cache != NULL) {
670 cpu_create_or_reset_tc(debugger_machine->cpus[i]);
671 debugger_machine->cpus[i]->
672 invalidate_translation_caches(
673 debugger_machine->cpus[i], 0, INVALIDATE_ALL);
674 }
675
676 /*
677 * Ugly GDB hack: After single stepping, we need to send back
678 * status to GDB:
679 */
680 if (exit_debugger == -1) {
681 int i, j;
682 for (i=0; i<debugger_n_emuls; i++) {
683 struct emul *e = debugger_emuls[i];
684 if (e == NULL)
685 continue;
686
687 for (j=0; j<e->n_machines; j++) {
688 if (e->machines[j]->gdb.port > 0)
689 debugger_gdb_after_singlestep(
690 e->machines[j]);
691 }
692 }
693 }
694
695
696 /* Stop timers while interacting with the user: */
697 timer_stop();
698
699 exit_debugger = 0;
700
701 while (!exit_debugger) {
702 /* Read a line from the terminal: */
703 cmd = debugger_readline();
704
705 /* Special hack for the "step" _GDB_ command: */
706 if (exit_debugger == -1)
707 return;
708
709 cmd_len = strlen(cmd);
710
711 /* Remove spaces: */
712 while (cmd_len > 0 && cmd[0]==' ')
713 memmove(cmd, cmd+1, cmd_len --);
714 while (cmd_len > 0 && cmd[cmd_len-1] == ' ')
715 cmd[(cmd_len--)-1] = '\0';
716
717 /* No command? Then try reading another line. */
718 if (cmd_len == 0) {
719 /* Special case for repeated commands: */
720 if (repeat_cmd[0] != '\0')
721 strlcpy(cmd, repeat_cmd, MAX_CMD_BUFLEN);
722 else
723 continue;
724 } else {
725 last_cmd_index ++;
726 if (last_cmd_index >= N_PREVIOUS_CMDS)
727 last_cmd_index = 0;
728
729 repeat_cmd[0] = '\0';
730 }
731
732 debugger_execute_cmd(cmd, cmd_len);
733
734 /* Special hack for the "step" command: */
735 if (exit_debugger == -1)
736 return;
737 }
738
739 /* Start up timers again: */
740 timer_start();
741
742 /* ... and reset starttime, so that nr of instructions per second
743 can be calculated correctly: */
744 gettimeofday(&debugger_machine->starttime, NULL);
745 debugger_machine->ninstrs_since_gettimeofday = 0;
746
747 single_step = NOT_SINGLE_STEPPING;
748 debugger_machine->instruction_trace = old_instruction_trace;
749 debugger_machine->show_trace_tree = old_show_trace_tree;
750 quiet_mode = old_quiet_mode;
751 }
752
753
754 /*
755 * debugger_reset():
756 *
757 * This function should be called before calling debugger(), when it is
758 * absolutely necessary that debugger() is interactive. Otherwise, it might
759 * return without doing anything, such as when single-stepping multiple
760 * instructions at a time.
761 */
762 void debugger_reset(void)
763 {
764 debugger_n_steps_left_before_interaction = 0;
765 }
766
767
768 /*
769 * debugger_init():
770 *
771 * Must be called before any other debugger function is used.
772 */
773 void debugger_init(struct emul **emuls, int n_emuls)
774 {
775 int i, j;
776
777 debugger_n_emuls = n_emuls;
778 debugger_emuls = emuls;
779
780 if (n_emuls < 1) {
781 fprintf(stderr, "\nERROR: No emuls (?)\n");
782 exit(1);
783 }
784
785 debugger_emul = emuls[0];
786 if (emuls[0]->n_machines < 1) {
787 fprintf(stderr, "\nERROR: No machines in emuls[0], "
788 "cannot handle this situation yet.\n\n");
789 exit(1);
790 }
791
792 for (i=0; i<n_emuls; i++)
793 for (j=0; j<emuls[i]->n_machines; j++)
794 debugger_gdb_init(emuls[i]->machines[j]);
795
796 debugger_machine = emuls[0]->machines[0];
797
798 debugger_cur_cpu = 0;
799 debugger_cur_machine = 0;
800 debugger_cur_emul = 0;
801
802 for (i=0; i<N_PREVIOUS_CMDS; i++) {
803 last_cmd[i] = malloc(MAX_CMD_BUFLEN);
804 if (last_cmd[i] == NULL) {
805 fprintf(stderr, "debugger_init(): out of memory\n");
806 exit(1);
807 }
808 last_cmd[i][0] = '\0';
809 }
810
811 last_cmd_index = 0;
812 repeat_cmd[0] = '\0';
813 }
814

  ViewVC Help
Powered by ViewVC 1.1.26