1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     dbg_Dump.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/types.h>
17 #include <nn/nstd.h>
18 #include <nn/dbg/dbg_DebugString.h>
19 #include <nn/dbg/dbg_Dump.h>
20 #include <cstring>
21 #include <cstdio>
22 #include <cctype>
23 #include <algorithm>
24 
25 /****************************************************************************/
26 namespace nn {
27 namespace dbg {
28 namespace detail {
DumpMemory(const void * p,size_t size,bit32 flags)29     void DumpMemory(const void* p, size_t size, bit32 flags)
30     {
31         char buffer[80];
32         char bufferString[16 + 1];
33         int  bufPos;
34         int  memPos;
35         uptr address;
36 
37         if (flags & NN_DBG_DUMP_FLAG_POINTER)
38         {
39             address = reinterpret_cast<uptr>(p);
40         }
41         else
42         {
43             address = 0;
44         }
45 
46         detail::PutString("\n");
47         {
48             bufPos = 0;
49             bufPos += nstd::TSPrintf(buffer, "            0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f");
50             if (flags & NN_DBG_DUMP_FLAG_STRING)
51             {
52                 bufPos += nstd::TSPrintf(&buffer[bufPos], "  0123456789abcdef");
53             }
54             bufPos += nstd::TSPrintf(&buffer[bufPos], "\n");
55             detail::PutString(buffer, bufPos);
56         }
57 
58         for(memPos = 0; size > 0; address += 0x10)
59         {
60             bufPos = 0;
61             bufPos += nstd::TSPrintf(buffer, "%08x: ", address & static_cast<uptr>(~0xf));
62             {
63                 int col = 0;
64 
65                 if (flags & NN_DBG_DUMP_FLAG_POINTER && memPos == 0)
66                 {
67                     for(; (address & 0xf) != col; ++col)
68                     {
69                         std::strcat(buffer, "   ");
70                         bufPos += 3;
71 
72                         bufferString[col] = ' ';
73                     }
74                 }
75                 for(; col < 0x10 && size > 0; ++col, --size, ++memPos)
76                 {
77                     u8 val = reinterpret_cast<const u8*>(p)[memPos];
78                     bufPos += nstd::TSPrintf(&buffer[bufPos], " %02x", val);
79                     if (std::isprint(val))
80                     {
81                         bufferString[col] = val;
82                     }
83                     else
84                     {
85                         bufferString[col] = '.';
86                     }
87                 }
88                 for(; col < 0x10 ; ++col)
89                 {
90                     std::strcat(buffer, "   ");
91                     bufPos += 3;
92 
93                     bufferString[col] = ' ';
94                 }
95                 bufferString[col] = '\0';
96             }
97             if (flags & NN_DBG_DUMP_FLAG_STRING)
98             {
99                 bufPos += nstd::TSPrintf(&buffer[bufPos], "  %s", bufferString);
100             }
101 
102             std::strcat(buffer, "\n");
103             detail::PutString(buffer);
104         }
105     }
106 
107 }  // namespace detail
108 }  // namespace dbg
109 }  // namespace nn
110 
nndbgDetailDumpMemory(const void * p,size_t size,bit32 flags)111 void nndbgDetailDumpMemory(const void* p, size_t size, bit32 flags)
112 {
113     nn::dbg::detail::DumpMemory(p, size, flags);
114 }
115