debug.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. ret = send(pdb_net_sock, &packet, sizeof(packet.header) + packet.header.len, MSG_NOSIGNAL);
  76. if (ret != sizeof(packet.header) + packet.header.len) {
  77. if (ret < 0)
  78. perror("send");
  79. else
  80. printf("send: %d/%d\n", ret, sizeof(packet.header) + packet.header.len);
  81. close(pdb_net_sock);
  82. pdb_net_sock = -1;
  83. ret = -1;
  84. }
  85. return ret;
  86. }
  87. #else
  88. #define pdb_net_send(a,b) 0
  89. #endif // PDB_NET
  90. #ifdef HAVE_READLINE
  91. #include <readline/readline.h>
  92. #include <readline/history.h>
  93. #endif
  94. static char *my_readline(const char *prompt)
  95. {
  96. char *line = NULL;
  97. #ifdef HAVE_READLINE
  98. line = readline("(pdb) ");
  99. if (line == NULL)
  100. return NULL;
  101. if (line[0] != 0)
  102. add_history(line);
  103. #else
  104. size_t size = 0;
  105. ssize_t ret;
  106. printf("(pdb) ");
  107. fflush(stdout);
  108. ret = getline(&line, &size, stdin);
  109. if (ret < 0)
  110. return NULL;
  111. if (ret > 0 && line[ret - 1] == '\n')
  112. line[ret - 1] = 0;
  113. #endif
  114. return line;
  115. }
  116. static struct pdb_cpu *context2cpu(const void *context)
  117. {
  118. int i;
  119. for (i = 0; i < pdb_cpu_count; i++)
  120. if (pdb_cpus[i].context == context)
  121. return &pdb_cpus[i];
  122. return NULL;
  123. }
  124. static const char *get_token(char *buf, int blen, const char *str)
  125. {
  126. const char *p, *s, *e;
  127. int len;
  128. p = str;
  129. while (isspace_(*p))
  130. p++;
  131. if (*p == 0)
  132. return NULL;
  133. if (*p == ';') {
  134. strcpy(buf, ";");
  135. return p + 1;
  136. }
  137. s = p;
  138. while (*p != 0 && *p != ';' && !isspace_(*p))
  139. p++;
  140. e = p;
  141. while (isspace_(*e))
  142. e++;
  143. len = p - s;
  144. if (len > blen - 1)
  145. len = blen - 1;
  146. memcpy(buf, s, len);
  147. buf[len] = 0;
  148. return e;
  149. }
  150. static const char *get_arg(char *buf, int blen, const char *str)
  151. {
  152. if (*str == ';')
  153. return NULL;
  154. return get_token(buf, blen, str);
  155. }
  156. enum cmd_ret_e {
  157. CMDRET_DONE, // ..and back to prompt
  158. // CMDRET_PROMPT, // go to prompt
  159. CMDRET_CONT_DO_NEXT, // continue and do remaining cmds on next event
  160. CMDRET_CONT_REDO, // continue and redo all cmds on next event
  161. };
  162. static int do_print(struct pdb_cpu *cpu, const char *args)
  163. {
  164. elprintf(EL_STATUS, "cpu %d (%s)", cpu->id, cpu->name);
  165. #ifndef NO_32X
  166. if (cpu->type == PDBCT_SH2) {
  167. SH2 *sh2 = cpu->context;
  168. int i;
  169. printf("PC,SR %08x, %03x\n", sh2->pc, sh2->sr & 0x3ff);
  170. for (i = 0; i < 16/2; i++)
  171. printf("R%d,%2d %08x,%08x\n", i, i + 8, sh2->r[i], sh2->r[i + 8]);
  172. printf("gb,vb %08x,%08x\n", sh2->gbr, sh2->vbr);
  173. printf("IRQs/mask: %02x/%02x\n", Pico32x.sh2irqi[sh2->is_slave],
  174. Pico32x.sh2irq_mask[sh2->is_slave]);
  175. printf("cycles %d/%d (%d)\n", sh2->cycles_done, sh2->cycles_aim, (signed int)sh2->sr >> 12);
  176. }
  177. #endif
  178. return CMDRET_DONE;
  179. }
  180. static int do_step_all(struct pdb_cpu *cpu, const char *args)
  181. {
  182. char tmp[32];
  183. if (!get_arg(tmp, sizeof(tmp), args)) {
  184. printf("step_all: missing arg\n");
  185. return CMDRET_DONE;
  186. }
  187. pdb_global_icount = atoi(tmp);
  188. return CMDRET_CONT_DO_NEXT;
  189. }
  190. static int do_continue(struct pdb_cpu *cpu, const char *args)
  191. {
  192. char tmp[32];
  193. if (get_arg(tmp, sizeof(tmp), args))
  194. cpu->icount = atoi(tmp);
  195. return CMDRET_CONT_DO_NEXT;
  196. }
  197. static int do_step(struct pdb_cpu *cpu, const char *args)
  198. {
  199. cpu->icount = 1;
  200. return do_continue(cpu, args);
  201. }
  202. static int do_waitcpu(struct pdb_cpu *cpu, const char *args)
  203. {
  204. char tmp[32];
  205. if (!get_arg(tmp, sizeof(tmp), args)) {
  206. printf("waitcpu: missing arg\n");
  207. return CMDRET_DONE;
  208. }
  209. if (strcmp(tmp, cpu->name) == 0)
  210. return CMDRET_DONE;
  211. return CMDRET_CONT_REDO;
  212. }
  213. static int do_help(struct pdb_cpu *cpu, const char *args);
  214. static struct {
  215. const char *cmd;
  216. const char *help;
  217. int (*handler)(struct pdb_cpu *cpu, const char *args);
  218. } pdb_cmds[] = {
  219. { "help", "", do_help },
  220. { "continue", "[insns]", do_continue },
  221. { "step", "[insns]", do_step },
  222. { "step_all", "<insns>", do_step_all },
  223. { "waitcpu", "<cpuname>", do_waitcpu },
  224. { "print", "", do_print },
  225. };
  226. static int do_help(struct pdb_cpu *cpu, const char *args)
  227. {
  228. int i;
  229. for (i = 0; i < ARRAY_SIZE(pdb_cmds); i++)
  230. printf("%s %s\n", pdb_cmds[i].cmd, pdb_cmds[i].help);
  231. return CMDRET_DONE;
  232. }
  233. static int do_comands(struct pdb_cpu *cpu, const char *cmds)
  234. {
  235. const char *p = cmds;
  236. while (p != NULL)
  237. {
  238. const char *pcmd;
  239. char cmd[32];
  240. int i, len;
  241. int ret = 0;
  242. pcmd = p;
  243. p = get_token(cmd, sizeof(cmd), p);
  244. if (p == NULL)
  245. break;
  246. if (cmd[0] == ';')
  247. continue;
  248. len = strlen(cmd);
  249. for (i = 0; i < ARRAY_SIZE(pdb_cmds); i++)
  250. if (strncmp(pdb_cmds[i].cmd, cmd, len) == 0) {
  251. ret = pdb_cmds[i].handler(cpu, p);
  252. break;
  253. }
  254. if (i == ARRAY_SIZE(pdb_cmds)) {
  255. printf("bad cmd: %s\n", cmd);
  256. break;
  257. }
  258. // skip until next command
  259. while (1) {
  260. p = get_token(cmd, sizeof(cmd), p);
  261. if (p == NULL || cmd[0] == ';')
  262. break;
  263. }
  264. pdb_event_cmds[0] = 0;
  265. if (ret == CMDRET_CONT_DO_NEXT) {
  266. pdb_pending_cmds[0] = 0;
  267. if (p != NULL)
  268. strcpy(pdb_event_cmds, p);
  269. return 0;
  270. }
  271. if (ret == CMDRET_CONT_REDO) {
  272. if (pcmd != pdb_pending_cmds)
  273. strncpy(pdb_pending_cmds, pcmd, sizeof(pdb_pending_cmds));
  274. return 0;
  275. }
  276. pdb_pending_cmds[0] = 0;
  277. }
  278. return 1;
  279. }
  280. static void do_prompt(struct pdb_cpu *cpu)
  281. {
  282. static char prev[128];
  283. int ret;
  284. while (1) {
  285. char *line, *cline;
  286. line = my_readline("(pdb) ");
  287. if (line == NULL)
  288. break;
  289. if (line[0] == 0)
  290. cline = prev;
  291. else {
  292. cline = line;
  293. strncpy(prev, line, sizeof(prev));
  294. }
  295. ret = do_comands(cpu, cline);
  296. free(line);
  297. if (ret == 0)
  298. break;
  299. }
  300. }
  301. void pdb_register_cpu(void *context, int type, const char *name)
  302. {
  303. int i = pdb_cpu_count;
  304. memset(&pdb_cpus[i], 0, sizeof(pdb_cpus[i]));
  305. pdb_cpus[i].context = context;
  306. pdb_cpus[i].type = type;
  307. pdb_cpus[i].id = pdb_cpu_count;
  308. pdb_cpus[i].name = name;
  309. pdb_cpus[i].icount = -1;
  310. pdb_cpu_count++;
  311. }
  312. void pdb_step(void *context, unsigned int pc)
  313. {
  314. struct pdb_cpu *cpu = context2cpu(context);
  315. int i;
  316. if (pdb_net_send(cpu, pc) < 0)
  317. goto prompt;
  318. if (pdb_pending_cmds[0] != 0)
  319. if (do_comands(cpu, pdb_pending_cmds))
  320. goto prompt;
  321. // breakpoint?
  322. for (i = 0; i < cpu->bpt_count; i++)
  323. if (cpu->bpts[i] == pc)
  324. goto prompt;
  325. // hit num of insns?
  326. if (pdb_global_icount > 0)
  327. if (--pdb_global_icount == 0)
  328. goto prompt;
  329. if (cpu->icount > 0)
  330. if (--(cpu->icount) == 0)
  331. goto prompt;
  332. return;
  333. prompt:
  334. if (pdb_event_cmds[0] != 0)
  335. if (!do_comands(cpu, pdb_event_cmds))
  336. return;
  337. printf("%s @%08x\n", cpu->name, pc);
  338. do_prompt(cpu);
  339. }
  340. void pdb_command(const char *cmd)
  341. {
  342. strncpy(pdb_pending_cmds, cmd, sizeof(pdb_pending_cmds));
  343. pdb_pending_cmds[sizeof(pdb_pending_cmds) - 1] = 0;
  344. }
  345. void pdb_cleanup(void)
  346. {
  347. pdb_cpu_count = 0;
  348. }
  349. // vim:shiftwidth=2:expandtab