Ticket #2012: main_mdump.diff
File main_mdump.diff, 6.1 KB (added by Ric Claus, on 02/02/12 at 19:02:33) |
---|
-
.c
old new 5 5 * Work: fernando.ruiz@ctv.es 6 6 * Home: correo@fernando-ruiz.com 7 7 * 8 * Reworked by Ric Claus at SLAC.Stanford.edu 9 * 8 10 * The license and distribution terms for this file may be 9 11 * found in the file LICENSE in this distribution or at 10 12 * http://www.rtems.com/license/LICENSE. … … 26 28 #include <rtems/stringto.h> 27 29 #include "internal.h" 28 30 31 static int args_parse(int argc, char* argv[], void** addr, int* max, int* sz); 32 33 static void mdumpB(void* addr, int m); 34 static void mdumpW(void* addr, int m); 35 static void mdumpL(void* addr, int m); 36 37 int rtems_mdump(void* addr, int max, int sz); 38 39 29 40 int rtems_shell_main_mdump( 30 41 int argc, 31 42 char *argv[] 32 43 ) 33 44 { 34 unsigned char n; 35 unsigned char m; 36 int max; 37 int res; 38 void *addr = NULL; 39 unsigned char *pb; 45 void *addr; 46 int max; 47 int sz; 48 49 if (args_parse(argc, argv, &addr, &max, &sz)) return -1; 50 return rtems_mdump(addr, max, sz); 51 } 52 53 54 int rtems_shell_main_wdump( 55 int argc, 56 char *argv[] 57 ) 58 { 59 void *addr; 60 int max; 61 int sz; 62 63 if (args_parse(argc, argv, &addr, &max, &sz)) return -1; 64 return rtems_mdump(addr, max, 2); 65 } 66 67 68 int rtems_shell_main_ldump( 69 int argc, 70 char *argv[] 71 ) 72 { 73 void *addr; 74 int max; 75 int sz; 76 77 if (args_parse(argc, argv, &addr, &max, &sz)) return -1; 78 return rtems_mdump(addr, max, 4); 79 } 80 81 82 int args_parse(int argc, 83 char* argv[], 84 void** addr, 85 int* max, 86 int* sz) 87 { 88 *addr = NULL; 89 *max = 320; 90 *sz = 1; 40 91 41 92 if (argc > 1) { 42 if ( rtems_string_to_pointer(argv[1], &addr, NULL) ) {93 if ( rtems_string_to_pointer(argv[1], addr, NULL) ) { 43 94 printf( "Address argument (%s) is not a number\n", argv[1] ); 44 95 return -1; 45 96 } 46 97 47 } 98 if (argc > 2) { 99 if ( rtems_string_to_int(argv[2], max, NULL, 0) ) { 100 printf( "Length argument (%s) is not a number\n", argv[2] ); 101 return -1; 102 } 48 103 49 if (argc > 2) { 50 if ( rtems_string_to_int(argv[1], &max, NULL, 0) ) { 51 printf( "Length argument (%s) is not a number\n", argv[1] ); 52 return -1; 53 } 54 if (max <= 0) { 55 max = 1; /* print 1 item if 0 or neg. */ 56 res = 0; 57 } else { 58 max--; 59 res = max & 0xf;/* num bytes in last row */ 60 max >>= 4; /* div by 16 */ 61 max++; /* num of rows to print */ 62 if (max > 20) { /* limit to 20 */ 63 max = 20; 64 res = 0xf; /* 16 bytes print in last row */ 104 if (argc > 3) { 105 if ( rtems_string_to_int(argv[3], sz, NULL, 0) ) { 106 printf( "Size argument (%s) is not a valid number\n", argv[3] ); 107 return -1; 108 } 65 109 } 66 110 } 111 } 112 return 0; 113 } 114 115 116 int rtems_mdump(void* addr, int max, int sz) 117 { 118 unsigned char n; 119 unsigned char m; 120 volatile unsigned char *pb; 121 int res; 122 int cnt; 123 124 if (!((sz == 1) || (sz == 2) || (sz == 4))) { 125 printf("Size argument (%d) is not one of 1 (bytes), 2 (words) or 4 (longwords)\n", sz); 126 return -1; 127 } 128 129 if (max <= 0) { 130 max = 1; /* print 1 item if 0 or neg. */ 131 res = 0; 67 132 } else { 68 max = 20; 69 res = 0xf; 133 max--; 134 res = max & 0xf; /* num bytes in last row */ 135 max >>= 4; /* div by 16 */ 136 max++; /* num of rows to print */ 137 if (max > 64) { /* limit to 64 lines */ 138 max = 64; 139 res = 0xf; /* 16 bytes print in last row */ 140 } 70 141 } 71 142 72 143 pb = addr; 73 144 for (m=0; m<max; m++) { 145 cnt = m==(max-1)?res:0xf; 74 146 printf("%10p ", pb); 75 for (n=0;n<=(m==(max-1)?res:0xf);n++) 76 printf("%02X%c",pb[n],n==7?'-':' '); 77 for (;n<=0xf;n++) 78 printf(" %c",n==7?'-':' '); 79 for (n=0;n<=(m==(max-1)?res:0xf);n++) { 147 if (sz == 1) mdumpB(pb, cnt); 148 else if (sz == 2) mdumpW(pb, cnt); 149 else if (sz == 4) mdumpL(pb, cnt); 150 for (n=0;n<=cnt;n++) { 80 151 printf("%c", isprint(pb[n]) ? pb[n] : '.'); 81 152 } 82 153 printf("\n"); 83 154 pb += 16; 84 155 } 156 85 157 return 0; 86 158 } 87 159 160 161 void mdumpB(void* addr, int m) 162 { 163 volatile unsigned char* pb = addr; 164 int n; 165 for (n=0;n<=m;n++) 166 printf("%02X%c",*pb++,n==7?'-':' '); 167 for (;n<=0xf;n++) 168 printf(" %c",n==7?'-':' '); 169 } 170 171 172 void mdumpW(void* addr, int m) 173 { 174 volatile unsigned short* pb = addr; 175 int n; 176 for (n=0;n<=m;n+=2) 177 printf("%04X%c",*pb++,n==6?'-':' '); 178 for (;n<=0xf;n+=2) 179 printf(" %c", n==6?'-':' '); 180 } 181 182 183 void mdumpL(void* addr, int m) 184 { 185 volatile unsigned int* pb = addr; 186 int n; 187 for (n=0;n<=m;n+=4) 188 printf("%08X%c",*pb++,n==4?'-':' '); 189 for (;n<=0xf;n+=4) 190 printf(" %c", n==4?'-':' '); 191 } 192 193 88 194 rtems_shell_cmd_t rtems_shell_MDUMP_Command = { 89 195 "mdump", /* name */ 90 "mdump [address [length ]]",/* usage */196 "mdump [address [length [size]]]", /* usage */ 91 197 "mem", /* topic */ 92 198 rtems_shell_main_mdump, /* command */ 93 199 NULL, /* alias */ 94 200 NULL /* next */ 95 201 }; 96 202 203 204 rtems_shell_cmd_t rtems_shell_WDUMP_Command = { 205 "wdump", /* name */ 206 "wdump [address [length]]", /* usage */ 207 "mem", /* topic */ 208 rtems_shell_main_wdump, /* command */ 209 NULL, /* alias */ 210 NULL /* next */ 211 }; 212 213 214 rtems_shell_cmd_t rtems_shell_LDUMP_Command = { 215 "ldump", /* name */ 216 "ldump [address [length]]", /* usage */ 217 "mem", /* topic */ 218 rtems_shell_main_ldump, /* command */ 219 NULL, /* alias */ 220 NULL /* next */ 221 }; 222