Skip to content

Commit

Permalink
debug: add debug_printThreadRegs()
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Heider <[email protected]>
  • Loading branch information
Axel Heider committed Jul 4, 2024
1 parent d5bf87b commit ba3fce6
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
64 changes: 63 additions & 1 deletion include/api/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifdef CONFIG_DEBUG_BUILD
#pragma once

#include <string.h>
#include <benchmark/benchmark_track.h>
#include <arch/api/syscall.h>
#include <arch/kernel/vspace.h>
Expand Down Expand Up @@ -65,12 +66,47 @@ static inline void debug_printKernelEntryReason(void)
}
}

#if CONFIG_WORD_SIZE == 32
#define SEL4_PRIx_reg "%08"SEL4_PRIx_word
#define SEL4_PRI_reg_param(r) (r)
#elif CONFIG_WORD_SIZE == 64
#define SEL4_PRIx_reg "%08"PRIx32"_%08"PRIx32
#define SEL4_PRI_reg_param(r) (uint32_t)((r) >> 32), (uint32_t)(r)
#else
#error "unsupported CONFIG_WORD_SIZE"
#endif

/* Prints the user context and stack trace of the current thread */
static inline void debug_printThreadRegs(tcb_t *tcb)
{
user_context_t *tcbCtx = &(tcb->tcbArch.tcbContext);
/* Dynamically adapt spaces to max register name len per column. */
unsigned int max_reg_name_len[2] = {0};
for (unsigned int i = 0; i < ARRAY_SIZE(tcbCtx->registers); i++) {
int col = i & 1;
unsigned int l = strnlen(register_names[i], 20);
if (max_reg_name_len[col] < l) {
max_reg_name_len[col] = l;
}
}
const unsigned int num_regs = ARRAY_SIZE(tcbCtx->registers);
for (unsigned int i = 0; i < num_regs; i++) {
int col = i & 1;
bool_t is_last = (num_regs == i + 1);
word_t reg = tcbCtx->registers[i];
printf("%*s: 0x"SEL4_PRIx_reg"%s",
max_reg_name_len[col] + 2, register_names[i],
SEL4_PRI_reg_param(reg), (col || is_last) ? "\n" : "");
}
}

/* Prints the user context and stack trace of the current thread */
static inline void debug_printUserState(void)
{
tcb_t *tptr = NODE_STATE(ksCurThread);
printf("Current thread: %s\n", TCB_PTR_DEBUG_PTR(tptr)->tcbName);
printf("Next instruction adress: %lx\n", getRestartPC(tptr));
//printf("Next instruction adress: %lx\n", getRestartPC(tptr));
debug_printThreadRegs(tptr);
printf("Stack:\n");
Arch_userStackTrace(tptr);
}
Expand Down Expand Up @@ -131,6 +167,32 @@ static inline void debug_dumpScheduler(void)
debug_printTCB(curr);
}
}


static inline void debug_timer_interrupt(void)
{
const word_t PRINT_CNT = 500;

static word_t dbg_cnt = 0;
static uint64_t cy_timer = 0;
static uint64_t cy_timer_1000 = 0;

uint64_t cy_now = riscv_read_cycle();
if (++dbg_cnt > PRINT_CNT) /* 2 ms timer */
{
dbg_cnt = 0;
printf("%"SEL4_PRIu_word" timer ticks: cy %"PRIu64" (%"PRIu64")\n",
PRINT_CNT,
cy_now - cy_timer,
(cy_now - cy_timer_1000) / PRINT_CNT);
cy_timer_1000 = cy_now;
}
cy_timer = cy_now;
// tcb_t *tptr = NODE_STATE(ksCurThread);
// debug_printTCB(tptr);
// debug_printThreadRegs(tptr);
}

#endif /* CONFIG_PRINTING */
#endif /* CONFIG_DEBUG_BUILD */

8 changes: 8 additions & 0 deletions include/arch/arm/arch/32/mode/machine/registerset.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ enum _register {
n_contextRegisters = 20,
};

#if defined(CONFIG_PRINTING) && defined(CONFIG_DEBUG_BUILD)
static UNUSED const char *register_names[n_contextRegisters] = {
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11",
"r12", "r13/sp", "r14/lr", "NextIP", "cpsr", "FaultIP", "tpidrurw/tls",
"tpidruro"
};
#endif /* defined(CONFIG_PRINTING) && defined(CONFIG_DEBUG_BUILD) */

#define NEXT_PC_REG NextIP

compile_assert(sp_offset_correct, SP *sizeof(word_t) == PT_SP)
Expand Down
9 changes: 9 additions & 0 deletions include/arch/arm/arch/64/mode/machine/registerset.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ compile_assert(sp_offset_correct, SP_EL0 *sizeof(word_t) == PT_SP_EL0)
compile_assert(lr_svc_offset_correct, ELR_EL1 *sizeof(word_t) == PT_ELR_EL1)
compile_assert(faultinstruction_offset_correct, FaultIP *sizeof(word_t) == PT_FaultIP)

#if defined(CONFIG_PRINTING) && defined(CONFIG_DEBUG_BUILD)
static UNUSED const char *register_names[n_contextRegisters] = {
"x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11",
"x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "x21", "x22",
"x23", "x24", "x25", "x26", "x27", "x28", "x29", "x30/lr", "sp", "elr_e11",
"NextIP", "spsr_e11", "tpidr_el0/tls", "tpidrro_el0"
};
#endif /* defined(CONFIG_PRINTING) && defined(CONFIG_DEBUG_BUILD) */

typedef word_t register_t;

enum messageSizes {
Expand Down
10 changes: 10 additions & 0 deletions include/arch/riscv/arch/machine/registerset.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ enum _register {
n_contextRegisters
};

#if defined(CONFIG_PRINTING) && defined(CONFIG_DEBUG_BUILD)
static UNUSED const char *register_names[n_contextRegisters] = {
"x1/ra", "x2/sp", "x3/gp", "x4/tp", "x5/t0", "x6/t1", "x7/t2", "x8/s0",
"x9/s1", "x10/a0", "x11/a1", "x12/a2", "x13/a3", "x14/a4", "x15/a5",
"x16/a6", "x17/a7", "x18/s2", "x19/s3", "x20/s4", "x21/s5", "x22/s6",
"x23/s7", "x24/s8", "x25/s9", "x26/s10", "x27/s11", "x28/t3", "x29/t4",
"x30/t5", "x31/t6", "scause", "sstatus", "FaultIP", "NextIP"
};
#endif /* defined(CONFIG_PRINTING) && defined(CONFIG_DEBUG_BUILD) */

typedef uint8_t register_t;

enum messageSizes {
Expand Down
8 changes: 8 additions & 0 deletions include/arch/x86/arch/32/mode/machine/registerset.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ enum _register {
/* 0x44 */ n_contextRegisters = 17
};

#if defined(CONFIG_PRINTING) && defined(CONFIG_DEBUG_BUILD)
static UNUSED const char *register_names[n_contextRegisters] = {
"(none)", /* enum _register starts with 1 */
"eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "FaultIP", "Error",
"NextIP", "cs", "flags", "esp", "ss", "fs base", "gs/tls base"
};
#endif /* defined(CONFIG_PRINTING) && defined(CONFIG_DEBUG_BUILD) */

typedef word_t register_t;

enum messageSizes {
Expand Down
8 changes: 8 additions & 0 deletions include/arch/x86/arch/64/mode/machine/registerset.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ enum _register {
n_contextRegisters = 24 /* 0xc0 */
};

#if defined(CONFIG_PRINTING) && defined(CONFIG_DEBUG_BUILD)
static UNUSED const char *register_names[n_contextRegisters] = {
"rdi", "rsi", "rax", "rbx", "rbp", "r12", "r13", "r14", "rdx", "r10", "r8",
"r9", "r15", "FLAGS", "NextIP", "Error", "rsp", "FaultIP", "r11", "rcx",
"cs", "ss", "fs/tls base", "gs base"
};
#endif /* defined(CONFIG_PRINTING) && defined(CONFIG_DEBUG_BUILD) */

typedef uint32_t register_t;

enum messageSizes {
Expand Down
5 changes: 5 additions & 0 deletions include/stdint.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ typedef int64_t intmax_t;
#define PRIi64 "lli"
#define PRIu64 "llu"
#define PRIx64 "llx"

#define PRId32 "d"
#define PRIi32 "i"
#define PRIu32 "u"
#define PRIx32 "x"

0 comments on commit ba3fce6

Please sign in to comment.