-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump-memory.cpp
159 lines (146 loc) · 3.48 KB
/
dump-memory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// dump-memory.cpp
//
// Created by massimo on 7/17/18.
//
#include "dump-memory.h"
#include <iomanip>
//#include <boost/format.hpp>
////////////////////////////////////////////////////////////////////////////////
namespace utilities {
//
// see: https://jrruethe.github.io/blog/2015/08/23/placement-new/
//
void
dumpMemory(const void* ptr,
const std::size_t size,
std::string&& demangledTypeName,
std::ostream& os) noexcept
{
using byte_t = unsigned char;
using uptr_t = unsigned long;
// dump from ptr-16 bytes through ptr+16 bytes
const uptr_t memBuffer {16};
// Allow direct arithmetic on the pointer
uptr_t iptr = reinterpret_cast<uptr_t>(ptr) - memBuffer;
os << "-----------------------------------------------------------------------\n";
if ("" != demangledTypeName)
{
//os << boost::format("Type %s of %d bytes\n") % demangledTypeName % size;
os << "Type "
<< demangledTypeName
<< " of "
<< std::dec
<< size
<< " bytes - Memory to dump starts at: "
<< std::hex << std::uppercase
<< ptr
<< "\n\n";
}
else
{
//os << boost::format("%d bytes\n") % size;
os << size
<< " bytes - Memory to dump starts at: "
<< std::hex << std::uppercase
<< ptr
<< "\n\n";
}
// Write the address offsets along the top row
os << std::string(19, ' ');
for (std::size_t i{0}; i < 16; ++i)
{
// Spaces between every 4 bytes
if (i % 4 == 0)
{
os << " ";
}
//os << boost::format(" %2hhX") % i; // Write the address offset
os << std::hex
<< std::uppercase
<< " "
<< std::setfill(' ')
<< std::setw(2)
<< i;
}
// If the object is not aligned
if (iptr % 16 != 0)
{
// Print the first address
//os << boost::format("\n0x%016lX:") % (iptr & ~15);
os << "\n0x"
<< std::setfill('0')
<< std::setw(16)
<< (iptr & ~15)
<< std::setw(0)
<< ":";
// Indent to the offset
for (std::size_t i{0}; i < iptr % 16; ++i)
{
os << " ";
if (i % 4 == 0)
{
os << " ";
}
}
}
bool closed {false};
const auto endByteToDump {(size + memBuffer * 2)};
const auto endByteToMark {(size + memBuffer - 1)};
// Dump the memory
for (std::size_t i {0}; i < endByteToDump; ++i, ++iptr)
{
// New line and address every 16 bytes, spaces every 4 bytes
if ( iptr % 16 == 0 )
{
//os << boost::format("\n0x%016lX:") % iptr;
os << "\n0x"
<< std::setfill('0')
<< std::setw(16)
<< iptr
<< std::setw(0)
<< ":";
}
if ( iptr % 4 == 0 )
{
os << " ";
}
// Write the address contents
//os << boost::format(" %02hhX") % static_cast<uptr_t>(*reinterpret_cast<byte_t *>(iptr));
if ( 16 == i )
{
os << "<";
}
else
{
if ( closed )
{
closed = false;
if ( iptr % 16 == 0 )
{
os << " ";
}
}
else
{
os << " ";
}
}
os << std::hex
<< std::setw(2)
<< std::setfill('0')
<< static_cast<uptr_t>(*reinterpret_cast<byte_t *>(iptr));
if ( i == endByteToMark )
{
closed = true;
os << ">";
}
}
os << "\n-----------------------------------------------------------------------"
<< std::endl;
} // dumpMemory
void dumpMemory(const char a[], std::ostream& os)
{
dumpMemory(a, std::strlen(a), os);
}
} // namespace utilities