comparison gdb_remote.c @ 487:c08a4efeee7f opengl

Update opengl branch from default. Fix build breakage unrelated to merge
author Mike Pavone <pavone@retrodev.com>
date Sat, 26 Oct 2013 22:38:47 -0700
parents 140af5509ce7
children b7b7a1cab44a
comparison
equal deleted inserted replaced
449:7696d824489d 487:c08a4efeee7f
1 /*
2 Copyright 2013 Michael Pavone
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.
5 */
6 #include "blastem.h"
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <stddef.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12
13 #define INITIAL_BUFFER_SIZE 4096
14
15 char * buf = NULL;
16 char * curbuf = NULL;
17 size_t bufsize;
18 int cont = 0;
19 int expect_break_response=0;
20 uint32_t resume_pc;
21
22 void gdb_debug_enter(genesis_context * gen, uint32_t pc)
23 {
24 resume_pc = pc;
25 while(!cont)
26 {
27 }
28 cont = 0;
29 }
30
31 void gdb_run_command(genesis_context * gen, char * command)
32 {
33 switch(*command)
34 {
35 case 'c':
36 if (*(command+1) != 0) {
37 resume_pc =
38 }
39 cont = 1;
40 expect_break_response = 1;
41 break;
42 case 's':
43
44 }
45 }
46
47 void gdb_run_commands(genesis_context * gen)
48 {
49 int enter_debugger = 0;
50 char * cur = buf;
51 while(cur < curbuf);
52 {
53 if(*cur == '$') {
54 cur++
55 char * start = cur;
56 while (cur < curbuf && *cur != '#') {
57 cur++;
58 }
59 if (*cur == '#') {
60 //check to make sure we've received the checksum bytes
61 if (curbuf-cur >= 2) {
62 //TODO: verify checksum
63 //Null terminate payload
64 //send acknowledgement
65 write(FILENO_STDOUT, "+", 1);
66 gdb_run_command(genesis_context * gen, start);
67 cur += 2;
68 } else {
69 cur = start - 1;
70 break;
71 }
72 } else {
73 cur = start - 1;
74 break;
75 }
76 } else {
77 if (*cur == 0x03) {
78 enter_debugger = 1;
79 }
80 cur++;
81 }
82 }
83
84 //FIXME
85 if (consumed == curbuf-buf) {
86 curbuf = buf;
87 } else if (consumed > 0) {
88 memmove(buf, buf + consumed, curbuf - buf - consumed);
89 curbuf -= consumed;
90 }
91 }
92
93 void gdb_command_poll(genesis_context * gen)
94 {
95 for(;;)
96 {
97 if (curbuf == buf + bufsize) {
98 //buffer is full, expand it
99 bufsize *= 2;
100 buf = realloc(buf, bufsize);
101 if (!buf) {
102 fprintf(stderr, "Failed to grow GDB command buffer to %d bytes\n", (int)bufsize);
103 exit(1);
104 }
105 curbuf = buf + bufsize/2;
106 }
107 int numread = read(STDIN_FILENO, buf, bufsize - (curbuf-buf));
108 if (numread < 0) {
109 if (errno == EAGAIN || errno == EWOULDBLOCK) {
110 return;
111 } else {
112 fprintf(stderr, "Error %d while reading GDB commands from stdin", errno);
113 exit(1);
114 }
115 } else if (numread == 0) {
116 exit(0);
117 }
118 gdb_run_commands(genesis_context * gen);
119 }
120 }
121
122 void gdb_remote_init()
123 {
124 fcntl(STDIN_FILENO, FD_SETFL, O_NONBLOCK);
125 buf = malloc(INITIAL_BUFFER_SIZE);
126 curbuf = buf;
127 bufzie = INITIAL_BUFFER_SIZE;
128 }