2 * vim:shiftwidth=2:expandtab
3 * PDB, the PicoDrive debugger
8 #include "../pico/pico_int.h"
11 static char pdb_pending_cmds[128];
12 static char pdb_event_cmds[128];
14 static struct pdb_cpu {
19 unsigned int bpts[16];
23 static int pdb_cpu_count;
25 static int pdb_global_icount;
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
35 #include "debug_net.h"
37 static int pdb_net_sock = -1;
39 int pdb_net_connect(const char *host, const char *port)
41 struct sockaddr_in sockadr;
45 sock = socket(PF_INET, SOCK_STREAM, 0);
51 sockadr.sin_addr.s_addr = inet_addr(host);
52 sockadr.sin_family = AF_INET;
53 sockadr.sin_port = htons(atoi(port));
55 ret = connect(sock, (struct sockaddr *)&sockadr, sizeof(sockadr));
57 perror("pdb_net: connect");
62 printf("pdb_net: connected to %s:%s\n", host, port);
68 static int pdb_net_send(struct pdb_cpu *cpu, unsigned int pc)
74 return 0; // not connected
76 if (cpu->type == PDBCT_SH2) {
77 SH2 *sh2 = cpu->context;
78 int rl = offsetof(SH2, macl) + sizeof(sh2->macl);
79 packet.header.type = PDBCT_SH2;
80 packet.header.cpuid = cpu->id;
82 memcpy(&packet.regs[1], sh2->r, rl);
83 packet.regs[1+24+0] = sh2->pdb_io_csum[0];
84 packet.regs[1+24+1] = sh2->pdb_io_csum[1];
85 packet.header.len = 4 + rl + 4*2;
86 sh2->pdb_io_csum[0] = sh2->pdb_io_csum[1] = 0;
89 ret = send(pdb_net_sock, &packet, sizeof(packet.header) + packet.header.len, MSG_NOSIGNAL);
90 if (ret != sizeof(packet.header) + packet.header.len) {
94 printf("send: %d/%d\n", ret, sizeof(packet.header) + packet.header.len);
102 #define pdb_net_send(a,b) 0
106 #include <readline/readline.h>
107 #include <readline/history.h>
110 static char *my_readline(const char *prompt)
115 line = readline("(pdb) ");
126 ret = getline(&line, &size, stdin);
129 if (ret > 0 && line[ret - 1] == '\n')
136 static struct pdb_cpu *context2cpu(const void *context)
139 for (i = 0; i < pdb_cpu_count; i++)
140 if (pdb_cpus[i].context == context)
145 static const char *get_token(char *buf, int blen, const char *str)
147 const char *p, *s, *e;
161 while (*p != 0 && *p != ';' && !isspace_(*p))
175 static const char *get_arg(char *buf, int blen, const char *str)
179 return get_token(buf, blen, str);
183 CMDRET_DONE, // ..and back to prompt
184 // CMDRET_PROMPT, // go to prompt
185 CMDRET_CONT_DO_NEXT, // continue and do remaining cmds on next event
186 CMDRET_CONT_REDO, // continue and redo all cmds on next event
189 static int do_print(struct pdb_cpu *cpu, const char *args)
191 elprintf(EL_STATUS, "cpu %d (%s)", cpu->id, cpu->name);
193 if (cpu->type == PDBCT_SH2) {
194 SH2 *sh2 = cpu->context;
196 printf("PC,SR %08x, %03x\n", sh2->pc, sh2->sr & 0x3ff);
197 for (i = 0; i < 16/2; i++)
198 printf("R%d,%2d %08x,%08x\n", i, i + 8, sh2->r[i], sh2->r[i + 8]);
199 printf("gb,vb %08x,%08x\n", sh2->gbr, sh2->vbr);
200 printf("IRQs/mask: %02x/%02x\n", Pico32x.sh2irqi[sh2->is_slave],
201 Pico32x.sh2irq_mask[sh2->is_slave]);
202 printf("cycles %d/%d (%d)\n", sh2->cycles_done, sh2->cycles_aim, (signed int)sh2->sr >> 12);
208 static int do_step_all(struct pdb_cpu *cpu, const char *args)
211 if (!get_arg(tmp, sizeof(tmp), args)) {
212 printf("step_all: missing arg\n");
216 pdb_global_icount = atoi(tmp);
217 return CMDRET_CONT_DO_NEXT;
220 static int do_continue(struct pdb_cpu *cpu, const char *args)
223 if (get_arg(tmp, sizeof(tmp), args))
224 cpu->icount = atoi(tmp);
225 return CMDRET_CONT_DO_NEXT;
228 static int do_step(struct pdb_cpu *cpu, const char *args)
231 return do_continue(cpu, args);
234 static int do_waitcpu(struct pdb_cpu *cpu, const char *args)
237 if (!get_arg(tmp, sizeof(tmp), args)) {
238 printf("waitcpu: missing arg\n");
241 if (strcmp(tmp, cpu->name) == 0)
244 return CMDRET_CONT_REDO;
247 static int do_help(struct pdb_cpu *cpu, const char *args);
252 int (*handler)(struct pdb_cpu *cpu, const char *args);
254 { "help", "", do_help },
255 { "continue", "[insns]", do_continue },
256 { "step", "[insns]", do_step },
257 { "step_all", "<insns>", do_step_all },
258 { "waitcpu", "<cpuname>", do_waitcpu },
259 { "print", "", do_print },
262 static int do_help(struct pdb_cpu *cpu, const char *args)
265 for (i = 0; i < ARRAY_SIZE(pdb_cmds); i++)
266 printf("%s %s\n", pdb_cmds[i].cmd, pdb_cmds[i].help);
270 static int do_comands(struct pdb_cpu *cpu, const char *cmds)
272 const char *p = cmds;
281 p = get_token(cmd, sizeof(cmd), p);
288 for (i = 0; i < ARRAY_SIZE(pdb_cmds); i++)
289 if (strncmp(pdb_cmds[i].cmd, cmd, len) == 0) {
290 ret = pdb_cmds[i].handler(cpu, p);
294 if (i == ARRAY_SIZE(pdb_cmds)) {
295 printf("bad cmd: %s\n", cmd);
299 // skip until next command
301 p = get_token(cmd, sizeof(cmd), p);
302 if (p == NULL || cmd[0] == ';')
306 pdb_event_cmds[0] = 0;
307 if (ret == CMDRET_CONT_DO_NEXT) {
308 pdb_pending_cmds[0] = 0;
310 strcpy(pdb_event_cmds, p);
313 if (ret == CMDRET_CONT_REDO) {
314 if (pcmd != pdb_pending_cmds)
315 strncpy(pdb_pending_cmds, pcmd, sizeof(pdb_pending_cmds));
318 pdb_pending_cmds[0] = 0;
324 static void do_prompt(struct pdb_cpu *cpu)
326 static char prev[128];
332 line = my_readline("(pdb) ");
339 strncpy(prev, line, sizeof(prev));
342 ret = do_comands(cpu, cline);
350 void pdb_register_cpu(void *context, int type, const char *name)
352 int i = pdb_cpu_count;
353 memset(&pdb_cpus[i], 0, sizeof(pdb_cpus[i]));
354 pdb_cpus[i].context = context;
355 pdb_cpus[i].type = type;
356 pdb_cpus[i].id = pdb_cpu_count;
357 pdb_cpus[i].name = name;
358 pdb_cpus[i].icount = -1;
362 void pdb_step(void *context, unsigned int pc)
364 struct pdb_cpu *cpu = context2cpu(context);
367 if (pdb_net_send(cpu, pc) < 0)
370 if (pdb_pending_cmds[0] != 0)
371 if (do_comands(cpu, pdb_pending_cmds))
375 for (i = 0; i < cpu->bpt_count; i++)
376 if (cpu->bpts[i] == pc)
380 if (pdb_global_icount > 0)
381 if (--pdb_global_icount == 0)
385 if (--(cpu->icount) == 0)
391 if (pdb_event_cmds[0] != 0)
392 if (!do_comands(cpu, pdb_event_cmds))
395 printf("%s @%08x\n", cpu->name, pc);
399 void pdb_command(const char *cmd)
401 strncpy(pdb_pending_cmds, cmd, sizeof(pdb_pending_cmds));
402 pdb_pending_cmds[sizeof(pdb_pending_cmds) - 1] = 0;
405 void pdb_cleanup(void)