Global Exception handler plugin

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
jojojoris
Posts: 255
Joined: Sun Mar 30, 2008 4:06 am

Global Exception handler plugin

Post by jojojoris »

Hello everyone,

I want to have a plugin which can show a BSOD-like screen when the psp crashes just like windows XP

I tried to write some plugin which load the exception.prx but it seems the exception.prx from sakya can only monitor the module which loaded it.

main.c

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>

#include "../utility/exception.h"

PSP_MODULE_INFO&#40;"pspBSOD_plugin", 0, 1, 0&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

int module_start&#40;SceSize args, void *argp&#41;
&#123;
    initExceptionHandler&#40;&#41;;
	return 0;

&#125;
exception.c

Code: Select all

#include <pspkernel.h>
#include <pspsdk.h>
#include <pspctrl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

PspDebugRegBlock exception_regs;

extern SceModule module_info;
extern int _ftext;

static const char *codeTxt&#91;32&#93; =&#123;
    "Interrupt", "TLB modification", "TLB load/inst fetch", "TLB store",
    "Address load/inst fetch", "Address store", "Bus error &#40;instr&#41;",
    "Bus error &#40;data&#41;", "Syscall", "Breakpoint", "Reserved instruction",
    "Coprocessor unusable", "Arithmetic overflow", "Unknown 13", "Unknown 14",
    "FPU Exception", "Unknown 16", "Unknown 17", "Unknown 18",
    "Unknown 20", "Unknown 21", "Unknown 22", "Unknown 23",
    "Unknown 24", "Unknown 25", "Unknown 26", "Unknown 27",
    "Unknown 28", "Unknown 29", "Unknown 30", "Unknown 31"
&#125;;

static const unsigned char regName&#91;32&#93;&#91;5&#93; =&#123;
    "zr", "at", "v0", "v1", "a0", "a1", "a2", "a3",
    "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
    "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
    "t8", "t9", "k0", "k1", "gp", "sp", "fp", "ra"
&#125;;

void ExceptionHandler&#40;PspDebugRegBlock * regs&#41; &#123;
    int i;
    SceCtrlData pad;

    pspDebugScreenInit&#40;&#41;;
    pspDebugScreenSetBackColor&#40;0x00FF0000&#41;;
    pspDebugScreenSetTextColor&#40;0xFFFFFFFF&#41;;
    pspDebugScreenClear&#40;&#41;;
    pspDebugScreenPrintf&#40;" A problem has been detected and your psp has been stopped.\n"&#41;;
    pspDebugScreenPrintf&#40;"\n"&#41;;
    pspDebugScreenPrintf&#40;" The problem seems to be caused by the following module&#58;\n %s\n", module_info.modname&#41;;
    pspDebugScreenPrintf&#40;"\n"&#41;;
    pspDebugScreenPrintf&#40;" If this is the first time you've seen this Stop error screen, you\n can restart the game. If this screen appears again, you can contact the developer to tell about this bug.\n"&#41;;
    pspDebugScreenPrintf&#40;"\n"&#41;;
    pspDebugScreenPrintf&#40;" Technical information&#58;\n\n"&#41;;

    pspDebugScreenPrintf&#40;" Exception - %s\n", codeTxt&#91;&#40;regs->cause >> 2&#41; & 31&#93;&#41;;
    pspDebugScreenPrintf&#40;" EPC       - %08X\n", &#40;int&#41; regs->epc&#41;;
    pspDebugScreenPrintf&#40;" Module    - %s\n", module_info.modname&#41;;
    pspDebugScreenPrintf&#40;" ModuleAddr- %08X\n", &#40;unsigned int&#41; &#40;regs->epc - &#40;int&#41; & _ftext&#41;&#41;;
    pspDebugScreenPrintf&#40;" Cause     - %08X\n", &#40;int&#41; regs->cause&#41;;
    pspDebugScreenPrintf&#40;" Status    - %08X\n", &#40;int&#41; regs->status&#41;;
    pspDebugScreenPrintf&#40;" BadVAddr  - %08X\n", &#40;int&#41; regs->badvaddr&#41;;
    for &#40;i = 0; i < 32; i += 4&#41; pspDebugScreenPrintf&#40;" %s&#58;%08X %s&#58;%08X %s&#58;%08X %s&#58;%08X\n", regName&#91;i&#93;, &#40;int&#41; regs->r&#91;i&#93;, regName&#91;i + 1&#93;, &#40;int&#41; regs->r&#91;i + 1&#93;, regName&#91;i + 2&#93;, &#40;int&#41; regs->r&#91;i + 2&#93;, regName&#91;i + 3&#93;, &#40;int&#41; regs->r&#91;i + 3&#93;&#41;;

    sceKernelDelayThread&#40;1000000&#41;;
    pspDebugScreenPrintf&#40;"\n Press CIRCLE to try to go back to the XMB"&#41;;

    for &#40;;;&#41; &#123;
        sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
        if &#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; &#123;
            break;
        &#125;
        sceKernelDelayThread&#40;100000&#41;;
    &#125;
    sceKernelExitGame&#40;&#41;;
&#125;

void initExceptionHandler&#40;&#41; &#123;
    SceKernelLMOption option;
    int args&#91;2&#93;, fd, modid;

    memset&#40;&option, 0, sizeof &#40;option&#41;&#41;;
    option.size = sizeof &#40;option&#41;;
    option.mpidtext = PSP_MEMORY_PARTITION_KERNEL;
    option.mpiddata = PSP_MEMORY_PARTITION_KERNEL;
    option.position = 0;
    option.access = 1;

    if &#40;&#40;modid = sceKernelLoadModule&#40;"exception.prx", 0, &option&#41;&#41; >= 0&#41; &#123;
        args&#91;0&#93; = &#40;int&#41; ExceptionHandler;
        args&#91;1&#93; = &#40;int&#41; & exception_regs;
        sceKernelStartModule&#40;modid, 8, args, &fd, NULL&#41;;
    &#125;
&#125;

Code: Select all

TARGET = bsodplugin
OBJS = ../utility/exception.o main.o

#To build for custom firmware&#58;
BUILD_PRX = 1

#CFLAGS = -O3 -G0 -Wall
CFLAGS = -O3 -frename-registers -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;
LIBDIR =

LIBS = 
LDFLAGS =

#PSP_EBOOT_ICON = ICON0.PNG
PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build_prx.mak
the exception.prx is the same as the one from sakya.

Why doesn't give this a nice BSOD on all crashes? (in this prx form it doesn't give any BSOD)

Code: Select all

int main&#40;&#41;&#123;
     SetupCallbacks&#40;&#41;;
     makeNiceGame&#40;&#41;;
     sceKernelExitGame&#40;&#41;;
&#125;
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

initExceptionHandler() is loading and starting the prx, but your module_start() is calling initExceptionHandler() making it recursive. Instead, initExceptionHandler() should be setting the exception vector, not loading the already loaded module.
jojojoris
Posts: 255
Joined: Sun Mar 30, 2008 4:06 am

Post by jojojoris »

maybe i don't understand you but i already have 2 prxes. bsodplugin.prx should be the one which load the exception.prx. the source is the source of bsodplugin.prx

and what do you mean with exception vector?

Code: Select all

int main&#40;&#41;&#123;
     SetupCallbacks&#40;&#41;;
     makeNiceGame&#40;&#41;;
     sceKernelExitGame&#40;&#41;;
&#125;
a_noob
Posts: 97
Joined: Sun Sep 17, 2006 8:33 am
Location: _start: jr 0xDEADBEEF

Post by a_noob »

THis has been done before, but not quite as a plugin I don't think, in pspradio and many other apps. The functions you need are already coded for you.

int pspDebugInstallErrorHandler (PspDebugErrorHandler handler)
Install an error handler to catch unhandled exceptions.
void pspDebugDumpException (PspDebugRegBlock *regs)
Dump an exception to screen using the pspDebugScreen functions.

all of the debug library docu is here http://psp.jim.sh/pspsdk-doc/group__Debug.html

So your code becomes greatly simplified, all you do is install a error handler and have that in turn call pspDebugDumpException, personally I would also have it write the exception to a file, so you can more easily send it to a developer. This will print all the registers along with the section that caused the error and the bad addr etc, can be quite helpfull.

Code: Select all

.øOº'ºOø.
'ºOo.oOº'
jojojoris
Posts: 255
Joined: Sun Mar 30, 2008 4:06 am

Post by jojojoris »

Thanks i will try that out.

I want it in the screen because the windows BSOD is cool.

EDIT:
wasn't pspDebugInstallErrorHandler kernel 1.50 only???

Code: Select all

int main&#40;&#41;&#123;
     SetupCallbacks&#40;&#41;;
     makeNiceGame&#40;&#41;;
     sceKernelExitGame&#40;&#41;;
&#125;
a_noob
Posts: 97
Joined: Sun Sep 17, 2006 8:33 am
Location: _start: jr 0xDEADBEEF

Post by a_noob »

jojojoris wrote:I want it in the screen because the windows BSOD is cool.
I'll take some of whatever you are smoking.

Also im not sure if its 1.50 only. Its possible.

Code: Select all

.øOº'ºOø.
'ºOo.oOº'
sakya
Posts: 190
Joined: Fri Apr 28, 2006 5:48 pm
Contact:

Post by sakya »

Hi! :)
jojojoris wrote:wasn't pspDebugInstallErrorHandler kernel 1.50 only???
So it seem (I didn't test it):
http://forums.ps2dev.org/viewtopic.php? ... on+handler

Ciaooo
Sakya
Post Reply