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 ret = send(pdb_net_sock, &packet, sizeof(packet.header) + packet.header.len, MSG_NOSIGNAL);
93 if (ret != sizeof(packet.header) + packet.header.len) {
97 printf("send: %d/%d\n", ret, sizeof(packet.header) + packet.header.len);
105 #define pdb_net_send(a,b) 0
109 #include <readline/readline.h>
110 #include <readline/history.h>
113 static char *my_readline(const char *prompt)
118 line = readline("(pdb) ");
129 ret = getline(&line, &size, stdin);
132 if (ret > 0 && line[ret - 1] == '\n')
139 static struct pdb_cpu *context2cpu(const void *context)
142 for (i = 0; i < pdb_cpu_count; i++)
143 if (pdb_cpus[i].context == context)
148 static const char *get_token(char *buf, int blen, const char *str)
150 const char *p, *s, *e;
164 while (*p != 0 && *p != ';' && !isspace_(*p))
178 static const char *get_arg(char *buf, int blen, const char *str)
182 return get_token(buf, blen, str);
186 CMDRET_DONE, // ..and back to prompt
187 // CMDRET_PROMPT, // go to prompt
188 CMDRET_CONT_DO_NEXT, // continue and do remaining cmds on next event
189 CMDRET_CONT_REDO, // continue and redo all cmds on next event
192 static int do_print(struct pdb_cpu *cpu, const char *args)
194 elprintf(EL_STATUS, "cpu %d (%s)", cpu->id, cpu->name);
196 if (cpu->type == PDBCT_SH2) {
197 SH2 *sh2 = cpu->context;
199 printf("PC,SR %08x, %03x\n", sh2->pc, sh2->sr & 0x3ff);
200 for (i = 0; i < 16/2; i++)
201 printf("R%d,%2d %08x,%08x\n", i, i + 8, sh2->r[i], sh2->r[i + 8]);
202 printf("gb,vb %08x,%08x\n", sh2->gbr, sh2->vbr);
203 printf("IRQs/mask: %02x/%02x\n", Pico32x.sh2irqi[sh2->is_slave],
204 Pico32x.sh2irq_mask[sh2->is_slave]);
205 printf("cycles %d/%d (%d)\n", sh2->cycles_done, sh2->cycles_aim, (signed int)sh2->sr >> 12);
211 static int do_step_all(struct pdb_cpu *cpu, const char *args)
214 if (!get_arg(tmp, sizeof(tmp), args)) {
215 printf("step_all: missing arg\n");
219 pdb_global_icount = atoi(tmp);
220 return CMDRET_CONT_DO_NEXT;
223 static int do_continue(struct pdb_cpu *cpu, const char *args)
226 if (get_arg(tmp, sizeof(tmp), args))
227 cpu->icount = atoi(tmp);
228 return CMDRET_CONT_DO_NEXT;
231 static int do_step(struct pdb_cpu *cpu, const char *args)
234 return do_continue(cpu, args);
237 static int do_waitcpu(struct pdb_cpu *cpu, const char *args)
240 if (!get_arg(tmp, sizeof(tmp), args)) {
241 printf("waitcpu: missing arg\n");
244 if (strcmp(tmp, cpu->name) == 0)
247 return CMDRET_CONT_REDO;
250 static int do_help(struct pdb_cpu *cpu, const char *args);
255 int (*handler)(struct pdb_cpu *cpu, const char *args);
257 { "help", "", do_help },
258 { "continue", "[insns]", do_continue },
259 { "step", "[insns]", do_step },
260 { "step_all", "<insns>", do_step_all },
261 { "waitcpu", "<cpuname>", do_waitcpu },
262 { "print", "", do_print },
265 static int do_help(struct pdb_cpu *cpu, const char *args)
268 for (i = 0; i < ARRAY_SIZE(pdb_cmds); i++)
269 printf("%s %s\n", pdb_cmds[i].cmd, pdb_cmds[i].help);
273 static int do_comands(struct pdb_cpu *cpu, const char *cmds)
275 const char *p = cmds;
284 p = get_token(cmd, sizeof(cmd), p);
291 for (i = 0; i < ARRAY_SIZE(pdb_cmds); i++)
292 if (strncmp(pdb_cmds[i].cmd, cmd, len) == 0) {
293 ret = pdb_cmds[i].handler(cpu, p);
297 if (i == ARRAY_SIZE(pdb_cmds)) {
298 printf("bad cmd: %s\n", cmd);
302 // skip until next command
304 p = get_token(cmd, sizeof(cmd), p);
305 if (p == NULL || cmd[0] == ';')
309 pdb_event_cmds[0] = 0;
310 if (ret == CMDRET_CONT_DO_NEXT) {
311 pdb_pending_cmds[0] = 0;
313 strcpy(pdb_event_cmds, p);
316 if (ret == CMDRET_CONT_REDO) {
317 if (pcmd != pdb_pending_cmds)
318 strncpy(pdb_pending_cmds, pcmd, sizeof(pdb_pending_cmds));
321 pdb_pending_cmds[0] = 0;
327 static void do_prompt(struct pdb_cpu *cpu)
329 static char prev[128];
335 line = my_readline("(pdb) ");
342 strncpy(prev, line, sizeof(prev));
345 ret = do_comands(cpu, cline);
353 void pdb_register_cpu(void *context, int type, const char *name)
355 int i = pdb_cpu_count;
356 memset(&pdb_cpus[i], 0, sizeof(pdb_cpus[i]));
357 pdb_cpus[i].context = context;
358 pdb_cpus[i].type = type;
359 pdb_cpus[i].id = pdb_cpu_count;
360 pdb_cpus[i].name = name;
361 pdb_cpus[i].icount = -1;
365 void pdb_step(void *context, unsigned int pc)
367 struct pdb_cpu *cpu = context2cpu(context);
370 if (pdb_net_send(cpu, pc) < 0)
373 if (pdb_pending_cmds[0] != 0)
374 if (do_comands(cpu, pdb_pending_cmds))
378 for (i = 0; i < cpu->bpt_count; i++)
379 if (cpu->bpts[i] == pc)
383 if (pdb_global_icount > 0)
384 if (--pdb_global_icount == 0)
388 if (--(cpu->icount) == 0)
394 if (pdb_event_cmds[0] != 0)
395 if (!do_comands(cpu, pdb_event_cmds))
398 printf("%s @%08x\n", cpu->name, pc);
402 void pdb_command(const char *cmd)
404 strncpy(pdb_pending_cmds, cmd, sizeof(pdb_pending_cmds));
405 pdb_pending_cmds[sizeof(pdb_pending_cmds) - 1] = 0;
408 void pdb_cleanup(void)
413 // vim:shiftwidth=2:expandtab