source: rtems-tools/rtemstoolkit/rtems-utils.cpp @ 0c0b2d4

4.105
Last change on this file since 0c0b2d4 was 87e0e76, checked in by Chris Johns <chrisj@…>, on 09/13/14 at 02:09:16

Refactor code into the RTEMS Toolkit.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 * Copyright (c) 2012, Chris Johns <chrisj@rtems.org>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16/**
17 * @file
18 *
19 * @ingroup RLD
20 *
21 * @brief A memory dump routine.
22 *
23 */
24
25#if HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include <iomanip>
30#include <iostream>
31#include <vector>
32
33#include <rtems-utils.h>
34
35namespace rtems
36{
37  namespace utils
38  {
39    void
40    dump (const void* addr,
41          size_t      length,
42          size_t      size,
43          bool        real,
44          size_t      line_length,
45          uint32_t    offset)
46    {
47      char*    data;
48      size_t   b = 0;
49      uint8_t  d8 = 0;
50      uint16_t d16 = 0;
51      uint32_t d32 = 0;
52      uint64_t d64 = 0;
53
54      const uint8_t* addr8 = static_cast <const uint8_t*> (addr);
55
56      data = new char[line_length];
57
58      try
59      {
60        std::cout << std::hex << std::setfill ('0');
61
62        while (true)
63        {
64          if (((b % line_length) == 0) || (b >= length))
65          {
66            if (b)
67            {
68              size_t line = b % line_length;
69
70              if (line)
71              {
72                size_t remaining = line_length - line;
73                remaining = (remaining * 2) + (remaining / size);
74                std::cout << std::setfill (' ')
75                          << std::setw (remaining) << ' '
76                          << std::setfill ('0');
77              }
78              else
79                line = line_length;
80
81              std::cout << ' ';
82
83              for (size_t c = 0; c < line; c++)
84              {
85                if ((data[c] < 0x20) || (data[c] > 0x7e))
86                  std::cout << '.';
87                else
88                  std::cout << data[c];
89              }
90
91              if (b >= length)
92              {
93                std::cout << std::dec << std::setfill (' ')
94                          << std::endl << std::flush;
95                break;
96              }
97
98              std::cout << std::endl;
99            }
100
101            if (real)
102              std::cout << std::setw (sizeof(void*)) << (uint64_t) (addr8 + b);
103            else
104              std::cout << std::setw (8) << (uint32_t) (offset + b);
105          }
106
107          if ((b & (line_length - 1)) == (line_length >> 1))
108            std::cout << "-";
109          else
110            std::cout << " ";
111
112          switch (size)
113          {
114            case sizeof (uint8_t):
115            default:
116              d8 = *(addr8 + b);
117              std::cout << std::setw (2) << (uint32_t) d8;
118              data[(b % line_length) + 0] = d8;
119              break;
120
121            case sizeof (uint16_t):
122              d16 = *((const uint16_t*) (addr8 + b));
123              std::cout << std::setw (4) << d16;
124              data[(b % line_length) + 0] = (uint8_t) (d16 >> 8);
125              data[(b % line_length) + 1] = (uint8_t) d16;
126              break;
127
128            case sizeof (uint32_t):
129              d32 = *((const uint32_t*) (addr8 + b));
130              std::cout << std::setw (8) << d32;
131              for (int i = sizeof (uint32_t); i > 0; --i)
132              {
133                data[(b % line_length) + i] = (uint8_t) d32;
134                d32 >>= 8;
135              }
136              break;
137
138            case sizeof (uint64_t):
139              d64 = *((const uint64_t*) (addr8 + b));
140              std::cout << std::setw (16) << d64;
141              for (int i = sizeof (uint64_t); i > 0; --i)
142              {
143                data[(b % line_length) + i] = (uint8_t) d64;
144                d64 >>= 8;
145              }
146              break;
147          }
148          b += size;
149        }
150      }
151      catch (...)
152      {
153        delete [] data;
154        throw;
155      }
156
157      delete [] data;
158
159      std::cout << std::dec << std::setfill (' ');
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.