From 34a21912aec8432c1c1b80feb5317d22b24aaf0b Mon Sep 17 00:00:00 2001 From: rkx1209 Date: Tue, 3 Apr 2018 17:18:12 +0900 Subject: [PATCH] Add cpu dump function after trigerring SEGV --- ARMv8/ARMv8.cpp | 14 +++++++------- Cpu.cpp | 2 +- Main.cpp | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/ARMv8/ARMv8.cpp b/ARMv8/ARMv8.cpp index ebb10b6..7ca506f 100644 --- a/ARMv8/ARMv8.cpp +++ b/ARMv8/ARMv8.cpp @@ -22,21 +22,21 @@ void RunLoop() { void Dump() { int cnt = 1; - debug_print ("CPU Dump:\n"); + ns_print ("CPU Dump:\n"); for (int r = 0; r < GPR_DUMMY; r++) { if (!X(r)) continue; if (r == GPR_LR) - debug_print ("LR:\t"); + ns_print ("LR:\t"); else if (r == GPR_SP) - debug_print ("SP:\t"); + ns_print ("SP:\t"); else - debug_print ("X%d:\t", r); - debug_print ("0x%016lx%c", X(r), cnt % 3 == 0 ? '\n' : '\t'); + ns_print ("X%d:\t", r); + ns_print ("0x%016lx%c", X(r), cnt % 3 == 0 ? '\n' : '\t'); cnt++; } - debug_print ("PC:\t0x%016lx\n", PC); - debug_print ("NZCV:\t0x%016lx\n", NZCV); + ns_print ("PC:\t0x%016lx\n", PC); + ns_print ("NZCV:\t0x%016lx\n", NZCV); } static uint64_t counter; diff --git a/Cpu.cpp b/Cpu.cpp index a0b8f62..ace7c0e 100644 --- a/Cpu.cpp +++ b/Cpu.cpp @@ -29,7 +29,7 @@ State GetState() { } void DumpMachine() { - ARMv8::Dump (); + //ARMv8::Dump (); if (TraceOut) ARMv8::DumpJson (TraceOut); } diff --git a/Main.cpp b/Main.cpp index b5d6c1c..82fb2a1 100644 --- a/Main.cpp +++ b/Main.cpp @@ -1,5 +1,6 @@ /* nsemu - LGPL - Copyright 2017 rkx1209 */ #include "Nsemu.hpp" +#include #include "optionparser.h" using namespace std; struct Arg : public option::Arg { @@ -88,6 +89,14 @@ const option::Descriptor usage[] = { 0, 0, nullptr, nullptr, nullptr, nullptr } }; +static void SignalHandler(int sig, siginfo_t* sig_info, void* sig_data) { + if(sig == SIGSEGV) { + ns_print ("SEGV: %p\n", sig_info->si_addr ); + ARMv8::Dump(); + _Exit(-1); + } +} + int main(int argc, char **argv) { Nsemu::create (); Nsemu *nsemu = Nsemu::get_instance (); @@ -141,6 +150,16 @@ printUsage: goto printUsage; } } + /* ### Register SEGV handler for debugging ### */ + struct sigaction segv_act; + sigemptyset(&segv_act.sa_mask); + sigaddset(&segv_act.sa_mask, SIGSEGV); + segv_act.sa_sigaction = SignalHandler; + segv_act.sa_flags = SA_SIGINFO|SA_RESTART|SA_ONSTACK; + if( sigaction( SIGSEGV, &segv_act, NULL ) == -1 ){ + ns_abort ("Failed to set my signal handler.\n"); + } + Banner (); nsemu->BootUp (parse.nonOption (0)); Nsemu::destroy ();