Skip to content

Commit

Permalink
Updated some debug code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed Jul 9, 2024
1 parent 27aee1b commit 02894b2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 108 deletions.
4 changes: 2 additions & 2 deletions aq/aq.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
int main(int argc, char *argv[]) {
// TODO(Aqvm): Finish this function after completing AQVM development.
if (Aqvm_InitVm() != 0) {
AqvmRuntimeDebugger_OutputReport(2, "Aqmain_InitVmError",
"Initializing Aqvm met error.", NULL);
AqvmRuntimeDebugger_OutputReport("\"ERROR\"", "\"main_InitVmError\"",
"\"Initializing Aqvm met error.\"", NULL);
return -1;
}
return 0;
Expand Down
95 changes: 55 additions & 40 deletions aqvm/memory/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,51 @@ int AqvmMemory_CheckMemoryConditions() {
int warning_count = 0;
if (sizeof(aqint) != 4) {
AqvmRuntimeDebugger_OutputReport(
1, "AqvmMemoryCheckMemoryConditions_IntLengthWarning",
"The length requirement for the int type does not conform to the type "
"definition.",
"\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_IntLengthWarning\"",
"\"The length requirement for the int type does not conform to the "
"type "
"definition.\"",
NULL);
++warning_count;
}
if (sizeof(aqlong) != 8) {
AqvmRuntimeDebugger_OutputReport(
1, "AqvmMemoryCheckMemoryConditions_LongLengthWarning",
"The length requirement for the long type does not conform to the type "
"definition.",
"\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_LongLengthWarning\"",
"\"The length requirement for the long type does not conform to the "
"type "
"definition.\"",
NULL);
++warning_count;
}
if (sizeof(aqfloat) != 4) {
AqvmRuntimeDebugger_OutputReport(
1, "AqvmMemoryCheckMemoryConditions_FloatLengthWarning",
"The length requirement for the float type does not conform to the "
"type definition.",
"\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_FloatLengthWarning\"",
"\"The length requirement for the float type does not conform to the "
"type definition.\"",
NULL);
++warning_count;
}
if (sizeof(aqdouble) != 4) {
AqvmRuntimeDebugger_OutputReport(
1, "AqvmMemoryCheckMemoryConditions_DoubleLengthWarning",
"The length requirement for the double type does not conform to the "
"type definition.",
"\"WARNING\"",
"\"AqvmMemory_CheckMemoryConditions_DoubleLengthWarning\"",
"\"The length requirement for the double type does not conform to the "
"type definition.\"",
NULL);
++warning_count;
}
if (sizeof(aqchar) != 1) {
AqvmRuntimeDebugger_OutputReport(
1, "AqvmMemoryCheckMemoryConditions_CharLengthWarning",
"The length requirement for the char type does not conform to the type "
"definition.",
"\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_CharLengthWarning\"",
"\"The length requirement for the char type does not conform to the "
"type "
"definition.\"",
NULL);
++warning_count;
}
if (sizeof(aqbool) != 1) {
AqvmRuntimeDebugger_OutputReport(
1, "AqvmMemoryCheckMemoryConditions_BoolLengthWarning",
"\"WARNING\"", "\"AqvmMemory_CheckMemoryConditions_BoolLengthWarning\"",
"The length requirement for the bool type does not conform to the type "
"definition.",
NULL);
Expand All @@ -72,8 +76,8 @@ struct AqvmMemory_Memory* AqvmMemory_CreateMemory(void* data, void* type,
(struct AqvmMemory_Memory*)malloc(sizeof(struct AqvmMemory_Memory));
if (memory_ptr == NULL) {
AqvmRuntimeDebugger_OutputReport(
2, "AqvmMemoryCreateMemory_MemoryAllocationFailure",
"Failed to allocate memory.", NULL);
"\"ERROR\"", "\"AqvmMemory_CreateMemory_MemoryAllocationFailure\"",
"\"Failed to allocate memory.\"", NULL);
return NULL;
}

Expand All @@ -91,26 +95,30 @@ void AqvmMemory_FreeMemory(struct AqvmMemory_Memory* memory_ptr) {
int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index,
uint8_t type) {
if (memory == NULL) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemorySetType_NullMemoryPointer",
"The memory pointer is NULL.", NULL);
AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
"\"AqvmMemory_SetType_NullMemoryPointer\"",
"\"The memory pointer is NULL.\"", NULL);
return -1;
}

if (memory->type == NULL) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemorySetType_NullTypePointer",
"The type pointer is NULL.", NULL);
AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
"\"AqvmMemory_SetType_NullTypePointer\"",
"\"The type pointer is NULL.\"", NULL);
return -2;
}

if (index > memory->size) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemorySetType_OutOfMemoryRange",
"The index is out of memory range.", NULL);
AqvmRuntimeDebugger_OutputReport(
"\"ERROR\"", "\"AqvmMemory_SetType_OutOfMemoryRange\"",
"\"The index is out of memory range.\"", NULL);
return -3;
}

if (type > 0x0F) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemorySetType_OutOfTypeRange",
"The type is out of range.", NULL);
AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
"\"AqvmMemory_SetType_OutOfTypeRange\"",
"\"The type is out of range.\"", NULL);
return -4;
}

Expand All @@ -125,20 +133,23 @@ int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index,

uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index) {
if (memory == NULL) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryGetType_NullMemoryPointer",
"The memory pointer is NULL.", NULL);
AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
"\"AqvmMemory_GetType_NullMemoryPointer\"",
"\"The memory pointer is NULL.\"", NULL);
return 0x11;
}

if (memory->type == NULL) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryGetType_NullTypePointer",
"The type pointer is NULL.", NULL);
AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
"\"AqvmMemory_GetType_NullTypePointer\"",
"\"The type pointer is NULL.\"", NULL);
return 0x12;
}

if (index > memory->size) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryGetType_OutOfMemoryRange",
"The index is out of memory range.", NULL);
AqvmRuntimeDebugger_OutputReport(
"\"ERROR\"", "\"AqvmMemory_GetType_OutOfMemoryRange\"",
"\"The index is out of memory range.\"", NULL);
return 0x13;
}

Expand All @@ -152,26 +163,30 @@ uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index) {
int AqvmMemory_WriteData(struct AqvmMemory_Memory* memory, size_t index,
void* data_ptr, size_t size) {
if (memory == NULL) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryWriteData_NullMemoryPointer",
"The memory pointer is NULL.", NULL);
AqvmRuntimeDebugger_OutputReport(
"\"ERROR\"", "\"AqvmMemory_WriteData_NullMemoryPointer\"",
"\"The memory pointer is NULL.\"", NULL);
return -1;
}

if (memory->type == NULL) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryWriteData_NullTypePointer",
"The type pointer is NULL.", NULL);
AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
"\"AqvmMemory_WriteData_NullTypePointer\"",
"\"The type pointer is NULL.\"", NULL);
return -2;
}

if (index > memory->size) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryWriteData_OutOfMemoryRange",
"The index is out of memory range.", NULL);
AqvmRuntimeDebugger_OutputReport(
"\"ERROR\"", "\"AqvmMemory_WriteData_OutOfMemoryRange\"",
"\"The index is out of memory range.\"", NULL);
return -3;
}

if (data_ptr == NULL) {
AqvmRuntimeDebugger_OutputReport(1, "AqvmMemoryWriteData_NullDataPointer",
"The data pointer is NULL.", NULL);
AqvmRuntimeDebugger_OutputReport("\"ERROR\"",
"\"AqvmMemory_WriteData_NullDataPointer\"",
"\"The data pointer is NULL.\"", NULL);
return -4;
}

Expand Down
42 changes: 11 additions & 31 deletions aqvm/runtime/debugger/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,12 @@
#include <string.h>
#include <time.h>

void AqvmRuntimeDebugger_OutputReport(uint8_t type, const char* code,
void AqvmRuntimeDebugger_OutputReport(const char* type, const char* code,
const char* message,
const char* other_info) {
const char* type_str =
AqvmRuntimeDebugger_FormatReport(type, code, message, other_info);

char time[28];
AqvmRuntimeDebugger_GetCurrentTime(time);

fprintf(stderr,
"{\"Time\":%s,\"Type\":%s,\"Code\":%s,\"Message\":%s,\"ErrnoInfo\":{"
"\"Errno\":%d,\"Message\":\"%s\"},\"OtherInfo\":%s}\n",
time, type_str, code, message, errno, strerror(errno), other_info);
}

const char* AqvmRuntimeDebugger_FormatReport(uint8_t type, const char* code,
const char* message,
const char* other_info) {
if (type == NULL) {
type = "NULL";
}
if (code == NULL) {
code = "NULL";
}
Expand All @@ -38,20 +26,12 @@ const char* AqvmRuntimeDebugger_FormatReport(uint8_t type, const char* code,
other_info = "NULL";
}

switch (type) {
case 0:
return "\"INFO\"";
case 1:
return "\"WARNING\"";
case 2:
return "\"ERROR\"";
default:
return "NULL";
}
}

void AqvmRuntimeDebugger_GetCurrentTime(char* return_time) {
char time_str[28];
time_t current_time = time(NULL);
strftime(return_time, 28, "\"%Y-%m-%dT%H:%M:%S%z\"",
localtime(&current_time));
strftime(time_str, 28, "\"%Y-%m-%dT%H:%M:%S%z\"", localtime(&current_time));

fprintf(stderr,
"{\"Time\":%s,\"Type\":%s,\"Code\":%s,\"Message\":%s,\"ErrnoInfo\":{"
"\"Errno\":%d,\"Message\":\"%s\"},\"OtherInfo\":%s}\n",
time_str, type, code, message, errno, strerror(errno), other_info);
}
47 changes: 12 additions & 35 deletions aqvm/runtime/debugger/debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,21 @@

#include <stdint.h>

// The struct stores information about the debug report. |type| is the type of
// the report. In |type|, 0 is INFO, 1 is WARNING, and 2 is ERROR.
// |type|, |code|, and |message| are necessary and shouldn't be set to NULL in
// common. |other_info| is a pointer to the other information and it can be set
// to NULL if it is not needed.
// NOTICE: If you need to use it, please use json format.
struct AqvmRuntimeDebugger_DebugReport {
uint8_t type;
char* code;
char* message;
char* other_info;
};

// Outputs |report| with time, errno and so on to printing to the console or
// other devices and writing to a log file. No return.
// |type| is the type of the report. In |type|, 0 is INFO, 1 is WARNING, and 2
// is ERROR. |type|, |code|, and |message| are necessary and shouldn't be set to
// NULL in common. |other_info| is a pointer to the other information and it can
// be set to NULL if it is not needed.
// NOTICE: If you need to use it, please use json format. The output is json
// format.
void AqvmRuntimeDebugger_OutputReport(uint8_t type, const char* code,
// |type|, |code|, and |message| are necessary and shouldn't be set to NULL in
// common. But |other_info| can be set to NULL if it is not needed.
// In general, |type| should be "ERROR", "WARNING" or "INFO". |code| should be a
// full function name plus a concise description of the error, separated by
// underscores (e.g., AqvmRuntimeDebugger_OutputReport_TestInfo). |message|
// should be a detailed and accurate description. |other_info| on the other hand
// should be an additional information to the current report (e.g. system
// information).
// NOTICE: If you need to use the function, please use json format. The output
// is json format. For example, AqvmRuntimeDebugger_OutputReport("\"type\"",
// "\"code\"", "\"message\"", "\"other_info\"");
void AqvmRuntimeDebugger_OutputReport(const char* type, const char* code,
const char* message,
const char* other_info);

// INTERNAL USE ONLY.
// Gets the current time in a string format based on ISO 8601 standard and
// stores it in |return_time|. No return.
// |return_time| length is at least 28 bytes.
// NOTICE: The output is json format.
void AqvmRuntimeDebugger_GetCurrentTime(char* return_time);

// INTERNAL USE ONLY.
// If |code|, |message|, or |other_info| is NULL, It will be set to the string
// "NULL". Returns type string accroding to the |type|.
// NOTICE: The output is json format.
const char* AqvmRuntimeDebugger_FormatReport(uint8_t type, const char* code,
const char* message,
const char* other_info);

#endif

0 comments on commit 02894b2

Please sign in to comment.