summaryrefslogtreecommitdiff
path: root/src/libu64/stackcheck.c
blob: 69fe224ee8faaea298027d66273aaed83d3958d2 (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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "libu64/debug.h"
#include "attributes.h"
#include "printf.h"
#include "stackcheck.h"
#include "terminal.h"
#include "translation.h"

StackEntry* sStackInfoListStart = NULL;
StackEntry* sStackInfoListEnd = NULL;

void StackCheck_Init(StackEntry* entry, void* stackBottom, void* stackTop, u32 initValue, s32 minSpace,
                     const char* name) {
    StackEntry* iter;
    u32* addr;

    if (entry == NULL) {
        sStackInfoListStart = NULL;
    } else {
        entry->head = stackBottom;
        entry->tail = stackTop;
        entry->initValue = initValue;
        entry->minSpace = minSpace;
        entry->name = name;

#if !PLATFORM_N64
        iter = sStackInfoListStart;
        while (iter) {
            if (iter == entry) {
                PRINTF(VT_COL(RED, WHITE) T("stackcheck_init: %08x は既にリスト中にある\n",
                                            "stackcheck_init: %08x is already in the list\n") VT_RST,
                       entry);
                return;
            }
            iter = iter->next;
        }
#endif

        entry->prev = sStackInfoListEnd;
        entry->next = NULL;

        if (sStackInfoListEnd) {
            sStackInfoListEnd->next = entry;
        }

        sStackInfoListEnd = entry;
        if (!sStackInfoListStart) {
            sStackInfoListStart = entry;
        }

        if (entry->minSpace != -1) {
            addr = entry->head;
            while (addr < entry->tail) {
                *addr++ = entry->initValue;
            }
        }
    }
}

void StackCheck_Cleanup(StackEntry* entry) {
#if PLATFORM_N64
    if (!entry->prev) {
        sStackInfoListStart = entry->next;
    } else {
        entry->prev->next = entry->next;
    }

    if (!entry->next) {
        sStackInfoListEnd = entry->prev;
    }
#else
    u32 inconsistency = false;

    if (!entry->prev) {
        if (entry == sStackInfoListStart) {
            sStackInfoListStart = entry->next;
        } else {
            inconsistency = true;
        }
    } else {
        entry->prev->next = entry->next;
    }

    if (!entry->next) {
        if (entry == sStackInfoListEnd) {
            sStackInfoListEnd = entry->prev;
        } else {
            inconsistency = true;
        }
    }

    if (inconsistency) {
        PRINTF(VT_COL(RED, WHITE) T("stackcheck_cleanup: %08x リスト不整合です\n",
                                    "stackcheck_cleanup: %08x list inconsistent\n") VT_RST,
               entry);
    }
#endif
}

#if PLATFORM_N64

u32 StackCheck_Check(StackEntry* entry) {
    if (entry == NULL) {
        u32 ret = 0;
        StackEntry* iter = sStackInfoListStart;

        while (iter) {
            u32 state = StackCheck_Check(iter);

            if (state != STACK_STATUS_OK) {
                ret = 1;
            }
            iter = iter->next;
        }

        return ret;
    } else {
        u32* last;
        UNUSED_NDEBUG u32 used;
        u32 free;
        u32 ret;

        for (last = entry->head; last < entry->tail; last++) {
            if (entry->initValue != *last) {
                break;
            }
        }

        used = (uintptr_t)entry->tail - (uintptr_t)last;
        free = (uintptr_t)last - (uintptr_t)entry->head;

        if (free == 0) {
            ret = STACK_STATUS_OVERFLOW;
        } else if (free < (u32)entry->minSpace && entry->minSpace != -1) {
            ret = STACK_STATUS_WARNING;
        } else {
            ret = STACK_STATUS_OK;
        }

        osSyncPrintf("head=%08x tail=%08x last=%08x used=%08x free=%08x [%s]\n", entry->head, entry->tail, last, used,
                     free, entry->name != NULL ? entry->name : "(null)");

        return ret;
    }
}

#else

u32 StackCheck_GetState(StackEntry* entry) {
    u32* last;
    UNUSED_NDEBUG u32 used;
    u32 free;
    u32 ret;

    for (last = entry->head; last < entry->tail; last++) {
        if (entry->initValue != *last) {
            break;
        }
    }

    used = (uintptr_t)entry->tail - (uintptr_t)last;
    free = (uintptr_t)last - (uintptr_t)entry->head;

    if (free == 0) {
        ret = STACK_STATUS_OVERFLOW;
        PRINTF_COLOR_RED();
    } else if (free < (u32)entry->minSpace && entry->minSpace != -1) {
        ret = STACK_STATUS_WARNING;
        PRINTF_COLOR_YELLOW();
    } else {
        PRINTF_COLOR_GREEN();
        ret = STACK_STATUS_OK;
    }

#if !DEBUG_FEATURES
    // This string is still in .rodata for retail builds
    (void)"(null)";
#endif

    PRINTF("head=%08x tail=%08x last=%08x used=%08x free=%08x [%s]\n", entry->head, entry->tail, last, used, free,
           entry->name != NULL ? entry->name : "(null)");
    PRINTF_RST();

#if DEBUG_FEATURES
    if (ret != STACK_STATUS_OK) {
        LogUtils_LogHexDump(entry->head, (uintptr_t)entry->tail - (uintptr_t)entry->head);
    }
#endif

    return ret;
}

u32 StackCheck_CheckAll(void) {
    u32 ret = 0;
    StackEntry* iter = sStackInfoListStart;

    while (iter) {
        u32 state = StackCheck_GetState(iter);

        if (state != STACK_STATUS_OK) {
            ret = 1;
        }
        iter = iter->next;
    }

    return ret;
}

u32 StackCheck_Check(StackEntry* entry) {
    if (entry == NULL) {
        return StackCheck_CheckAll();
    } else {
        return StackCheck_GetState(entry);
    }
}

#endif