-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtc64monitor.c
executable file
·296 lines (238 loc) · 7.42 KB
/
tc64monitor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
tc64monitor
Copyright (C) 2020 Filippo Bergamasco
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <errno.h>
#include "tc64monitor.h"
#undef VERBOSE
#define VERSION "0.1"
void* c64mem = 0;
int pid = 0;
unsigned long int str2hex( char* str, int strlen ) {
unsigned long int val=0;
while( *str && strlen-- ) {
val *= 16;
if( *str >= 'A' && *str<='F' ) {
val += (*str - 'A')+10;
}
else if( *str >= 'a' && *str<='f' ) {
val += (*str - 'a')+10;
}
else if( *str >= '0' && *str<='9' ) {
val += (*str - '0');
}
else {
printf("Invalid HEX: %c\n", *str );
return 0;
}
++str;
}
return val;
}
void dopoke()
{
yylex();
if( t.type != HEXNUM )
{
printf("Error: address expected\n");
return;
}
unsigned long int addr = str2hex( t.str, t.len );
void* c64memaddr = c64mem + addr;
#ifdef VERBOSE
printf("POKE at addr $%04x (0x%lx)\n", addr,c64memaddr );
#endif
unsigned char buff[256]={0};
size_t bufflen=0;
yylex();
do
{
if( t.type != HEXBYTE )
{
printf("Error: byte expected\n");
return;
}
buff[bufflen++] = (unsigned char)str2hex( t.str, t.len );
yylex();
} while( t.type != ENDCOMMAND && bufflen<256 );
#ifdef VERBOSE
printf("Poking %s\n", buff);
#endif
// Build iovec structs
struct iovec local[1];
local[0].iov_base = buff;
local[0].iov_len = bufflen;
struct iovec remote[1];
remote[0].iov_base = c64memaddr;
remote[0].iov_len = bufflen;
// Call process_vm_readv - handle any error codes
ssize_t nwrite = process_vm_writev(pid, local, 1, remote, 1, 0);
if (nwrite < 0) {
switch (errno) {
case EINVAL:
printf("ERROR: INVALID ARGUMENTS.\n");
break;
case EFAULT:
printf("ERROR: UNABLE TO ACCESS TARGET MEMORY ADDRESS.\n");
break;
case ENOMEM:
printf("ERROR: UNABLE TO ALLOCATE MEMORY.\n");
break;
case EPERM:
printf("ERROR: INSUFFICIENT PRIVILEGES TO TARGET PROCESS.\n");
break;
case ESRCH:
printf("ERROR: PROCESS DOES NOT EXIST.\n");
break;
default:
printf("ERROR: AN UNKNOWN ERROR HAS OCCURRED.\n");
}
return;
}
}
void dopeek()
{
yylex();
if( t.type != HEXNUM )
{
printf("Error: address expected\n");
return;
}
unsigned long int addr = str2hex( t.str, t.len );
void* c64memaddr = c64mem + addr;
#ifdef VERBOSE
printf("PEEK at addr $%04x (0x%lx)\n", addr,c64memaddr );
#endif
yylex();
if( t.type != INTEGER )
{
printf("Error: num bytes expected after address\n");
return;
}
int nbytes = atoi( t.str );
#ifdef VERBOSE
printf("Want to read %d bytes\n",nbytes);
#endif
do
{
yylex();
} while( t.type != ENDCOMMAND );
// Build iovec structs
struct iovec local[1];
local[0].iov_base = calloc(nbytes, sizeof(char));
local[0].iov_len = nbytes;
struct iovec remote[1];
remote[0].iov_base = c64memaddr;
remote[0].iov_len = nbytes;
ssize_t nread = process_vm_readv(pid, local, 1, remote, 1, 0);
if (nread < 0) {
switch (errno) {
case EINVAL:
printf("ERROR: INVALID ARGUMENTS.\n");
break;
case EFAULT:
printf("ERROR: UNABLE TO ACCESS TARGET MEMORY ADDRESS.\n");
break;
case ENOMEM:
printf("ERROR: UNABLE TO ALLOCATE MEMORY.\n");
break;
case EPERM:
printf("ERROR: INSUFFICIENT PRIVILEGES TO TARGET PROCESS.\n");
break;
case ESRCH:
printf("ERROR: PROCESS DOES NOT EXIST.\n");
break;
default:
printf("ERROR: AN UNKNOWN ERROR HAS OCCURRED.\n");
}
free( local[0].iov_base );
return;
}
unsigned char* data = local[0].iov_base;
int ii;
for( ii=0; ii<nbytes; ++ii )
{
printf("%02x%c",data[ii],ii==nbytes-1?'\n':' ');
}
free( local[0].iov_base );
}
int main( int argc, char* argv[] )
{
printf("\n");
printf("***** tc64monitor *** v %s \n",VERSION);
printf("***************************** \n");
printf(" Filippo Bergamasco 2020 \n\n");
if( argc != 5 )
{
printf("Usage: \n");
printf("%s <pid> <base_address> <offset> <c64addr>\n",argv[0]);
return 0;
}
pid = atoi( argv[1] );
printf(" PID: %d\n",pid);
void* base_address = (void*)str2hex( argv[2], strlen(argv[2]));
printf("BASE ADDR: 0x%lx\n", (unsigned long int)base_address );
unsigned long int offset = str2hex( argv[3], strlen(argv[3]));
printf(" OFFSET: 0x%lx\n", (unsigned long int)offset );
unsigned long int c64addr = str2hex( argv[4], strlen(argv[4]));
printf(" C64ADDR: 0x%lx\n", (unsigned long int)c64addr );
c64mem = base_address + offset - c64addr;
printf(" C64MEM: 0x%lx\n", (unsigned long int)c64mem );
printf("Type ? for help.\n");
while( 1 )
{
printf("> ");
fflush(0);
yylex();
switch( t.type ) {
case COMMAND_PEEK:
{
dopeek();
break;
}
case COMMAND_POKE:
{
dopoke();
break;
}
case COMMAND_HELP:
{
printf("Available commands:\n");
printf(" M <addr> +n PEEK n bytes from C64 memory starting at <addr>\n");
printf(" : <addr> <byte1> <byte2> ... <byten> POKE byte1..byten to C64 memory starting at <addr>\n");
printf(" ? Show this help\n");
printf(" q Quit\n");
printf("\n");
printf("<addr> is a 16bit hex address (in C64 memory space) in the form 0x0000..0xFFFF or $0000..$FFFF\n");
printf("<byte> is an HEX number of the form 00,01,..,FF\n");
break;
}
case COMMAND_QUIT:
{
printf("Bye.\n");
exit(0);
}
case ENDCOMMAND:
{
break;
}
default: printf("Error: command expected. Type ? for help\n");
}
} // main loop
return 0;
}