39void hexdump (std::ostream& s,
const void* addr,
size_t n,
size_t offset )
41 const char* ptr =
reinterpret_cast<const char*
> (addr);
44 boost::io::ios_all_saver saver (s);
48 char cbuf[
width + 1]{};
49 std::array<unsigned char, 4> bbuf{};
52 if ((ipos %
width) == 0) {
53 s << std::setw(16) << reinterpret_cast<uintptr_t>(ptr + ipos) - offset <<
" ";
55 if ((ipos % 4) == 0) {
58 bbuf[ipos % 4] =
static_cast<unsigned char>(ptr[ipos]);
59 cbuf[ipos %
width] = std::isgraph(
static_cast<unsigned char>(ptr[ipos])) ? ptr[ipos] :
'.';
62 if ((ipos % 4) == 0) {
63 s << std::setw(8) << static_cast<unsigned int>(std::bit_cast<uint32_t>(bbuf));
65 if ((ipos %
width) == 0) {
66 s <<
" " << cbuf <<
"\n";
70 if ((ipos %
width) > 0) {
71 unsigned ntrail = (ipos % 4);
73 for (
unsigned i = ntrail; i < 4; i++) {
76 s << std::setw(2*ntrail) << static_cast<unsigned int>(std::bit_cast<uint32_t>(bbuf));
78 while ((ipos %
width) != 0) {
79 if ((ipos % 4) == 0) {
83 cbuf[ipos %
width] =
' ';
86 s <<
" " << cbuf <<
"\n";
103void safeHexdump (std::ostream& s,
const void* addr,
size_t n,
size_t offset )
105 const char* ptr =
reinterpret_cast<const char*
> (addr);
108 size_t nadj =
reinterpret_cast<uintptr_t
>(ptr) %
width;
114 long pagesize_ret = sysconf (_SC_PAGESIZE);
115 if (pagesize_ret < 0 || pagesize_ret >= 1024*1024*1024) {
118 size_t pagesize = pagesize_ret;
125 uintptr_t iptr =
reinterpret_cast<uintptr_t
>(ptr);
126 size_t thispage = ((iptr + pagesize) & ~(pagesize-1)) - iptr;
132 hexdump (s, ptr, thispage, offset);
135 boost::io::ios_all_saver saver (s);
138 s << std::setw(16) << reinterpret_cast<uintptr_t>(ptr) - offset
139 <<
" --- is not readable\n";
141 thispage = std::max (ent->
endAddress - iptr, thispage);
void hexdump(std::ostream &s, const void *addr, size_t n, size_t offset=0)
Make a hex dump of memory.
void safeHexdump(std::ostream &s, const void *addr, size_t n, size_t offset=0)
Make a hex dump of memory, protected against bad reads.