summaryrefslogtreecommitdiff
path: root/src/boot/logutils.c
blob: 5b3d024b23fc885146d342856558f845b847e3e9 (plain)
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
#include "global.h"
#include "terminal.h"

f32 LogUtils_CheckFloatRange(const char* exp, s32 line, const char* valueName, f32 value, const char* minName, f32 min,
                             const char* maxName, f32 max) {
    if (value < min || max < value) {
        PRINTF("%s %d: range error %s(%f) < %s(%f) < %s(%f)\n", exp, line, minName, min, valueName, value, maxName,
               max);
    }
    return value;
}

s32 LogUtils_CheckIntRange(const char* exp, s32 line, const char* valueName, s32 value, const char* minName, s32 min,
                           const char* maxName, s32 max) {
    if (value < min || max < value) {
        PRINTF("%s %d: range error %s(%d) < %s(%d) < %s(%d)\n", exp, line, minName, min, valueName, value, maxName,
               max);
    }
    return value;
}

void LogUtils_LogHexDump(void* ptr, s32 size0) {
    u8* addr = (u8*)ptr;
    s32 size = (s32)size0;
    s32 rest;
    s32 i;
    u32 off;

    PRINTF("dump(%08x, %u)\n", addr, size);
    PRINTF("address  off  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f   0123456789abcdef\n");

    off = 0;
    while (size > 0) {

        PRINTF("%08x %04x", addr, off);
        rest = (size < 0x10) ? size : 0x10;

        i = 0;
        while (true) {
            if (i < rest) {
                PRINTF(" %02x", *((u8*)addr + i));
            } else {
                PRINTF("   ");
            }

            i++;
            if (i > 0xF) {
                break;
            }
        }
        PRINTF("   ");

        i = 0;
        while (true) {
            if (i < rest) {
                u8 a = *(addr + i);

                PRINTF("%c", (a >= 0x20 && a < 0x7F) ? a : '.');
            } else {
                PRINTF(" ");
            }

            i++;
            if (i > 0xF) {
                break;
            }
        }
        PRINTF("\n");
        size -= rest;
        addr += rest;
        off += rest;
    }
}

void LogUtils_LogPointer(s32 value, u32 max, void* ptr, const char* name, const char* file, s32 line) {
    PRINTF(VT_COL(RED, WHITE) "%s %d %s[%d] max=%u ptr=%08x\n" VT_RST, file, line, name, value, max, ptr);
}

void LogUtils_CheckBoundary(const char* name, s32 value, s32 unk, const char* file, s32 line) {
    u32 mask = (unk - 1);

    if (value & mask) {
        PRINTF(VT_COL(RED, WHITE) "%s %d:%s(%08x) は バウンダリ(%d)違反です\n" VT_RST, file, line, name, value, unk);
    }
}

void LogUtils_CheckNullPointer(const char* exp, void* ptr, const char* file, s32 line) {
    if (ptr == NULL) {
        PRINTF(VT_COL(RED, WHITE) "%s %d:%s は はヌルポインタです\n" VT_RST, file, line, exp);
    }
}

void LogUtils_CheckValidPointer(const char* exp, void* ptr, const char* file, s32 line) {
    if (ptr == NULL || (u32)ptr < 0x80000000 || (0x80000000 + osMemSize) <= (u32)ptr) {
        PRINTF(VT_COL(RED, WHITE) "%s %d:ポインタ %s(%08x) が異常です\n" VT_RST, file, line, exp, ptr);
    }
}

void LogUtils_LogThreadId(const char* name, s32 line) {
    PRINTF("<%d %s %d>", osGetThreadId(NULL), name, line);
}

void LogUtils_HungupThread(const char* name, s32 line) {
    PRINTF("*** HungUp in thread %d, [%s:%d] ***\n", osGetThreadId(NULL), name, line);
    Fault_AddHungupAndCrash(name, line);
}

void LogUtils_ResetHungup(void) {
    PRINTF("*** Reset ***\n");
    Fault_AddHungupAndCrash("Reset", 0);
}