examineMem.c
20.5 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
#include <sys/types.h>
#ifdef __sgi__
#include <sys/sbd.h>
#endif
#include <sys/stat.h>
#include <sys/mman.h>
#ifdef __sgi__
#include <sys/sema.h>
#endif
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <getopt.h>
/*
* From $ROOT/usr/include/ide, which is installed from $ROOT/PR/diags/include
*/
#include "diag.h"
#include <dbg_comm.h>
#define EXAMINE_MEM_TEST_BASE 10
#define NODATA -1
#define SUCCESS 0
#define FAILURE 1
#define LINEBUFSIZE 256
#define EIGHT_BITS 2 /* 2 hex digits represent an 8 bit datum */
#define SIXTEEN_BITS 4 /* 4 hex digits represent a 16 bit datum */
#define THIRTY_TWO_BITS 8 /* 8 hex digits represent a 32 bit datum */
/*
* size constants for usage by the "e" command.
*/
#define BYTE 2
#define WORD 4
#define LONG 8
struct monGlobals{
unsigned char *lineptr; /* current pointer into line */
unsigned char line[256]; /* buffer for user input to monitor */
};
/*
* obviously stolen from somewhere else...
*/
struct monGlobals globalInput;
struct monGlobals *pGlobals;
static int examineMem(TEST_REF *test_ref);
/*
* Create an array of tests, each of which corresponds to a separate menu
* item callable from the master ide menu.
*/
static TEST_REF TestRefs[] = {
{"EXAMINE_MEM cmds", EXAMINE_MEM_TEST_BASE+0, examineMem},
{"",0,0}
};
static char *addr, *mask, *count;
static int NumFailures = 0;
static int ShortMsg = 0;
static int DebugLevel = 0;
static int examineMem_Init();
static int examineMem_Do(TEST_REF *test_ref);
/*
* diagnostic entry point:
*
* Each separately invokable ide diagnostic command corresponds to an
* independent ".c" module; the entry point herein must match
* the test name as specified in the commoncmd.awk script. These command
* names correspond to the names you see from the ide menu.
*
* We follow the 'dg' naming convention for this one globally accessible
* symbol (all other functions are statically declared within this module).
*/
int dgExamineMemEntry()
{
int i;
/*
* Take all arguments after the initial command character 'e', and
* stuff them into the current "line buffer", as maintained by the pGlobals
* struct.
*/
pGlobals = &globalInput;
pGlobals->lineptr = pGlobals->line;
*pGlobals->lineptr = '\0';
for (i = 1; i < pGlobalComm->argc; i++) {
strcat(pGlobals->lineptr, pGlobalComm->argv[i]);
/*
* Leave a space between each arg.
*/
strcat(pGlobals->lineptr, " ");
}
/*
* IDE will call our one-time initialization function examineMem_Init(),
* then invoke examineMem_Do() for as many tests as we've put into the
* global test array "TestRefs", declared at the top of this module.
*/
diaginit(TestRefs, examineMem_Init, examineMem_Do);
}
static int examineMem_Init()
{
char *env;
if (env = getenv("IDE_DEBUG_LEVEL")) DebugLevel = atoi(env);
if (env = getenv("IDE_SHORT_MSG")) ShortMsg = atoi(env);
NumFailures = 0;
/*
* Setup the diagnostic communication link to the target system.
*/
if ( dgInitComm() ) {
errlog(ERR_SEVERE,
"Unable to initialize communication with target system.\n");
pGlobalComm->entryNum = -1;
}
return(0);
}
static int examineMem_Do(TEST_REF *test_ref)
{
int rc;
/*
* Invoke the actual test from the "TEST_REF" array statically declared
* as a global within this test module.
*/
rc = test_ref->fnc(test_ref);
return(0);
}
/*
* This module contains Functions to support the "e" command (byte, word, long)
*
* Prints and/or modifies a location. The address and length (2, 4, or 8
* hex digits) are arguments. The result is 1 if the location was modified,
* 2 if it was not, but a cr was typed, and 0 if anything else was typed.
*
* FUNCTIONS DEFINED:
*
* examineMem() ... Initial command line interpreter (fetches opening address).
*
* getnum() ... Get a hex number from the global line buffer.
*
* ishex() ... Returns hex value of char or -1 if char is not hex.
*
* printhex() ... Prints a specified number of hex digits, given an int.
*
* readNprint() ... Reads a memory location, then prints out its contents.
*
* writeNprint()... Writes to a memory location, then prints out the new value.
*
* queryMem() ... Prints and/or modifies a memory location, then returns
* or prompts for more command line input.
*
* peekc() ... Does a getc() followed by an ungetc, and returns the
* available character (if any).
*/
int dontIncrement;/* If TRUE, address increment step should be skipped */
int decrementAddr;/* If TRUE, address will be decremented instead of inc'd */
/* Function: examineMem()
**
** Description: Memory peek/poke command line parsing function.
**
** Note: Once an examine memory command is initiated,
** the "queryMem" function begins prompting for (and parsing)
** command line input.
**
** Global variables used: pGlobals
**
** Parameters: TEST_REF *
**
** Returns: none.
*/
static int
examineMem(TEST_REF *test_ref)
{
int status;
int size = LONG;
register unsigned long *startAddr;
/*
* Parse length specifier (byte/word/long) and starting address.
*/
startAddr = ( (unsigned long *)
getonedec(&status, &pGlobals->lineptr) );
if ((status == FALSE) || (status == NODATA)) {
printf("Can't interpret address argument to e(xamine memory).\n");
printf("usage: e [addr][actions]\n");
return(0);
}
/*
* Eat any leading "+" characters (they have special meaning to queryMem()). */
skipWhite();
while(*pGlobals->lineptr == '+') {
pGlobals->lineptr++;
skipWhite();
}
/*
* Initialize these flags before calling queryMem. They get set
* and cleared by the command line parser in queryMem().
*/
dontIncrement = FALSE;
decrementAddr = FALSE;
while (queryMem(startAddr, size)) {
if (dontIncrement == FALSE) { /* set if user entered a "+" as an arg */
if (decrementAddr == FALSE) {
switch(size) {
case BYTE:
startAddr = (unsigned long *)((int)(startAddr) + 1);
break;
case WORD:
startAddr = (unsigned long *)((int)(startAddr) + 2);
break;
case LONG:
startAddr = (unsigned long *)((int)(startAddr) + 4);
break;
}
} else {
/*
* Let's go the other way...
*/
switch(size) {
case BYTE:
startAddr = (unsigned long *)((int)(startAddr) - 1);
break;
case WORD:
startAddr = (unsigned long *)((int)(startAddr) - 2);
break;
case LONG:
startAddr = (unsigned long *)((int)(startAddr) - 4);
break;
}
decrementAddr = FALSE; /* clear the flag each time thru loop */
}
}
}
return(0);
}
/* get a hex number */
int getnum()
{
register int v = 0;
register int hexval;
while ((hexval= ishex(*pGlobals->lineptr)) >=0) {
v= (v<<4)| hexval;
pGlobals->lineptr++;
}
return(v);
}
#define isnum(c) ((c>='0')&&(c<='9'))
/*
* Returns the hex value of a char or -1 if the char is not hex
*/
int ishex(c)
register unsigned char c;
{
if (isnum(c))
return(c - '0');
if (c >= 'a' && c <= 'f')
return(c - 'a' + 10);
if (c >= 'A' && c <= 'F')
return(c - 'A' + 10);
return(-1);
}
char hexString[] = "0123456789ABCDEF";
/*
* printhex prints rightmost <digs> hex digits of <val>
*/
printhex(val,digs)
register int val;
register int digs;
{
digs = ((--digs)&7)<<2; /* digs == 0 => print 8 digits */
for (; digs >= 0; digs-=4)
putchar(hexString[(val>>digs)&0xF]);
}
/* Function: readNprint()
**
** Description: Reads from a memory location, then prints its contents.
**
** Global variables used: none.
**
** Parameters: addr, len (BYTE, WORD, LONG).
**
** Returns: none.
**
** Side Effects: Scribbles on diagnostic console. Could cause
** a bus error if the user requested a non-existant
** memory location.
*/
readNprint(addr, len)
register int addr, len;
{
unsigned long val;
register int length;
printhex(addr, 8); /* print 32 bits worth of address... */
printf(": "); /* then separate the data from the address. */
/*
* Copy len into length, so that len may be modified in the
* body of the switch statement.
*/
length = len;
switch (length) { /* Read the value from memory */
#ifdef NOTDEF
case BYTE:
val = *(unsigned char *)addr;
break;
default: /* shouldn't happen, but if it does, */
len = WORD; /* fix it, then fall through to the WORD case */
case WORD:
val = *(unsigned short*)addr;
break;
#endif
case LONG:
dgReadMem(addr, 4, ( (char *) (&val) ) );
break;
}
printhex((int)val, len);
}
/* Function: writeNprint()
**
** Description: Writes to a memory location, then prints its new contents.
**
** Global variables used: none.
**
** Parameters: addr, len (BYTE, WORD, LONG).
**
** Returns: none.
**
** Side Effects: Scribbles on diagnostic console. Could cause
** a bus error if the user requested a non-existant
** memory location.
*/
writeNprint(addr, len)
register int addr, len;
{
unsigned long val;
register int length;
printhex(addr, 8); /* print 32 bits worth of address */
val = getnum(); /* read a datum from global line buffer */
printf(" -> "); /* show that a new value is being written to memory */
printhex((int)val, len); /* then print this new value */
putchar('\n');
/*
* Copy len into length, so that len may be modified in the
* body of the switch statement.
*/
length = len;
switch (length) { /* Write the value to memory */
#ifdef NOTDEF
case BYTE:
*(unsigned char *)addr = (unsigned char)val;
break;
default: /* shouldn't happen, but if it does, */
len = WORD; /* fix it, then fall through to the WORD case */
case WORD:
*(unsigned short*)addr = (unsigned short)val;
break;
#endif
case LONG:
dgWriteMem(addr, 4, ( (char *) (&val) ) );
break;
}
}
/* Function: checkForPlus()
**
** Description: Checks the command line argument to see if a '+' character
** has been entered. If so, all '+' chars are eaten, and the
** "don't increment" flag is set.
**
** Global variables used: pGlobals->lineptr, dontIncrement.
**
** Parameters: none.
**
** Returns: none.
**
** Side Effects: none.
*/
checkForPlus()
{
if (*pGlobals->lineptr == '+') {
dontIncrement = TRUE;
/*
* Eat any and all "+" characters.
*/
pGlobals->lineptr++;
skipWhite();
while(*pGlobals->lineptr == '+') {
pGlobals->lineptr++;
skipWhite();
}
} else {
dontIncrement = FALSE; /* clear the flag on second pass */
}
}
/* Function: queryMem()
**
** Description: Opens a memory location for interactive reading/writing.
**
** If queryMem() is invoked with command line input, it will parse
** that command line, perform the requested actions, and return.
**
** If queryMem() is invoked with only an address, it will read
** that location (dumping its contents) and prompt for command line input.
**
** Command line input is interpreted as follows:
**
** . A blank delimited hex value will overwrite the current addressed location.
**
** . A non-hex character will read from the current addressed location. If the
** non-hex character is on a line by itself, we return to the monitor.
**
** . A carriage return on a line by itself will read the next sequential memory
** location.
**
** . The "+" character may be used to read, write, or read, then write the
** same memory location multiple times.
**
** Global variables used: pGlobals->lineptr.
**
** Parameters: addr, len(BYTE, WORD, LONG).
**
** Returns: TRUE if we are to continue reading and/or
** writing memory;
** FALSE otherwise.
**
** Side Effects: Scribbles on diagnostic console.
*/
queryMem(addr, len)
register int addr, len;
{
register unsigned char c;
register unsigned long val;
register int retval;
c = *pGlobals->lineptr;
if (c == '\0') {
/*
* No command line arguments were supplied. We read addr,
* print it, then prompt for command line arguments.
*/
readNprint(addr, len);
printf("? "); /* ask for args */
#ifdef __sgi__
gets(pGlobals->line);
#else
fgets(pGlobals->line, sizeof pGlobals->line, stdin);
#endif
pGlobals->lineptr = pGlobals->line; /* reset the global line ptr */
c = *pGlobals->lineptr;
/*
* A <cr> typed here means we should continue reading sequential
* memory locations. We check for NULL, as gets() strips <cr>.
*/
if (c == '\0')
return(TRUE);
/*
* A '^' typed here means we will decrement the current address on
* the next pass through queryMem().
*/
if (c == '^') {
decrementAddr = TRUE;
pGlobals->lineptr++;
return(TRUE);
}
/*
* Check for the special character which indicates that we should
* recycle the current address for subsequent reads and/or writes.
* If there are any leading "+" characters, we'll skip past them.
*/
checkForPlus();
c = *pGlobals->lineptr;
/*
* If the first character typed was non-hex, we close this edit session.
*/
if (ishex(c) < 0) {
return(FALSE);
} else {
/*
* Otherwise, a hex argument was supplied. Write it into memory.
*/
writeNprint(addr, len);
skipWhite();
/*
* Check for the special character which indicates that we should
* recycle the current address for subsequent reads and/or writes.
*/
checkForPlus();
/*
* Finally, we return to process the next (or same) memory location.
*/
return(TRUE);
}
} else {
/*
* A command line was supplied. We parse it (possibly re-entering
* queryMem() a few times with the global line pointer advanced to the
* next argument). We return when, upon looking past the current
* argument, we find there ain't no mo arguments.
*/
if (ishex(c) < 0) {
/*
* A non-hex value was encountered. This means we should read the
* current address, then print its contents (we do not write to it).
*/
readNprint(addr, len);
putchar('\n');
/*
* Check for the '^' character, which means we should decrement the
* current address on the next pass through queryMem().
*/
if (c == '^')
decrementAddr = TRUE;
pGlobals->lineptr++; /* advance to next argument */
skipWhite();
/*
* Don't quit if the user typed a bunch of '^' characters to
* go upwards in memory (think of '^' as a peer to <cr>.
*/
if ((*pGlobals->lineptr == '\0') && (decrementAddr == FALSE))
return(FALSE); /* That's all, folks. */
/*
* Check for the special character which indicates that we should
* recycle the current address for subsequent reads and/or writes.
*/
checkForPlus();
/*
* Finally, we return to process the previous, same, or same
* memory location.
*/
return(TRUE);
} else {
/*
* A hex value was supplied. Write without reading...
*/
writeNprint(addr, len);
/*
* Now advance to the next command line argument (if any).
*/
skipWhite();
if (*pGlobals->lineptr == '\0')
return(FALSE); /* That's all, folks. */
/*
* Check for the special character which indicates that we should
* recycle the current address for subsequent reads and/or writes.
*/
checkForPlus();
return(TRUE);
}
}
}
/* Function: getonedec()
**
** Description: gets a decimal number from a line buffer parameter:
** advances this pointer to the first blank following
** the start of the number.
**
** Global variables used: none.
**
** Parameters: status, inputBuf.
**
** Returns: integral value of input decimal number,
** or 0 if a bad number was encountered.
** The parameter status is set as follows:
**
** TRUE if the hex value converted successfully,
** FALSE if there was inconvertible input,
** NODATA if there was no input.
**
** Side Effects: Reads from the global line buffer, but does NOT
** read past end of line; It reads the number
** in from the global line buffer, then points
** the global line pointer at either the next
** character in the global line buffer or to the
** terminating '\0'.
**
*/
getonedec(status, inputBuf)
int *status;
unsigned char **inputBuf;
{
register unsigned char *pChar, *numStart;
register int limCheck, zeroesRead;
unsigned char buf[LINEBUFSIZE];
numStart = buf;
zeroesRead = FALSE;
pChar = *inputBuf;
/* skip past white space (if any). */
while ((*pChar != '\0') && ((*pChar == ' ') || (*pChar == '\t')))
pChar++;
while (*pChar == '0') {
pChar++; /* skip past zeroes */
zeroesRead = TRUE;
}
if (*pChar == '\0') { /* no input */
if (zeroesRead == TRUE)
*status = TRUE;
else
*status = NODATA;
return(0);
}
while ((*pChar != ' ') && (*pChar != '\t') &&
(*pChar != '\0'))
{
*numStart++ = *pChar++;
}
*numStart++ = '\0';
/*
* Reset the user's line buffer pointer.
*/
*inputBuf = pChar;
return(atoi(buf));
}
/* Function: getonex()
**
** Description: gets a hexadecimal number from a line buffer parameter:
** advances this pointer to the first blank following
** the start of the number.
** limit = 0 => no limit
** limit = 2 => 8 bit number expected
** limit = 4 => 16 bit number expected
** limit = 8 => 32 bit number expected
**
** Global variables used: none.
**
** Parameters: limit, status, inputBuf.
**
** Returns: integral value of input hexadecimal number
** or 0 if a bad number was encountered. The
** The parameter status is set as follows:
**
** TRUE if the hex value converted successfully,
** FALSE if there was inconvertible input,
** NODATA if there was no input.
**
** Side Effects: Resets the user's buffer pointer to point to
** either the next character in the buffer or to
** the terminating '\0'.
**
*/
int
getonex(limit, status, inputBuf)
int limit, *status;
unsigned char **inputBuf;
{
register unsigned char *pChar, *numStart;
register int limCheck, zeroesRead;
unsigned char buf[LINEBUFSIZE];
numStart = buf;
limCheck = 0;
pChar = *inputBuf;
/* skip past white space (if any). */
while ((*pChar != '\0') && ((*pChar == ' ') || (*pChar == '\t')))
pChar++;
while (*pChar == '0') {
pChar++; /* skip past zeroes */
zeroesRead = TRUE;
}
if (*pChar == '\0') { /* no input */
if (zeroesRead == TRUE)
*status = TRUE;
else
*status = NODATA;
/*
* Reset the user's line buffer pointer.
*/
*inputBuf = pChar;
return(0);
}
while ((*pChar != ' ') && (*pChar != '\t') &&
(*pChar != '\0') && (limCheck < limit))
{
*numStart++ = *pChar++;
limCheck++;
}
*numStart++ = '\0';
/*
* Reset the user's line buffer pointer.
*/
*inputBuf = pChar;
return(atox(buf, limit, status));
}
/* Function: skipWhite()
**
** Description: Skips past white space (pointed to by
** pGlobals->lineptr), advancing the pointer to the first
** character after spaces and tabs.
**
**
** Global variables used: TBD.
**
** Parameters: none.
**
** Returns: none.
**
** Side Effects: TBD.
**
*/
skipWhite()
{
register unsigned char *pChar;
pChar = pGlobals->lineptr;
/* skip past white space (if any). */
while ((*pChar != '\0') && ((*pChar == ' ') || (*pChar == '\t')))
pChar++;
pGlobals->lineptr = pChar;
}
/* Function: atox().
**
** Description: Converts a hex string (pointed to by parameter s) to an integral
** value. Checks to make sure there are only as many chars in the
** string as expected by the parameter limit.
**
** Global variables used: none.
**
** Parameters: s, limit, status.
**
** Returns: integral value of hex string (or 0 if error).
** The parameter status is set to TRUE if the
** hex value converted okay; FALSE otherwise.
**
** Side Effects: none.
**
*/
atox(s, limit, status)
register char *s;
register int limit, *status;
{
register int num, limck;
register char c;
num = 0;
limck = 1;
while(c = *s++) { /* while c is not '\0' */
if (limit != 0) {
if (limck++ > limit) {
*status = FALSE;
return(0);
}
}
switch(c) {
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9':
num = num*16 + (c - '0');
break;
case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f':
num = num*16 + (10 + c - 'a');
break;
case 'A': case 'B': case 'C':
case 'D': case 'E': case 'F':
num = num*16 + (10 + c - 'A');
break;
default:
*status = FALSE;
return(0);
break;
}
}
*status = TRUE;
return(num);
}