source: rtems/cpukit/libmisc/shell/main_mdump.c

Last change on this file was 5f8c41c, checked in by Joel Sherrill <joel@…>, on 04/01/22 at 18:18:49

Update email address of Fernando Ruiz Casas to <fruizcasas@…>

This was requested to be executed prior to relicensing to BSD-2.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/**
2 * @file
3 *
4 * @brief MDUMP Shell Command Implmentation
5 */
6
7/*
8 * Copyright (c) 2001 Fernando Ruiz Casas <fruizcasas@gmail.com>
9 *
10 *  Reworked by Ric Claus at SLAC.Stanford.edu
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <ctype.h>
22#include <stdio.h>
23#include <inttypes.h>
24#include <string.h>
25
26#include <rtems.h>
27#include <rtems/shell.h>
28#include <rtems/stringto.h>
29#include "internal.h"
30
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);
36static void mdumpC(void* addr, int m);
37
38int rtems_mdump(void* addr, int max, int sz);
39
40static int rtems_shell_main_mdump(
41  int   argc,
42  char *argv[]
43)
44{
45  void *addr;
46  int   max;
47  int   sz;
48
49  if (args_parse(argc, argv, &addr, &max, &sz))
50    return -1;
51  return rtems_mdump(addr, max, sz);
52}
53
54static 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))
64    return -1;
65  return rtems_mdump(addr, max, 2);
66}
67
68
69static int rtems_shell_main_ldump(
70  int   argc,
71  char *argv[]
72)
73{
74  void *addr;
75  int   max;
76  int   sz;
77
78  if (args_parse(argc, argv, &addr, &max, &sz))
79    return -1;
80  return rtems_mdump(addr, max, 4);
81}
82
83
84int args_parse(int    argc,
85               char*  argv[],
86               void** addr,
87               int*   max,
88               int*   sz)
89{
90  *addr = NULL;
91  *max  = 320;
92  *sz   = 1;
93
94  if (argc > 1) {
95    if ( rtems_string_to_pointer(argv[1], addr, NULL) ) {
96      printf( "Address argument (%s) is not a number\n", argv[1] );
97      return -1;
98    }
99
100    if (argc > 2) {
101      if ( rtems_string_to_int(argv[2], max, NULL, 0) ) {
102        printf( "Length argument (%s) is not a number\n", argv[2] );
103        return -1;
104      }
105
106      if (argc > 3) {
107        if ( rtems_string_to_int(argv[3], sz, NULL, 0) ) {
108          printf( "Size argument (%s) is not a valid number\n", argv[3] );
109          return -1;
110        }
111      }
112    }
113  }
114  return 0;
115}
116
117
118int rtems_mdump(void* addr, int max, int sz)
119{
120  unsigned char  m;
121  unsigned char *pb;
122  int            res;
123  int            cnt;
124
125  if (!((sz == 1) || (sz == 2) || (sz == 4))) {
126    printf( "Size argument (%d) is not one of 1 (bytes), "
127              " 2 (words) or 4 (longwords)\n", sz);
128    return -1;
129  }
130
131  if (max <= 0) {
132    max = 1;           /* print 1 item if 0 or neg. */
133    res = 0;
134  } else {
135    max--;
136    res = max & 0xf;   /* num bytes in last row */
137    max >>= 4;         /* div by 16 */
138    max++;             /* num of rows to print */
139    if (max > 64) {    /* limit to 64 lines */
140      max = 64;
141      res = 0xf;       /* 16 bytes print in last row */
142    }
143  }
144
145  pb = addr;
146  for (m=0; m<max; m++) {
147    cnt = m==(max-1)?res:0xf;
148    printf("%10p ", pb);
149    if      (sz == 1)  mdumpB(pb, cnt);
150    else if (sz == 2)  mdumpW(pb, cnt);
151    else if (sz == 4)  mdumpL(pb, cnt);
152    mdumpC(pb, cnt);
153    printf("\n");
154    pb += 16;
155  }
156
157  return 0;
158}
159
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
194void mdumpC(void* addr, int m)
195{
196  volatile unsigned char* pb = addr;
197  int n;
198  for (n=0;n<=m;n++)
199    printf("%c", isprint(pb[n]) ? pb[n] : '.');
200}
201
202
203rtems_shell_cmd_t rtems_shell_MDUMP_Command = {
204  "mdump",                                      /* name */
205  "mdump [address [length [size]]]",            /* usage */
206  "mem",                                        /* topic */
207  rtems_shell_main_mdump,                       /* command */
208  NULL,                                         /* alias */
209  NULL                                          /* next */
210};
211
212
213rtems_shell_cmd_t rtems_shell_WDUMP_Command = {
214  "wdump",                                      /* name */
215  "wdump [address [length]]",                   /* usage */
216  "mem",                                        /* topic */
217  rtems_shell_main_wdump,                       /* command */
218  NULL,                                         /* alias */
219  NULL                                          /* next */
220};
221
222
223rtems_shell_cmd_t rtems_shell_LDUMP_Command = {
224  "ldump",                                      /* name */
225  "ldump [address [length]]",                   /* usage */
226  "mem",                                        /* topic */
227  rtems_shell_main_ldump,                       /* command */
228  NULL,                                         /* alias */
229  NULL                                          /* next */
230};
231
Note: See TracBrowser for help on using the repository browser.