comparison gdb_remote.c @ 802:6811f601008f

Old changes for GDB remote debugging on Windows I forgot to commit
author Michael Pavone <pavone@retrodev.com>
date Sun, 26 Jul 2015 16:48:25 -0700
parents 9324f721efa6
children 236a184bf6f0
comparison
equal deleted inserted replaced
742:2e1b3b258523 802:6811f601008f
1 /* 1 /*
2 Copyright 2013 Michael Pavone 2 Copyright 2013 Michael Pavone
3 This file is part of BlastEm. 3 This file is part of BlastEm.
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text. 4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
5 */ 5 */
6 #ifdef _WIN32
7 #define WINVER 0x501
8 #include <winsock2.h>
9 #include <ws2tcpip.h>
10
11 int gdb_sock;
12 #define GDB_IN_FD gdb_sock
13 #define GDB_OUT_FD gdb_sock
14 #define GDB_READ(fd, buf, bufsize) recv(fd, buf, bufsize, 0)
15 #define GDB_WRITE(fd, buf, bufsize) send(fd, buf, bufsize, 0)
16 #else
17 #define GDB_IN_FD STDIN_FILENO
18 #define GDB_OUT_FD STDOUT_FILENO
19 #define GDB_READ read
20 #define GDB_WRITE write
21 #endif
22
6 #include "gdb_remote.h" 23 #include "gdb_remote.h"
7 #include "68kinst.h" 24 #include "68kinst.h"
8 #include "debug.h" 25 #include "debug.h"
9 #include <unistd.h> 26 #include <unistd.h>
10 #include <fcntl.h> 27 #include <fcntl.h>
11 #include <stddef.h> 28 #include <stddef.h>
12 #include <stdlib.h> 29 #include <stdlib.h>
13 #include <stdio.h> 30 #include <stdio.h>
14 #include <string.h> 31 #include <string.h>
15 32
33
34
16 #define INITIAL_BUFFER_SIZE (16*1024) 35 #define INITIAL_BUFFER_SIZE (16*1024)
17 36
18 #ifdef DO_DEBUG_PRINT 37 #ifdef DO_DEBUG_PRINT
19 #define dfprintf fprintf 38 #define dfprintf fprintf
20 #else 39 #else
73 hex_8(checksum, out); 92 hex_8(checksum, out);
74 } 93 }
75 94
76 void write_or_die(int fd, const void *buf, size_t count) 95 void write_or_die(int fd, const void *buf, size_t count)
77 { 96 {
78 if (write(fd, buf, count) < count) { 97 if (GDB_WRITE(fd, buf, count) < count) {
79 fputs("Error writing to stdout\n", stderr); 98 fputs("Error writing to stdout\n", stderr);
80 exit(1); 99 exit(1);
81 } 100 }
82 } 101 }
83 102
84 void gdb_send_command(char * command) 103 void gdb_send_command(char * command)
85 { 104 {
86 char end[3]; 105 char end[3];
87 write_or_die(STDOUT_FILENO, "$", 1); 106 write_or_die(GDB_OUT_FD, "$", 1);
88 write_or_die(STDOUT_FILENO, command, strlen(command)); 107 write_or_die(GDB_OUT_FD, command, strlen(command));
89 end[0] = '#'; 108 end[0] = '#';
90 gdb_calc_checksum(command, end+1); 109 gdb_calc_checksum(command, end+1);
91 write_or_die(STDOUT_FILENO, end, 3); 110 write_or_die(GDB_OUT_FD, end, 3);
92 dfprintf(stderr, "Sent $%s#%c%c\n", command, end[1], end[2]); 111 dfprintf(stderr, "Sent $%s#%c%c\n", command, end[1], end[2]);
93 } 112 }
94 113
95 uint32_t calc_status(m68k_context * context) 114 uint32_t calc_status(m68k_context * context)
96 { 115 {
144 word = context->mem_pointers[1] + (address & 0xFFFF)/2; 163 word = context->mem_pointers[1] + (address & 0xFFFF)/2;
145 } else if (address >= 0xA00000 && address < 0xA04000) { 164 } else if (address >= 0xA00000 && address < 0xA04000) {
146 z80_ram[address & 0x1FFF] = value; 165 z80_ram[address & 0x1FFF] = value;
147 genesis_context * gen = context->system; 166 genesis_context * gen = context->system;
148 #ifndef NO_Z80 167 #ifndef NO_Z80
149 z80_handle_code_write(address & 0x1FFF, gen->z80); 168 z80_handle_code_GDB_WRITE(address & 0x1FFF, gen->z80);
150 #endif 169 #endif
151 return; 170 return;
152 } else { 171 } else {
153 return; 172 return;
154 } 173 }
485 cont = 0; 504 cont = 0;
486 uint8_t partial = 0; 505 uint8_t partial = 0;
487 while(!cont) 506 while(!cont)
488 { 507 {
489 if (!curbuf) { 508 if (!curbuf) {
490 int numread = read(STDIN_FILENO, buf, bufsize); 509 int numread = GDB_READ(GDB_IN_FD, buf, bufsize);
510 if (numread < 0) {
511 fputs("Failed to read on GDB input file descriptor\n", stderr);
512 exit(1);
513 }
514 dfprintf(stderr, "read %d bytes\n", numread);
491 curbuf = buf; 515 curbuf = buf;
492 end = buf + numread; 516 end = buf + numread;
493 } else if (partial) { 517 } else if (partial) {
494 if (curbuf != buf) { 518 if (curbuf != buf) {
495 memmove(curbuf, buf, end-curbuf); 519 memmove(curbuf, buf, end-curbuf);
496 end -= curbuf - buf; 520 end -= curbuf - buf;
497 } 521 }
498 int numread = read(STDIN_FILENO, end, bufsize - (end-buf)); 522 int numread = read(GDB_IN_FD, end, bufsize - (end-buf));
499 end += numread; 523 end += numread;
500 curbuf = buf; 524 curbuf = buf;
501 } 525 }
502 for (; curbuf < end; curbuf++) 526 for (; curbuf < end; curbuf++)
503 { 527 {
513 if (end-curbuf >= 2) { 537 if (end-curbuf >= 2) {
514 //TODO: verify checksum 538 //TODO: verify checksum
515 //Null terminate payload 539 //Null terminate payload
516 *curbuf = 0; 540 *curbuf = 0;
517 //send acknowledgement 541 //send acknowledgement
518 if (write(STDOUT_FILENO, "+", 1) < 1) { 542 if (GDB_WRITE(GDB_OUT_FD, "+", 1) < 1) {
519 fputs("Error writing to stdout\n", stderr); 543 fputs("Error writing to stdout\n", stderr);
520 exit(1); 544 exit(1);
521 } 545 }
522 gdb_run_command(context, pc, start); 546 gdb_run_command(context, pc, start);
523 curbuf += 2; 547 curbuf += 2;
536 } 560 }
537 } 561 }
538 return context; 562 return context;
539 } 563 }
540 564
565 #ifdef _WIN32
566 void gdb_cleanup(void)
567 {
568 WSACleanup();
569 }
570 WSADATA wsa_data;
571 #endif
572
541 void gdb_remote_init(void) 573 void gdb_remote_init(void)
542 { 574 {
543 buf = malloc(INITIAL_BUFFER_SIZE); 575 buf = malloc(INITIAL_BUFFER_SIZE);
544 curbuf = NULL; 576 curbuf = NULL;
545 bufsize = INITIAL_BUFFER_SIZE; 577 bufsize = INITIAL_BUFFER_SIZE;
546 } 578 #ifdef _WIN32
579 WSAStartup(MAKEWORD(2,2), &wsa_data);
580
581 struct addrinfo request, *result;
582 memset(&request, 0, sizeof(request));
583 request.ai_family = AF_UNSPEC;
584 request.ai_socktype = SOCK_STREAM;
585 request.ai_flags = AI_PASSIVE;
586 getaddrinfo(NULL, "1234", &request, &result);
587
588 int listen_sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
589 if (listen_sock < 0) {
590 fputs("Failed to open GDB remote debugging socket", stderr);
591 exit(1);
592 }
593 if (bind(listen_sock, result->ai_addr, result->ai_addrlen) < 0) {
594 fputs("Failed to bind GDB remote debugging socket", stderr);
595 exit(1);
596 }
597 if (listen(listen_sock, 1) < 0) {
598 fputs("Failed to listen on GDB remote debugging socket", stderr);
599 exit(1);
600 }
601 gdb_sock = accept(listen_sock, NULL, NULL);
602 if (gdb_sock < 0) {
603 fputs("accpet returned an error while listening on GDB remote debugging socket", stderr);
604 exit(1);
605 }
606 closesocket(listen_sock);
607 #endif
608 }