2 * PDB, the PicoDrive debugger
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
11 #include "../pico/pico_int.h"
14 static char pdb_pending_cmds[128];
15 static char pdb_event_cmds[128];
17 static struct pdb_cpu {
22 unsigned int bpts[16];
26 static int pdb_cpu_count;
28 static int pdb_global_icount;
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
38 #include "debug_net.h"
40 static int pdb_net_sock = -1;
42 int pdb_net_connect(const char *host, const char *port)
44 struct sockaddr_in sockadr;
48 sock = socket(PF_INET, SOCK_STREAM, 0);
54 sockadr.sin_addr.s_addr = inet_addr(host);
55 sockadr.sin_family = AF_INET;
56 sockadr.sin_port = htons(atoi(port));
58 ret = connect(sock, (struct sockaddr *)&sockadr, sizeof(sockadr));
60 perror("pdb_net: connect");
65 printf("pdb_net: connected to %s:%s\n", host, port);
71 static int pdb_net_send(struct pdb_cpu *cpu, unsigned int pc)
77 return 0; // not connected
79 if (cpu->type == PDBCT_SH2) {
80 SH2 *sh2 = cpu->context;
81 int rl = offsetof(SH2, macl) + sizeof(sh2->macl);
82 packet.header.type = PDBCT_SH2;
83 packet.header.cpuid = cpu->id;
85 memcpy(&packet.regs[1], sh2->r, rl);
86 packet.regs[1+24+0] = sh2->pdb_io_csum[0];
87 packet.regs[1+24+1] = sh2->pdb_io_csum[1];
88 packet.header.len = 4 + rl + 4*2;
89 sh2->pdb_io_csum[0] = sh2->pdb_io_csum[1] = 0;
92 memset(&packet, 0, sizeof(packet));
94 ret = send(pdb_net_sock, &packet, sizeof(packet.header) + packet.header.len, MSG_NOSIGNAL);
95 if (ret != sizeof(packet.header) + packet.header.len) {
99 printf("send: %d/%d\n", ret, sizeof(packet.header) + packet.header.len);
107 #define pdb_net_send(a,b) 0
111 #include <readline/readline.h>
112 #include <readline/history.h>
115 static char *my_readline(const char *prompt)
120 line = readline("(pdb) ");
131 ret = getline(&line, &size, stdin);
134 if (ret > 0 && line[ret - 1] == '\n')
141 static struct pdb_cpu *context2cpu(const void *context)
144 for (i = 0; i < pdb_cpu_count; i++)
145 if (pdb_cpus[i].context == context)
150 static const char *get_token(char *buf, int blen, const char *str)
152 const char *p, *s, *e;
166 while (*p != 0 && *p != ';' && !isspace_(*p))
180 static const char *get_arg(char *buf, int blen, const char *str)
184 return get_token(buf, blen, str);
188 CMDRET_DONE, // ..and back to prompt
189 // CMDRET_PROMPT, // go to prompt
190 CMDRET_CONT_DO_NEXT, // continue and do remaining cmds on next event
191 CMDRET_CONT_REDO, // continue and redo all cmds on next event
194 static int do_print(struct pdb_cpu *cpu, const char *args)
196 elprintf(EL_STATUS, "cpu %d (%s)", cpu->id, cpu->name);
198 if (cpu->type == PDBCT_SH2) {
199 SH2 *sh2 = cpu->context;
201 printf("PC,SR %08x, %03x\n", sh2->pc, sh2->sr & 0x3ff);
202 for (i = 0; i < 16/2; i++)
203 printf("R%d,%2d %08x,%08x\n", i, i + 8, sh2->r[i], sh2->r[i + 8]);
204 printf("gb,vb %08x,%08x\n", sh2->gbr, sh2->vbr);
205 printf("IRQs/mask: %02x/%02x\n", Pico32x.sh2irqi[sh2->is_slave],
206 Pico32x.sh2irq_mask[sh2->is_slave]);
207 printf("cycles %d/%d (%d)\n", sh2->cycles_done, sh2->cycles_aim, (signed int)sh2->sr >> 12);
213 static int do_step_all(struct pdb_cpu *cpu, const char *args)
216 if (!get_arg(tmp, sizeof(tmp), args)) {
217 printf("step_all: missing arg\n");
221 pdb_global_icount = atoi(tmp);
222 return CMDRET_CONT_DO_NEXT;
225 static int do_continue(struct pdb_cpu *cpu, const char *args)
228 if (get_arg(tmp, sizeof(tmp), args))
229 cpu->icount = atoi(tmp);
230 return CMDRET_CONT_DO_NEXT;
233 static int do_step(struct pdb_cpu *cpu, const char *args)
236 return do_continue(cpu, args);
239 static int do_waitcpu(struct pdb_cpu *cpu, const char *args)
242 if (!get_arg(tmp, sizeof(tmp), args)) {
243 printf("waitcpu: missing arg\n");
246 if (strcmp(tmp, cpu->name) == 0)
249 return CMDRET_CONT_REDO;
252 static int do_help(struct pdb_cpu *cpu, const char *args);
257 int (*handler)(struct pdb_cpu *cpu, const char *args);
259 { "help", "", do_help },
260 { "continue", "[insns]", do_continue },
261 { "step", "[insns]", do_step },
262 { "step_all", "<insns>", do_step_all },
263 { "waitcpu", "<cpuname>", do_waitcpu },
264 { "print", "", do_print },
267 static int do_help(struct pdb_cpu *cpu, const char *args)
270 for (i = 0; i < ARRAY_SIZE(pdb_cmds); i++)
271 printf("%s %s\n", pdb_cmds[i].cmd, pdb_cmds[i].help);
275 static int do_comands(struct pdb_cpu *cpu, const char *cmds)
277 const char *p = cmds;
286 p = get_token(cmd, sizeof(cmd), p);
293 for (i = 0; i < ARRAY_SIZE(pdb_cmds); i++)
294 if (strncmp(pdb_cmds[i].cmd, cmd, len) == 0) {
295 ret = pdb_cmds[i].handler(cpu, p);
299 if (i == ARRAY_SIZE(pdb_cmds)) {
300 printf("bad cmd: %s\n", cmd);
304 // skip until next command
306 p = get_token(cmd, sizeof(cmd), p);
307 if (p == NULL || cmd[0] == ';')
311 pdb_event_cmds[0] = 0;
312 if (ret == CMDRET_CONT_DO_NEXT) {
313 pdb_pending_cmds[0] = 0;
315 strcpy(pdb_event_cmds, p);
318 if (ret == CMDRET_CONT_REDO) {
319 if (pcmd != pdb_pending_cmds)
320 strncpy(pdb_pending_cmds, pcmd, sizeof(pdb_pending_cmds));
323 pdb_pending_cmds[0] = 0;
329 static void do_prompt(struct pdb_cpu *cpu)
331 static char prev[128];
337 line = my_readline("(pdb) ");
344 strncpy(prev, line, sizeof(prev));
347 ret = do_comands(cpu, cline);
355 void pdb_register_cpu(void *context, int type, const char *name)
357 int i = pdb_cpu_count;
358 memset(&pdb_cpus[i], 0, sizeof(pdb_cpus[i]));
359 pdb_cpus[i].context = context;
360 pdb_cpus[i].type = type;
361 pdb_cpus[i].id = pdb_cpu_count;
362 pdb_cpus[i].name = name;
363 pdb_cpus[i].icount = -1;
367 void pdb_step(void *context, unsigned int pc)
369 struct pdb_cpu *cpu = context2cpu(context);
372 if (pdb_net_send(cpu, pc) < 0)
375 if (pdb_pending_cmds[0] != 0)
376 if (do_comands(cpu, pdb_pending_cmds))
380 for (i = 0; i < cpu->bpt_count; i++)
381 if (cpu->bpts[i] == pc)
385 if (pdb_global_icount > 0)
386 if (--pdb_global_icount == 0)
390 if (--(cpu->icount) == 0)
396 if (pdb_event_cmds[0] != 0)
397 if (!do_comands(cpu, pdb_event_cmds))
400 printf("%s @%08x\n", cpu->name, pc);
404 void pdb_command(const char *cmd)
406 strncpy(pdb_pending_cmds, cmd, sizeof(pdb_pending_cmds));
407 pdb_pending_cmds[sizeof(pdb_pending_cmds) - 1] = 0;
410 void pdb_cleanup(void)
415 // vim:shiftwidth=2:expandtab