debug.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * PDB, the PicoDrive debugger
  3. * (C) notaz, 2010
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #define _GNU_SOURCE
  9. #include <stdio.h>
  10. #include <pico/pico_int.h>
  11. #include "debug.h"
  12. static char pdb_pending_cmds[128];
  13. static char pdb_event_cmds[128];
  14. static struct pdb_cpu {
  15. void *context;
  16. int type;
  17. int id;
  18. const char *name;
  19. unsigned int bpts[16];
  20. int bpt_count;
  21. int icount;
  22. } pdb_cpus[5];
  23. static int pdb_cpu_count;
  24. static int pdb_global_icount;
  25. #ifdef PDB_NET
  26. #include <stddef.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #include <unistd.h>
  32. #include "debug_net.h"
  33. static int pdb_net_sock = -1;
  34. int pdb_net_connect(const char *host, const char *port)
  35. {
  36. struct sockaddr_in sockadr;
  37. int sock = -1;
  38. int ret;
  39. sock = socket(PF_INET, SOCK_STREAM, 0);
  40. if (sock == -1) {
  41. perror("socket");
  42. return -1;
  43. }
  44. sockadr.sin_addr.s_addr = inet_addr(host);
  45. sockadr.sin_family = AF_INET;
  46. sockadr.sin_port = htons(atoi(port));
  47. ret = connect(sock, (struct sockaddr *)&sockadr, sizeof(sockadr));
  48. if (ret != 0) {
  49. perror("pdb_net: connect");
  50. close(sock);
  51. return -1;
  52. }
  53. printf("pdb_net: connected to %s:%s\n", host, port);
  54. pdb_net_sock = sock;
  55. return 0;
  56. }
  57. static int pdb_net_send(struct pdb_cpu *cpu, unsigned int pc)
  58. {
  59. packet_t packet;
  60. int ret;
  61. if (pdb_net_sock < 0)
  62. return 0; // not connected
  63. if (cpu->type == PDBCT_SH2) {
  64. SH2 *sh2 = cpu->context;
  65. int rl = offsetof(SH2, macl) + sizeof(sh2->macl);
  66. packet.header.type = PDBCT_SH2;
  67. packet.header.cpuid = cpu->id;
  68. packet.regs[0] = pc;
  69. memcpy(&packet.regs[1], sh2->r, rl);
  70. packet.regs[1+24+0] = sh2->pdb_io_csum[0];
  71. packet.regs[1+24+1] = sh2->pdb_io_csum[1];
  72. packet.header.len = 4 + rl + 4*2;
  73. sh2->pdb_io_csum[0] = sh2->pdb_io_csum[1] = 0;
  74. }
  75. else
  76. memset(&packet, 0, sizeof(packet));
  77. ret = send(pdb_net_sock, &packet, sizeof(packet.header) + packet.header.len, MSG_NOSIGNAL);
  78. if (ret != sizeof(packet.header) + packet.header.len) {
  79. if (ret < 0)
  80. perror("send");
  81. else
  82. printf("send: %d/%d\n", ret, sizeof(packet.header) + packet.header.len);
  83. close(pdb_net_sock);
  84. pdb_net_sock = -1;
  85. ret = -1;
  86. }
  87. return ret;
  88. }
  89. #else
  90. #define pdb_net_send(a,b) 0
  91. #endif // PDB_NET
  92. #ifdef HAVE_READLINE
  93. #include <readline/readline.h>
  94. #include <readline/history.h>
  95. #endif
  96. static char *my_readline(const char *prompt)
  97. {
  98. char *line = NULL;
  99. #ifdef HAVE_READLINE
  100. line = readline("(pdb) ");
  101. if (line == NULL)
  102. return NULL;
  103. if (line[0] != 0)
  104. add_history(line);
  105. #else
  106. size_t size = 0;
  107. ssize_t ret;
  108. printf("(pdb) ");
  109. fflush(stdout);
  110. ret = getline(&line, &size, stdin);
  111. if (ret < 0)
  112. return NULL;
  113. if (ret > 0 && line[ret - 1] == '\n')
  114. line[ret - 1] = 0;
  115. #endif
  116. return line;
  117. }
  118. static struct pdb_cpu *context2cpu(const void *context)
  119. {
  120. int i;
  121. for (i = 0; i < pdb_cpu_count; i++)
  122. if (pdb_cpus[i].context == context)
  123. return &pdb_cpus[i];
  124. return NULL;
  125. }
  126. static const char *get_token(char *buf, int blen, const char *str)
  127. {
  128. const char *p, *s, *e;
  129. int len;
  130. p = str;
  131. while (isspace_(*p))
  132. p++;
  133. if (*p == 0)
  134. return NULL;
  135. if (*p == ';') {
  136. strcpy(buf, ";");
  137. return p + 1;
  138. }
  139. s = p;
  140. while (*p != 0 && *p != ';' && !isspace_(*p))
  141. p++;
  142. e = p;
  143. while (isspace_(*e))
  144. e++;
  145. len = p - s;
  146. if (len > blen - 1)
  147. len = blen - 1;
  148. memcpy(buf, s, len);
  149. buf[len] = 0;
  150. return e;
  151. }
  152. static const char *get_arg(char *buf, int blen, const char *str)
  153. {
  154. if (*str == ';')
  155. return NULL;
  156. return get_token(buf, blen, str);
  157. }
  158. enum cmd_ret_e {
  159. CMDRET_DONE, // ..and back to prompt
  160. // CMDRET_PROMPT, // go to prompt
  161. CMDRET_CONT_DO_NEXT, // continue and do remaining cmds on next event
  162. CMDRET_CONT_REDO, // continue and redo all cmds on next event
  163. };
  164. static int do_print(struct pdb_cpu *cpu, const char *args)
  165. {
  166. elprintf(EL_STATUS, "cpu %d (%s)", cpu->id, cpu->name);
  167. #ifndef NO_32X
  168. if (cpu->type == PDBCT_SH2) {
  169. SH2 *sh2 = cpu->context;
  170. int i;
  171. printf("PC,SR %08x, %03x\n", sh2->pc, sh2->sr & 0x3ff);
  172. for (i = 0; i < 16/2; i++)
  173. printf("R%d,%2d %08x,%08x\n", i, i + 8, sh2->r[i], sh2->r[i + 8]);
  174. printf("gb,vb %08x,%08x\n", sh2->gbr, sh2->vbr);
  175. printf("IRQs/mask: %02x/%02x\n", Pico32x.sh2irqi[sh2->is_slave],
  176. Pico32x.sh2irq_mask[sh2->is_slave]);
  177. printf("cycles %d/%d (%d)\n", sh2->cycles_done, sh2->cycles_aim, (signed int)sh2->sr >> 12);
  178. }
  179. #endif
  180. return CMDRET_DONE;
  181. }
  182. static int do_step_all(struct pdb_cpu *cpu, const char *args)
  183. {
  184. char tmp[32];
  185. if (!get_arg(tmp, sizeof(tmp), args)) {
  186. printf("step_all: missing arg\n");
  187. return CMDRET_DONE;
  188. }
  189. pdb_global_icount = atoi(tmp);
  190. return CMDRET_CONT_DO_NEXT;
  191. }
  192. static int do_continue(struct pdb_cpu *cpu, const char *args)
  193. {
  194. char tmp[32];
  195. if (get_arg(tmp, sizeof(tmp), args))
  196. cpu->icount = atoi(tmp);
  197. return CMDRET_CONT_DO_NEXT;
  198. }
  199. static int do_step(struct pdb_cpu *cpu, const char *args)
  200. {
  201. cpu->icount = 1;
  202. return do_continue(cpu, args);
  203. }
  204. static int do_waitcpu(struct pdb_cpu *cpu, const char *args)
  205. {
  206. char tmp[32];
  207. if (!get_arg(tmp, sizeof(tmp), args)) {
  208. printf("waitcpu: missing arg\n");
  209. return CMDRET_DONE;
  210. }
  211. if (strcmp(tmp, cpu->name) == 0)
  212. return CMDRET_DONE;
  213. return CMDRET_CONT_REDO;
  214. }
  215. static int do_help(struct pdb_cpu *cpu, const char *args);
  216. static struct {
  217. const char *cmd;
  218. const char *help;
  219. int (*handler)(struct pdb_cpu *cpu, const char *args);
  220. } pdb_cmds[] = {
  221. { "help", "", do_help },
  222. { "continue", "[insns]", do_continue },
  223. { "step", "[insns]", do_step },
  224. { "step_all", "<insns>", do_step_all },
  225. { "waitcpu", "<cpuname>", do_waitcpu },
  226. { "print", "", do_print },
  227. };
  228. static int do_help(struct pdb_cpu *cpu, const char *args)
  229. {
  230. int i;
  231. for (i = 0; i < ARRAY_SIZE(pdb_cmds); i++)
  232. printf("%s %s\n", pdb_cmds[i].cmd, pdb_cmds[i].help);
  233. return CMDRET_DONE;
  234. }
  235. static int do_comands(struct pdb_cpu *cpu, const char *cmds)
  236. {
  237. const char *p = cmds;
  238. while (p != NULL)
  239. {
  240. const char *pcmd;
  241. char cmd[32];
  242. int i, len;
  243. int ret = 0;
  244. pcmd = p;
  245. p = get_token(cmd, sizeof(cmd), p);
  246. if (p == NULL)
  247. break;
  248. if (cmd[0] == ';')
  249. continue;
  250. len = strlen(cmd);
  251. for (i = 0; i < ARRAY_SIZE(pdb_cmds); i++)
  252. if (strncmp(pdb_cmds[i].cmd, cmd, len) == 0) {
  253. ret = pdb_cmds[i].handler(cpu, p);
  254. break;
  255. }
  256. if (i == ARRAY_SIZE(pdb_cmds)) {
  257. printf("bad cmd: %s\n", cmd);
  258. break;
  259. }
  260. // skip until next command
  261. while (1) {
  262. p = get_token(cmd, sizeof(cmd), p);
  263. if (p == NULL || cmd[0] == ';')
  264. break;
  265. }
  266. pdb_event_cmds[0] = 0;
  267. if (ret == CMDRET_CONT_DO_NEXT) {
  268. pdb_pending_cmds[0] = 0;
  269. if (p != NULL)
  270. strcpy(pdb_event_cmds, p);
  271. return 0;
  272. }
  273. if (ret == CMDRET_CONT_REDO) {
  274. if (pcmd != pdb_pending_cmds)
  275. strncpy(pdb_pending_cmds, pcmd, sizeof(pdb_pending_cmds));
  276. return 0;
  277. }
  278. pdb_pending_cmds[0] = 0;
  279. }
  280. return 1;
  281. }
  282. static void do_prompt(struct pdb_cpu *cpu)
  283. {
  284. static char prev[128];
  285. int ret;
  286. while (1) {
  287. char *line, *cline;
  288. line = my_readline("(pdb) ");
  289. if (line == NULL)
  290. break;
  291. if (line[0] == 0)
  292. cline = prev;
  293. else {
  294. cline = line;
  295. strncpy(prev, line, sizeof(prev));
  296. }
  297. ret = do_comands(cpu, cline);
  298. free(line);
  299. if (ret == 0)
  300. break;
  301. }
  302. }
  303. void pdb_register_cpu(void *context, int type, const char *name)
  304. {
  305. int i = pdb_cpu_count;
  306. memset(&pdb_cpus[i], 0, sizeof(pdb_cpus[i]));
  307. pdb_cpus[i].context = context;
  308. pdb_cpus[i].type = type;
  309. pdb_cpus[i].id = pdb_cpu_count;
  310. pdb_cpus[i].name = name;
  311. pdb_cpus[i].icount = -1;
  312. pdb_cpu_count++;
  313. }
  314. void pdb_step(void *context, unsigned int pc)
  315. {
  316. struct pdb_cpu *cpu = context2cpu(context);
  317. int i;
  318. if (pdb_net_send(cpu, pc) < 0)
  319. goto prompt;
  320. if (pdb_pending_cmds[0] != 0)
  321. if (do_comands(cpu, pdb_pending_cmds))
  322. goto prompt;
  323. // breakpoint?
  324. for (i = 0; i < cpu->bpt_count; i++)
  325. if (cpu->bpts[i] == pc)
  326. goto prompt;
  327. // hit num of insns?
  328. if (pdb_global_icount > 0)
  329. if (--pdb_global_icount == 0)
  330. goto prompt;
  331. if (cpu->icount > 0)
  332. if (--(cpu->icount) == 0)
  333. goto prompt;
  334. return;
  335. prompt:
  336. if (pdb_event_cmds[0] != 0)
  337. if (!do_comands(cpu, pdb_event_cmds))
  338. return;
  339. printf("%s @%08x\n", cpu->name, pc);
  340. do_prompt(cpu);
  341. }
  342. void pdb_command(const char *cmd)
  343. {
  344. strncpy(pdb_pending_cmds, cmd, sizeof(pdb_pending_cmds));
  345. pdb_pending_cmds[sizeof(pdb_pending_cmds) - 1] = 0;
  346. }
  347. void pdb_cleanup(void)
  348. {
  349. pdb_cpu_count = 0;
  350. }
  351. // vim:shiftwidth=2:expandtab