Ticket #2012: main_mdump.diff

File main_mdump.diff, 6.1 KB (added by Ric Claus, on 02/02/12 at 19:02:33)

Patchfile for main_mdump.c

  • .c

    old new  
    55 *  Work: fernando.ruiz@ctv.es
    66 *  Home: correo@fernando-ruiz.com
    77 *
     8 *  Reworked by Ric Claus at SLAC.Stanford.edu
     9 *
    810 *  The license and distribution terms for this file may be
    911 *  found in the file LICENSE in this distribution or at
    1012 *  http://www.rtems.com/license/LICENSE.
     
    2628#include <rtems/stringto.h>
    2729#include "internal.h"
    2830
     31static int args_parse(int argc, char* argv[], void** addr, int* max, int* sz);
     32
     33static void mdumpB(void* addr, int m);
     34static void mdumpW(void* addr, int m);
     35static void mdumpL(void* addr, int m);
     36
     37int rtems_mdump(void* addr, int max, int sz);
     38
     39
    2940int rtems_shell_main_mdump(
    3041  int   argc,
    3142  char *argv[]
    3243)
    3344{
    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
     54int 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
     68int 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
     82int 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;
    4091
    4192  if (argc > 1) {
    42     if ( rtems_string_to_pointer(argv[1], &addr, NULL) ) {
     93    if ( rtems_string_to_pointer(argv[1], addr, NULL) ) {
    4394      printf( "Address argument (%s) is not a number\n", argv[1] );
    4495      return -1;
    4596    }
    4697
    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      }
    48103
    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        }
    65109      }
    66110    }
     111  }
     112  return 0;
     113}
     114
     115
     116int 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;
    67132  } 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    }
    70141  }
    71142
    72143  pb = addr;
    73144  for (m=0; m<max; m++) {
     145    cnt = m==(max-1)?res:0xf;
    74146    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++) {
    80151      printf("%c", isprint(pb[n]) ? pb[n] : '.');
    81152    }
    82153    printf("\n");
    83154    pb += 16;
    84155  }
     156
    85157  return 0;
    86158}
    87159
     160
     161void 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
     172void 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
     183void 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
    88194rtems_shell_cmd_t rtems_shell_MDUMP_Command = {
    89195  "mdump",                                      /* name */
    90   "mdump [address [length]]",                   /* usage */
     196  "mdump [address [length [size]]]",            /* usage */
    91197  "mem",                                        /* topic */
    92198  rtems_shell_main_mdump,                       /* command */
    93199  NULL,                                         /* alias */
    94200  NULL                                          /* next */
    95201};
    96202
     203
     204rtems_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
     214rtems_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