1
1
#include < windows.h>
2
2
#include < psapi.h>
3
3
#include < common/error/Win32Error.h>
4
- #include < ostream>
5
4
#include < fstream>
5
+ #include < format>
6
6
#include < ctime>
7
7
#include < cassert>
8
8
#include < cstring>
12
12
#include < cstddef>
13
13
#include " ProcessMemoryCache.h"
14
14
15
- inline std::string StringFormat (const char * format, ...)
16
- {
17
- va_list args;
18
- va_start (args, format);
19
- int size = vsnprintf (nullptr , 0 , format, args) + 1 ;// Extra space for '\0'
20
- va_end (args);
21
- std::unique_ptr<char []> buf (new char [size]);
22
- va_start (args, format);
23
- vsnprintf (buf.get (), size, format, args);
24
- va_end (args);
25
- return std::string (buf.get (), buf.get () + size - 1 ); // We don't want the '\0' inside
26
- }
27
-
28
15
class TextDumpHelper {
29
16
private:
30
17
SYSTEM_INFO m_sys_info;
@@ -70,7 +57,7 @@ class TextDumpHelper {
70
57
}
71
58
if (prefix) {
72
59
auto addr = exc_rec.ExceptionInformation [1 ];
73
- out << prefix << StringFormat (" location % 08X caused an access violation.\n " , addr);
60
+ out << prefix << std::format (" location {: 08X} caused an access violation.\n " , addr);
74
61
}
75
62
}
76
63
out << std::endl;
@@ -125,20 +112,20 @@ class TextDumpHelper {
125
112
void write_exception_info (std::ostream& out, const EXCEPTION_RECORD& exc_rec)
126
113
{
127
114
out << " Exception Record:\n " ;
128
- out << " ExceptionCode = " << StringFormat ( " % 08X" , exc_rec.ExceptionCode ) << " \n " ;
129
- out << " ExceptionFlags = " << StringFormat ( " % 08X" , exc_rec.ExceptionFlags ) << " \n " ;
130
- out << " ExceptionAddress = " << StringFormat ( " %08X " , exc_rec.ExceptionAddress ) << " \n " ;
115
+ out << " ExceptionCode = " << std::format ( " {: 08X} " , exc_rec.ExceptionCode ) << " \n " ;
116
+ out << " ExceptionFlags = " << std::format ( " {: 08X} " , exc_rec.ExceptionFlags ) << " \n " ;
117
+ out << " ExceptionAddress = " << std::format ( " {} " , exc_rec.ExceptionAddress ) << " \n " ;
131
118
for (unsigned i = 0 ; i < exc_rec.NumberParameters ; ++i)
132
- out << " ExceptionInformation[" << i << " ] = " << StringFormat ( " % 08X" , exc_rec.ExceptionInformation [i]) << " \n " ;
119
+ out << " ExceptionInformation[" << i << " ] = " << std::format ( " {: 08X} " , exc_rec.ExceptionInformation [i]) << " \n " ;
133
120
out << std::endl;
134
121
}
135
122
136
123
void write_context (std::ostream& out, const CONTEXT& ctx)
137
124
{
138
125
out << " Context:\n " ;
139
- out << StringFormat (" EIP=% 08X EFLAGS=% 08X\n " , ctx.Eip , ctx.EFlags );
140
- out << StringFormat (" EAX=% 08X EBX=% 08X ECX=% 08X EDX=% 08X\n " , ctx.Eax , ctx.Ebx , ctx.Ecx , ctx.Edx );
141
- out << StringFormat (" ESP=% 08X EBP=% 08X ESI=% 08X EDI=% 08X\n " , ctx.Esp , ctx.Ebp , ctx.Esi , ctx.Edi );
126
+ out << std::format (" EIP={: 08X} EFLAGS={: 08X} \n " , ctx.Eip , ctx.EFlags );
127
+ out << std::format (" EAX={: 08X} EBX={: 08X} ECX={: 08X} EDX={: 08X} \n " , ctx.Eax , ctx.Ebx , ctx.Ecx , ctx.Edx );
128
+ out << std::format (" ESP={: 08X} EBP={: 08X} ESI={: 08X} EDI={: 08X} \n " , ctx.Esp , ctx.Ebp , ctx.Esi , ctx.Edi );
142
129
out << std::endl;
143
130
}
144
131
@@ -158,9 +145,9 @@ class TextDumpHelper {
158
145
{
159
146
out << " Memory map:\n " ;
160
147
for (auto & mbi: m_memory_map) {
161
- out << StringFormat ( " %08X : State % 08X Protect % 08X Type % 08X RegionSize % 08X\n " ,
148
+ out << std::format ( " {} : State {: 08X} Protect {: 08X} Type {: 08X} RegionSize {: 08X} \n " ,
162
149
mbi.BaseAddress , mbi.State , mbi.Protect , mbi.Type , mbi.RegionSize );
163
- // out << StringFormat (" AllocBase %x AllocProtect %x \n", mbi.AllocationBase, mbi.AllocationProtect);
150
+ // out << std::format (" AllocBase {:x} AllocProtect {:x} \n", mbi.AllocationBase, mbi.AllocationProtect);
164
151
}
165
152
out << std::endl;
166
153
}
@@ -357,15 +344,15 @@ class TextDumpHelper {
357
344
{
358
345
constexpr size_t num_cols = 8 ;
359
346
for (unsigned i = 0 ; i < num_dwords; i += num_cols) {
360
- out << StringFormat ( " % 08X:" , base_addr + i * 4 );
347
+ out << std::format ( " {: 08X} :" , base_addr + i * 4 );
361
348
for (size_t j = 0 ; j < num_cols && i + j < num_dwords; ++j) {
362
- out << StringFormat (" % 08X" , data[i + j]);
349
+ out << std::format (" {: 08X} " , data[i + j]);
363
350
}
364
351
out << " " ;
365
352
for (size_t j = 0 ; j < num_cols && i + j < num_dwords; ++j) {
366
353
const char * as_chars = reinterpret_cast <const char *>(&data[i + j]);
367
354
for (size_t k = 0 ; k < 4 ; ++k) {
368
- out << StringFormat ( " %c " , std::isprint (as_chars[k]) ? as_chars[k] : ' .' );
355
+ out << ( std::isprint (as_chars[k]) ? as_chars[k] : ' .' );
369
356
}
370
357
}
371
358
out << ' \n ' ;
@@ -403,21 +390,21 @@ class TextDumpHelper {
403
390
{
404
391
out << " Modules:\n " ;
405
392
for (auto & mod_info : m_modules) {
406
- out << StringFormat ( " % 08X - % 08X: %s \n " , mod_info.start_addr , mod_info.end_addr , mod_info.path . c_str () );
393
+ out << std::format ( " {: 08X} - {: 08X}: {} \n " , mod_info.start_addr , mod_info.end_addr , mod_info.path );
407
394
}
408
395
out << std::endl;
409
396
}
410
397
411
398
std::string format_module_relative_address (uintptr_t addr)
412
399
{
413
- std::stringstream ss ;
414
- ss << StringFormat ( " % 08X" , addr);
400
+ std::string buf ;
401
+ std::format_to ( std::back_inserter (buf), " {: 08X} " , addr);
415
402
for (auto & mod_info : m_modules) {
416
403
if (addr >= mod_info.start_addr && addr < mod_info.end_addr ) {
417
- ss << StringFormat ( " (%s+%X )" , mod_info.name . c_str () , addr - mod_info.start_addr );
404
+ std::format_to ( std::back_inserter (buf), " ({}+{:X} )" , mod_info.name , addr - mod_info.start_addr );
418
405
break ;
419
406
}
420
407
}
421
- return ss. str () ;
408
+ return buf ;
422
409
}
423
410
};
0 commit comments