int mon_backtrace(int argc, char **argv, struct Trapframe *tf) { // Your code here. uint64_t *rbp = (uint64_t *)read_rbp(); uint64_t rip; read_rip(rip); cprintf("Stack backtrace: \n"); do { cprintf("rbp %16.0x rip %16.0x\n", rbp, rip); struct Ripdebuginfo info; debuginfo_rip(rip, &info); //file name and line within that file of the stack frame's rip, followed by the name of the function and the offset of the rip from the first instruction of the function int offset=rip-info.rip_fn_addr; cprintf(" %s:%d: %s+%16.0x ",info.rip_file, info.rip_line, info.rip_fn_name,offset); cprintf("args:%x ",info.rip_fn_narg); //number of arguments int i; for(i = 1; i <= info.rip_fn_narg; i++) cprintf("%16.0x ", *((int *)(rbp) -i)); cprintf("\n"); rip = (uint64_t) *(rbp+1); rbp = (uint64_t *)(*rbp); } while (rbp!=0); return 0; }
int mon_backtrace(int argc, char **argv, struct Trapframe *tf) { // Backtrace code. uint64_t rbp_value; uint64_t *rbp_pointer; int res, i; long int rip_value; rbp_pointer = (uint64_t *)read_rbp(); struct Ripdebuginfo debuginfo_instance; while(rbp_pointer) { rip_value = *((int*)rbp_pointer + 2); rip_value &= 0x00000000FFFFFFFF; //rip_value = rip_value & 0x00000000FFFFFFFF; res = debuginfo_rip((uintptr_t)rip_value, &debuginfo_instance); if(res == -1) { cprintf("\nCannot find bounds of current function\n"); return -1; } cprintf("rbp %016x rip %016x args %016x %016x %016x %016x\n", rbp_pointer, (int)rip_value, *((int *)rbp_pointer - 1), *((int *)rbp_pointer - 2), *((int *)rbp_pointer - 3), *((int *)rbp_pointer - 4)); cprintf("\t%s:%d: ", debuginfo_instance.rip_file, debuginfo_instance.rip_line); cprintf("%.*s+%016x\n", debuginfo_instance.rip_fn_namelen, debuginfo_instance.rip_fn_name,((int)rip_value - debuginfo_instance.rip_fn_addr)); rbp_pointer = (uint64_t *)*rbp_pointer; } return 0; }
inline void* calloc(size_t nmemb, size_t size) { void* ptr = __calloc(nmemb, size, __malloc_pool); #if DEBUG malloced(((void**)read_rbp())[1], nmemb * size, ptr); #endif /* DEBUG */ return ptr; }
inline void* realloc(void* ptr, size_t size) { void* ptr2 = __realloc(ptr, size, __malloc_pool); #if DEBUG freed(ptr); malloced(((void**)read_rbp())[1], size, ptr2); #endif /* DEBUG */ return ptr2; }
inline void* malloc(size_t size) { void* ptr = __malloc(size, __malloc_pool); if(!ptr) { // TODO: print to stderr printf("Not enough local memory!!!\n"); printf("Requested size: %ld from %p\n", size, ((void**)read_rbp())[1]); #if DEBUG malloc_statistics(); #endif /* DEBUG */ while(1) asm("hlt"); } #if DEBUG malloced(((void**)read_rbp())[1], size, ptr); #endif /* DEBUG */ return ptr; }
void freed(void* ptr) { debug_free_count++; if(is_debug) { is_debug = false; Trace* trace = map_remove(tracing, ptr); if(!trace) { printf("ERROR: freeing never alloced pointer: %p from: %p\n", ptr, ((void**)read_rbp())[1]); uint64_t* v = ptr; for(int i = 0; i < 16; i++) { printf("[%d] %p\n", i, v[i]); } while(1) asm("hlt"); } Stat* stat = map_get(statistics, trace->caller); if(stat == NULL) { printf("ERROR: freeing not alloced pointer from: %p\n", ((void**)read_rbp())[1]); while(1) asm("hlt"); } stat->count--; stat->size -= trace->size; if(stat->count == 0 && stat->size == 0) { map_remove(statistics, trace->caller); free(stat); } else if(stat->count == 0 && stat->size != 0) { printf("ERROR: malloc/free mismatching: count: %ld, size: %ld\n", stat->count, stat->size); while(1) asm("hlt"); } free(trace); is_debug = true; } }
int mon_backtrace(int argc, char **argv, struct Trapframe *tf) { // Your code here. uint64_t rbp = 0x0; uint64_t rip = 0x0; uint64_t* prbp = NULL; struct Ripdebuginfo info; cprintf("Stack backtrace:\n"); rbp = read_rbp(); read_rip(rip); if(rbp == 0x0 || rip == 0x0) { cprintf("Not able to show backtrace"); return -1; } prbp = (uint64_t*)(rbp); cprintf(" rbp %016x rip %016x\n", prbp, rip); debuginfo_rip(rip ,&info); cprintf(" %s:%d: ",info.rip_file, info.rip_line); cprintf("%.*s+%016x",info.rip_fn_namelen, info.rip_fn_name, (rip - info.rip_fn_addr)); cprintf(" args:%d", info.rip_fn_narg); printArgList(prbp, &info); while(prbp && *(prbp) != 0x0 && *(prbp+1) != 0x0) { cprintf(" rbp %016x rip %016x\n",*(prbp),*((prbp) +1)); debuginfo_rip(*(prbp+1) ,&info); cprintf(" %s:%d: ",info.rip_file, info.rip_line); cprintf("%.*s+%016x",info.rip_fn_namelen, info.rip_fn_name, (rip - info.rip_fn_addr)); cprintf(" args:%d", info.rip_fn_narg); printArgList((uint64_t*)(*(prbp)), &info); prbp = (uint64_t*)(*(prbp)); } return 0; }
int mon_backtrace(int argc, char **argv, struct Trapframe *tf) { // Your code here. cprintf("Stack backtrace:\n"); uint64_t stack_base_p = read_rbp(); uint64_t ret_inst_p; struct Ripdebuginfo rip_debug_info; read_rip(ret_inst_p); while(stack_base_p != 0) { cprintf("%5s %016x%5s %016x\n", "rbp", stack_base_p, "rip", ret_inst_p); if (debuginfo_rip(ret_inst_p, &rip_debug_info) == 0) { // Print th file name and line within that file of the stack frame's rip cprintf("%7s%s:%d: ", " ", rip_debug_info.rip_file, rip_debug_info.rip_line); // Print the name of the function cprintf("%.*s+", rip_debug_info.rip_fn_namelen, rip_debug_info.rip_fn_name); // Print the offset of the rip from the first instruction of the function cprintf("%016x", ret_inst_p - rip_debug_info.rip_fn_addr); // Print the number of function arguments cprintf(" args:%d ", rip_debug_info.rip_fn_narg); // Print the actual arguments themselves int arg_num; uint64_t arg_addr = stack_base_p; for (arg_num = 0; arg_num < rip_debug_info.rip_fn_narg; arg_num++) { arg_addr -= rip_debug_info.size_fn_arg[arg_num]; if (rip_debug_info.size_fn_arg[arg_num] == sizeof(uint32_t)) { cprintf(" %016x", *(uint32_t *)arg_addr); } else { arg_addr = arg_addr - arg_addr % 8; cprintf(" %016x", *(uint64_t *)arg_addr); } } cprintf("\n"); } ret_inst_p = *((uint64_t *)(stack_base_p + 1 * sizeof(uint64_t))); stack_base_p = *(uint64_t *)stack_base_p; } return 0; }
void check_stack(void) { __thread cpu_p pcpu = get_current_cpu(); extern ulong init_stack_top[]; extern void switchtrapret(void); ulong min; ulong max; ulong rbp = read_rbp(); if (is_bp_cpu(pcpu)) { max = (ulong) init_stack_top; min = max - 4 * 4096; printk("BP stack:min:%lx-max:%lx,irqret:%lx,rbp:%lx\n", min, max, switchtrapret, rbp); } else { min = (uint64) per_cpu_ptr(&init_stack, pcpu->cpu); max = min + sizeof(init_stack); printk("AP stack:min:%lx-max:%lx\n", min, max); } ulong *p; int stacknr = 0; p = (ulong *) rbp; while ((ulong) p >= min && (ulong) p < max && ++stacknr < 50) { if ((ulong) (switchtrapret) == *(p + 1)) { printk("irq:%lx,rbp:%lx,rip:%lx\n", *(p + 1), p, *(p + 19)); } else printk("%lx,rbp:%lx\n", *(p + 1), p); p = (ulong *) * p; } }