-
Notifications
You must be signed in to change notification settings - Fork 10
/
tracing.cpp
65 lines (48 loc) · 1.08 KB
/
tracing.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
#include "tracing.h"
#include <cstring>
#include <unistd.h>
void malloc_intercept::print_object(char const* message)
{
::write(2, message, strlen(message));
}
void malloc_intercept::print_object(void* px)
{
char const* hexdigits = "0123456789abcdef";
char buffer[32];
size_t n = (size_t)px;
size_t divisor = std::numeric_limits<size_t>::max() / 2;
char* p = buffer;
*p++ = '0';
*p++ = 'x';
do
{
*p++ = hexdigits[(n / divisor) % 16];
divisor /= 16;
}
while (divisor != 0);
*p = '\0';
print_object(buffer);
}
void malloc_intercept::print_object(size_t n)
{
char buffer[32];
size_t divisor = 1;
while (divisor <= (n / 10))
divisor *= 10;
char* p = buffer;
do
{
*p++ = ((n / divisor) % 10) + '0';
divisor /= 10;
}
while (divisor != 0);
*p = '\0';
print_object(buffer);
}
void malloc_intercept::print()
{}
bool malloc_intercept::trace_enabled()
{
static bool enabled = (getenv("MALLOC_INTERCEPT_NO_TRACE") == NULL);
return enabled;
}