-
Notifications
You must be signed in to change notification settings - Fork 13
/
machine_mon.c
475 lines (419 loc) · 12.2 KB
/
machine_mon.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/**
* \file machine_mon.c
* \brief Machine memory monitor
* \author Antoine Fraboulet
* \date 2009
**/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "arch/common/hardware.h"
#include "devices/devices.h"
#include "machine/machine.h"
#include "libelf/libelf.h"
#include "libetrace/libetrace.h"
#include "liblogger/logger.h"
#include "machine.h"
#include "machine_mon.h"
static struct watchpoint_t watchpoint[MONITOR_MAX_WATCHPOINT];
static int watchpoint_max;
/* ************************************************** */
/* ************************************************** */
/* ************************************************** */
static char* mode2str(int mode)
{
switch (mode) {
case MAC_WATCH_READ: return "r";
case MAC_WATCH_WRITE: return "w";
case (MAC_WATCH_READ | MAC_WATCH_WRITE): return "rw";
default: return "xx";
}
}
void machine_monitor_print()
{
int i;
OUTPUT("==\n");
OUTPUT("Memory monitor\n");
for(i=0; i<watchpoint_max; i++)
{
OUTPUT(" monitor: %s 0x%04x:%d %2s mod %d %d\n",
watchpoint[i].name,
watchpoint[i].addr,
watchpoint[i].size,
mode2str(watchpoint[i].mode),
machine.state->watchpoint_modify_on_first_write[i],
watchpoint[i].modify_value
);
}
OUTPUT("==\n");
}
/* ************************************************** */
/* ************************************************** */
/* ************************************************** */
void machine_monitor_vcd_start(void)
{
int i;
for(i=0; i<watchpoint_max; i++)
{
char mns[MONITOR_MAX_VARIABLE_NAME+2];
snprintf(mns,MONITOR_MAX_VARIABLE_NAME+2,"%s,%s",
watchpoint[i].name,
mode2str(watchpoint[i].mode));
watchpoint[i].trc_id_val = tracer_event_add_id(watchpoint[i].size * 8,
watchpoint[i].name,
"monitor" );
watchpoint[i].trc_id_rw = tracer_event_add_id(2,mns,"monitor");
mcu_ramctl_set_bp(watchpoint[i].addr,watchpoint[i].mode);
}
}
/* ************************************************** */
/* ************************************************** */
/* ************************************************** */
void machine_monitor_vcd_stop(void)
{
int i;
for(i=0; i<watchpoint_max; i++)
{
mcu_ramctl_unset_bp(watchpoint[i].addr,watchpoint[i].mode);
}
}
/* ************************************************** */
/* ************************************************** */
/* ************************************************** */
void machine_monitor_init(elf32_t UNUSED elf)
{
int i;
for(i=0; i<MONITOR_MAX_WATCHPOINT; i++)
{
memset(& watchpoint[i], 0, sizeof(struct watchpoint_t));
watchpoint[i].size = -1;
}
watchpoint_max = 0;
}
void machine_monitor_start (void)
{
machine_monitor_print();
machine_monitor_vcd_start();
}
void machine_monitor_stop (void)
{
machine_monitor_vcd_stop();
}
/* ************************************************** */
/* ************************************************** */
/* ************************************************** */
int machine_monitor_find_by_addr(uint32_t addr)
{
int i;
for(i=0; i<watchpoint_max; i++)
{
if ((addr >= (watchpoint[i].addr)) &&
(addr < (watchpoint[i].addr + watchpoint[i].size)))
{
return i;
}
}
return -1;
}
void machine_monitor_add_trace(int access_type)
{
int index = -1;
uint32_t addr = MCU_RAMCTL_ADDR;
uint32_t value;
if ((index = machine_monitor_find_by_addr( addr )) == -1)
{
WARNING("machine:monitor: watchpoint trace undefined for address 0x%04x\n",addr);
return;
}
switch (watchpoint[index].size)
{
case 0:
value = mcu_jtag_read_byte(addr);
break;
case 1:
value = mcu_jtag_read_byte(addr);
break;
case 2:
value = mcu_jtag_read_word(addr & ~1); /* align read */
break;
default:
value = mcu_jtag_read_word(addr & ~1); /* align read */
break;
}
#if 0
/* *******************************
* available information example
*
*/
INFO("machine:monitor: watchpoint trace for addr 0x%04x pc=0x%04x val=0x%04x type=%s\n",
addr, mcu_get_pc(), value, mode2str(access_type));
#endif
#if 0
/* *******************************
* example on how to set a breakpoint on value write (debugger write watchpoint)
*
*/
if ((strcmp(watchpoint[ index ].name,"my_watchpoint_name") == 0) ||
(addr == my_watchpoint_address))
{
if (value == my_breakpoint_value)
{
mcu_signal_add(SIG_MAC | MAC_TO_SIG(MAC_WATCH_WRITE));
}
}
#endif
switch (access_type)
{
case MAC_WATCH_READ:
tracer_event_record(watchpoint[ index ].trc_id_val,value);
tracer_event_record_force(watchpoint[ index ].trc_id_rw,1);
break;
case MAC_WATCH_WRITE:
tracer_event_record(watchpoint[ index ].trc_id_val,value);
tracer_event_record_force(watchpoint[ index ].trc_id_rw,2);
break;
default:
/* do nothing */
break;
}
if (((access_type | MAC_WATCH_WRITE) != 0) &&
(machine.state->watchpoint_modify_on_first_write[ index ]))
{
int v_addr = watchpoint[index].addr;
int v_size = watchpoint[index].size;
int v_value = watchpoint[index].modify_value;
machine.state->watchpoint_modify_on_first_write[ index ] = 0;
switch (v_size) {
case 0:
ERROR(" modify: variable \"%s\" [0x%04x] has size 0\n",
watchpoint[index].name, v_addr);
break;
case 1:
mcu_jtag_write_byte(v_addr, v_value);
INFO(" modify:byte variable \"%s\" [0x%04x:%d] = %d (0x%04x)\n",
watchpoint[index].name, v_addr, v_size, v_value, v_value);
break;
case 2:
mcu_jtag_write_word(v_addr, v_value);
INFO(" modify:word variable \"%s\" [0x%04x:%d] = %d (0x%04x)\n",
watchpoint[index].name, v_addr, v_size, v_value, v_value);
break;
default:
ERROR(" modify: variable \"%s\" [0x%04x] has a size > 2\n",
watchpoint[index].name, v_addr);
break;
}
}
/*
* Log messages from application: see svn:wsim/utils/wsimlog/WSimLog.h
*/
if (((access_type | MAC_WATCH_WRITE) != 0) &&
(strcmp(watchpoint[ index ].name,"__WSIMLOGBUFFER") == 0))
{
int iter;
uint16_t int16;
char *logString = (char*)calloc( watchpoint[ index ].size, sizeof(char));
for(iter=0 ; iter<watchpoint[ index ].size ; iter++)
{
logString[iter] = (char)mcu_jtag_read_byte(addr+iter);
if (logString[iter]=='\0')
break;
}
switch (logString[0])
{
case 0 :
case '0': // init (silent)
break;
case '1': // char
INFO("machine:monitor(%d): Variable:%s at:0x%04x (char) = '%c'\n",
logString[0], watchpoint[ index ].name,watchpoint[ index ].addr,logString[1]);
break;
case '2': // Byte (int8)
INFO("machine:monitor(%d): Variable:%s at:0x%04x (int8) = '%d'\n",
logString[0], watchpoint[ index ].name,watchpoint[ index ].addr,logString[1]);
break;
case '3': // Int (int16)
int16 = ((logString[2] & 0xff) << 8) | (logString[1] & 0xff);
INFO("machine:monitor(%d): Variable:%s at:0x%04x (int16) = '%d'\n",
logString[0], watchpoint[ index ].name,watchpoint[ index ].addr,int16);
break;
case '4': // string
INFO("machine:monitor(%d): Variable:%s at:0x%04x (str) = '%s'\n",
logString[0], watchpoint[ index ].name,watchpoint[ index ].addr,&logString[1]);
break;
default:
INFO("machine:monitor(%d): Variable:%s at:0x%04x (Unknown type) = '%s'\n",
logString[0], watchpoint[ index ].name,watchpoint[ index ].addr,iter,logString);
break;
}
free(logString);
}
}
/* ************************************************** */
/* ************************************************** */
/* ************************************************** */
void machine_monitor_set(char* args, elf32_t elf)
{
/* --monitor=0xAAAA:size:rw,symbol:rw ... */
char delim1[] = ",";
char delim2[] = ":";
char *str1,*str2;
char *token,*subtoken;
char *saveptr1,*saveptr2;
int j;
// OUTPUT("==\n");
// OUTPUT("Memory monitor = %s\n",args);
for (j = 1, str1 = args; ; j++, str1 = NULL)
{
token = strtok_r(str1, delim1, &saveptr1);
if (token == NULL)
break;
str2 = token;
/* NAME or HEX */
if ((subtoken = strtok_r(str2, delim2, &saveptr2)) == NULL)
{
continue;
}
strncpyz(watchpoint[watchpoint_max].name,subtoken, MONITOR_MAX_VARIABLE_NAME);
/* HEXA:SIZE */
if (subtoken[0] == '0')
{
sscanf(subtoken, "0x%"SCNx32,& watchpoint[watchpoint_max].addr);
if ((subtoken = strtok_r(NULL, delim2, &saveptr2)) != NULL)
{
sscanf(subtoken, "%d", & watchpoint[watchpoint_max].size );
}
}
/* NAME */
else
{
if (elf != NULL)
{
watchpoint[watchpoint_max].addr = libelf_symtab_find_addr_by_name(elf, subtoken );
watchpoint[watchpoint_max].size = libelf_symtab_find_size_by_name(elf, subtoken );
}
else
{
ERROR("monitor: cannot find symbol %s, no elf file given\n", subtoken);
}
}
if ((watchpoint[watchpoint_max].addr == 0) &&
(watchpoint[watchpoint_max].size == -1))
{
ERROR("monitor: cannot find symbol \"%s\"\n",subtoken);
continue;
}
else
{
/* RW */
if ((subtoken = strtok_r(NULL, delim2, &saveptr2)) == NULL)
{
//continue;
}
else
{
char c0 = tolower((unsigned char)subtoken[0]);
char c1 = tolower((unsigned char)subtoken[1]);
if (c0 == 'r' || c1 == 'r')
watchpoint[watchpoint_max].mode |= MAC_WATCH_READ;
if (c0 == 'w' || c1 == 'w')
watchpoint[watchpoint_max].mode |= MAC_WATCH_WRITE;
#if 0
OUTPUT("machine:monitor:add name %s addr %x size %d mode %c%c\n",
watchpoint[watchpoint_max].name,
watchpoint[watchpoint_max].addr,
watchpoint[watchpoint_max].size,
c0?c0:'.',c1?c1:'.');
#endif
watchpoint_max ++;
}
}
}
// OUTPUT("==\n");
}
/* ************************************************** */
/* ************************************************** */
/* ************************************************** */
void machine_modify_set(char* args, elf32_t elf)
{
/* --modify=0xAAAA:size:value,symbol:value ... */
char delim1[] = ",";
char delim2[] = ":";
char *str1,*str2;
char *token,*subtoken;
char *saveptr1,*saveptr2;
int j;
char v_name[MONITOR_MAX_VARIABLE_NAME];
int v_size, v_addr, v_value;
// OUTPUT("==\n");
// OUTPUT("Memory modify = %s\n",args);
for (j = 1, str1 = args; ; j++, str1 = NULL)
{
token = strtok_r(str1, delim1, &saveptr1);
if (token == NULL)
break;
str2 = token;
/* NAME or hex */
if ((subtoken = strtok_r(str2, delim2, &saveptr2)) == NULL)
{
continue;
}
strncpyz( v_name, subtoken, MONITOR_MAX_VARIABLE_NAME);
v_addr = 0;
v_size = -1;
/* HEXA:SIZE */
if (subtoken[0] == '0')
{
sscanf(subtoken, "0x%"SCNx32, &v_addr );
if ((subtoken = strtok_r(NULL, delim2, &saveptr2)) != NULL)
{
sscanf(subtoken, "%d", & v_size );
}
}
/* NAME */
else
{
if (elf)
{
v_addr = libelf_symtab_find_addr_by_name(elf, subtoken );
v_size = libelf_symtab_find_size_by_name(elf, subtoken );
}
else
{
ERROR("monitor: cannot find symbol %s, no elf file given\n", subtoken);
}
}
if ((v_addr == 0) &&
(v_size == -1))
{
ERROR("monitor: cannot find symbol \"%s\"\n",subtoken);
}
/* VALUE */
if ((subtoken = strtok_r(NULL, delim2, &saveptr2)) == NULL)
{
continue;
}
sscanf(subtoken, "%d", & v_value );
int index = -1;
if ((index = machine_monitor_find_by_addr(v_addr)) == -1)
{
index = watchpoint_max++;
strncpyz(watchpoint[ index ].name, v_name, MONITOR_MAX_VARIABLE_NAME);
watchpoint[ index ].addr = v_addr;
watchpoint[ index ].size = v_size;
watchpoint[ index ].mode = MAC_WATCH_WRITE;
OUTPUT("Memory modify = %s, add index %d\n",watchpoint[ index ].name,index);
}
else
{
OUTPUT("Memory modify = %s, index %d\n",watchpoint[ index ].name,index);
}
machine.state->watchpoint_modify_on_first_write[index] = 1;
watchpoint[index].modify_value = v_value;
}
// OUTPUT("==\n");
}
/* ************************************************** */
/* ************************************************** */
/* ************************************************** */