PatchworkOS
Loading...
Searching...
No Matches
debug.c
Go to the documentation of this file.
2
4#include <kernel/log/log.h>
6
7static void aml_debug_dump_print_line(const uint8_t* start, uint64_t lineStart, uint64_t lineEnd)
8{
9 LOG_ERR(" %08x: ", lineStart);
10 for (uint64_t j = 0; j < 16; j++)
11 {
12 if (lineStart + j <= lineEnd)
13 {
14 LOG_ERR("%02x ", start[lineStart + j]);
15 }
16 else
17 {
18 LOG_ERR(" ");
19 }
20 }
21 LOG_ERR(" | ");
22 for (uint64_t j = 0; j < 16; j++)
23 {
24 if (lineStart + j <= lineEnd)
25 {
26 uint8_t c = start[lineStart + j];
27 if (c >= 32 && c <= 126)
28 {
29 LOG_ERR("%c", c);
30 }
31 else
32 {
33 LOG_ERR(".");
34 }
35 }
36 }
37 LOG_ERR("\n");
38}
39
40static void aml_debug_dump(const uint8_t* start, const uint8_t* end, const uint8_t* current)
41{
42 uint64_t index = current - start;
43 uint64_t dataSize = end - start;
44
45 uint64_t errorLineStart = (index / 16) * 16;
46 uint64_t prevLineStart = errorLineStart >= 16 ? errorLineStart - 16 : 0;
47 uint64_t nextLineStart = errorLineStart + 16;
48
49 if (errorLineStart > 0)
50 {
51 uint64_t prevLineEnd = errorLineStart - 1;
52 if (prevLineEnd >= dataSize)
53 {
54 prevLineEnd = dataSize - 1;
55 }
56 aml_debug_dump_print_line(start, prevLineStart, prevLineEnd);
57 }
58
59 uint64_t errorLineEnd = errorLineStart + 15;
60 if (errorLineEnd >= dataSize)
61 {
62 errorLineEnd = dataSize - 1;
63 }
64 aml_debug_dump_print_line(start, errorLineStart, errorLineEnd);
65
66 uint64_t errorOffsetInLine = index - errorLineStart;
67 LOG_ERR(" ");
68 for (uint64_t i = 0; i < errorOffsetInLine; i++)
69 {
70 LOG_ERR(" ");
71 }
72 LOG_ERR("^^");
73 uint64_t reminaingInLine = 15 - errorOffsetInLine;
74 for (uint64_t i = 0; i < reminaingInLine; i++)
75 {
76 LOG_ERR(" ");
77 }
78 LOG_ERR(" ");
79 for (uint64_t i = 0; i < errorOffsetInLine; i++)
80 {
81 LOG_ERR(" ");
82 }
83 LOG_ERR("^\n");
84
85 if (nextLineStart < dataSize)
86 {
87 uint64_t nextLineEnd = nextLineStart + 15;
88 if (nextLineEnd >= dataSize)
89 {
90 nextLineEnd = dataSize - 1;
91 }
92 aml_debug_dump_print_line(start, nextLineStart, nextLineEnd);
93 }
94}
95
96void aml_debug_error(aml_term_list_ctx_t* ctx, const char* function, const char* format, ...)
97{
98 aml_state_t* state = ctx->state;
99
100 if (state->errorDepth++ == 0)
101 {
102 LOG_ERR("AML ERROR in '%s()'", function);
103
104 const uint8_t* start = NULL;
105 const uint8_t* end = NULL;
106
108 if (method != NULL)
109 {
110 LOG_ERR(" at method '%s' and offset 0x%lx\n", AML_NAME_TO_STRING(method->name),
111 ctx->current - method->start);
112 DEREF(method);
113 start = method->start;
114 end = method->end;
115 }
116 else
117 {
118 LOG_ERR(" at offset 0x%lx\n", ctx->current - ctx->start);
119 start = ctx->start;
120 end = ctx->end;
121 }
122
123 LOG_ERR("message: ");
124
125 va_list args;
126 va_start(args, format);
127 log_vprint(LOG_LEVEL_ERR, FILE_BASENAME, format, args);
128 va_end(args);
129
130 LOG_ERR("\n");
131
132 aml_debug_dump(start, end, ctx->current);
133 LOG_ERR("backtrace:\n");
134 }
135 else
136 {
137 if (state->errorDepth == 10)
138 {
139 LOG_ERR(" ...\n");
140 return;
141 }
142 else if (state->errorDepth > 10)
143 {
144 return;
145 }
146 LOG_ERR(" %s() -> ", function);
147
148 va_list args;
149 va_start(args, format);
150 log_vprint(LOG_LEVEL_ERR, FILE_BASENAME, format, args);
151 va_end(args);
152
153 LOG_ERR("\n");
154 }
155}
static void aml_debug_dump(const uint8_t *start, const uint8_t *end, const uint8_t *current)
Definition debug.c:40
static void aml_debug_dump_print_line(const uint8_t *start, uint64_t lineStart, uint64_t lineEnd)
Definition debug.c:7
void aml_debug_error(aml_term_list_ctx_t *ctx, const char *function, const char *format,...)
Log a debug error message with context information.
Definition debug.c:96
#define AML_NAME_TO_STRING(name)
Macro to convert an aml_name_t to a stack allocated string.
Definition namespace.h:129
aml_method_obj_t * aml_method_find(const uint8_t *addr)
Find the method which contains the provided address in its AML bytecode range.
Definition object.c:794
#define LOG_ERR(format,...)
Definition log.h:89
uint64_t log_vprint(log_level_t level, const char *prefix, const char *format, va_list args)
Print a formatted log message with a va_list.
Definition log.c:173
@ LOG_LEVEL_ERR
Definition log.h:44
#define DEREF(ptr)
Decrement reference count.
Definition ref.h:80
#define NULL
Pointer error value.
Definition NULL.h:23
static void start()
Definition main.c:542
#define va_start(ap, parmN)
Definition stdarg.h:14
#define va_end(ap)
Definition stdarg.h:13
__builtin_va_list va_list
Definition stdarg.h:9
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
Data for a method object.
Definition object.h:284
const uint8_t * end
Definition object.h:293
const uint8_t * start
Definition object.h:292
AML State.
Definition state.h:25
uint64_t errorDepth
The length of the error traceback, if 0 then no error has occurred.
Definition state.h:29
Context for reading a TermList.
Definition term.h:37
aml_state_t * state
Definition term.h:38
const uint8_t * start
Definition term.h:40
const uint8_t * end
Definition term.h:41
const uint8_t * current
Definition term.h:42