Exception Handler for Kernel 3.xx

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

Moderators: cheriff, TyRaNiD

Post Reply
sakya
Posts: 190
Joined: Fri Apr 28, 2006 5:48 pm
Contact:

Exception Handler for Kernel 3.xx

Post by sakya »

Hi! :)

First: this is not my code, I simply got the code from crazyc and SamuraiX and made it easy to use (at least for me). ;)
Original thread: http://forums.ps2dev.org/viewtopic.php?t=9564
Maybe it's usefull for someone else...

What is this?
This is a prx to handle exception on PSP, when an exception is raised some usefull information are shown on the display (and can be dumped to a text file).

How to compile it
Enter the prx directory and simply type

Code: Select all

make
This way you have the exception.prx file.

How to use it
Check the test program in the test directory.
In your program you have only to include exception.h and initialize the handler.

Code: Select all

#include "../utility/exception.h"
initExceptionHandler();
Copy exception.prx in the same directory where your EBOOT is.

Download: http://www.sakya.it/downloads/exceptionHandler.rar

Ciaooo
Sakya
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

Why do you use your own exception code (well actually just taken from other sources anyway) when you can just call pspDebugInstallErrorHandler from the sdk, geez some people :P
sakya
Posts: 190
Joined: Fri Apr 28, 2006 5:48 pm
Contact:

Post by sakya »

Hi! :)
TyRaNiD wrote:Why do you use your own exception code (well actually just taken from other sources anyway) when you can just call pspDebugInstallErrorHandler from the sdk, geez some people :P
Sorry, I saw crazyc code and thought that pspDebugInstallErrorHandler wasn't working on newer firmwares (the sdk sample is 1.50 only) but didn't test it.
If pspDebugInstallErrorHandler works on all firmware than the prx can be simpler. :)

Ciaooo
Sakya
crazyc
Posts: 408
Joined: Fri Jun 17, 2005 10:13 am

Post by crazyc »

TyRaNiD wrote:Why do you use your own exception code (well actually just taken from other sources anyway) when you can just call pspDebugInstallErrorHandler from the sdk, geez some people :P
Are you sure? I'm fairly sure it won't work unless you can load a kernel PRX into the user memory partition, which admittedly I didn't try, because pspDebugInstallErrorHandler erets to _pspDebugTrapEntry, which will be part of the prx. If the exception occurs in user mode, the eret will return to user mode but _pspDebugTrapEntry will be in the kernel partition. Also, 3.71 mixes the nid for sceKernelRegisterDefaultExceptionHandler.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

SNES9xTYL has pspDebugInstallErrorHandler in its init code, but it had to be disabled with #ifdef to get the app to work in 3.xx. You cannot successfully call that function from a user mode prx in 3.xx - at least not from what I've seen. Having this as a prx with a simple init call to set it up is better for apps in any case. That allows the exception handling to be independent of the app, and simple to use in the app. If any more changes occur with the exception handling, it also means that it can be handled merely by changing the prx and not the entire app.

I plan to start using this in all my stuff. Many thanks to crazyc and sakya for this useful tool. :)
SamuraiX
Posts: 76
Joined: Tue Jan 31, 2006 6:28 am
Location: USA
Contact:

Post by SamuraiX »

If I remember correctly it wasn't the fact you couldn't use pspDebugInstallErrorHandler(). It was the fact that you couldn't use pspDebugScreenPrintf() in a kernel mode prx for the dump. There would be linker errors for missing libraries for debug printing to screen.
crazyc
Posts: 408
Joined: Fri Jun 17, 2005 10:13 am

Post by crazyc »

Two things. In your init code, you have

Code: Select all

PspDebugRegBlock *exception_regs;
That should not be a pointer, and I should have put

Code: Select all

/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * pspexception_asm.S - Basic exception handler for applications.
 *
 * Copyright &#40;c&#41; 2005 James Forshaw <[email protected]>
 *
 * $Id$
 */
at the top of exception_asm.S, so if you could do that. Also, an idea. Add

Code: Select all

pspDebugScreenPrintf&#40;" / %s.text + %X\n", module_info.modname, regs->epc-&#40;int&#41;&_ftext&#41;;
to your ExceptionHandler to print the offset of the exception to the start of the text section as the relocated addresses are kind of useless.
SamuraiX
Posts: 76
Joined: Tue Jan 31, 2006 6:28 am
Location: USA
Contact:

Post by SamuraiX »

crazyc wrote:Also, an idea. Add

Code: Select all

pspDebugScreenPrintf&#40;" / %s.text + %X\n", module_info.modname, regs->epc-&#40;int&#41;&_ftext&#41;;
to your ExceptionHandler to print the offset of the exception to the start of the text section as the relocated addresses are kind of useless.

I'm a little confused. I've purposely created an exception and the RA value pointed to the last call prior to the exception and the EPC value was pointing at the exception itself. Now could you explain a little further on why we would need the start of the text section? Also why do you label the module with '.text' ? Why would the relocated address be useless?


When I added the additional printing above the result did not make sense to me.
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

SamuraiX wrote:
crazyc wrote:Also, an idea. Add

Code: Select all

pspDebugScreenPrintf&#40;" / %s.text + %X\n", module_info.modname, regs->epc-&#40;int&#41;&_ftext&#41;;
to your ExceptionHandler to print the offset of the exception to the start of the text section as the relocated addresses are kind of useless.

I'm a little confused. I've purposely created an exception and the RA value pointed to the last call prior to the exception and the EPC value was pointing at the exception itself. Now could you explain a little further on why we would need the start of the text section? Also why do you label the module with '.text' ? Why would the relocated address be useless?


When I added the additional printing above the result did not make sense to me.
just use psp-objdump/prxtool for disassembly code, and you'll understand why crazyc's advise is a good one and cheap to do so (just a line to add).
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

Of course in prxtool you can pass the --reloc parameter and get it to the location it really is in memory ;)
M.Jackson
Posts: 85
Joined: Mon Sep 10, 2007 6:37 pm
Contact:

Post by M.Jackson »

This is probably an unrelated question. But is this installable exception handler a "true" exception handler for Allegrex (which suggests it can handle all exceptions/traps like clock interrupt or data/instruction bus error), or just one of the cascaded exception handlers managed by the firmware's exception/trap subsystem?

I am asking this because I saw there were code preserving the CPU status at the beginning of the handler, and if it was a cascaded handler it probably would not need to do so as the firmware's trap subsystem would have had the status preserved instead and passed it to our installed handlers, I guess.
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

No you need to handle the register saving yourself the kernel doesn't do it for you even though this uses the default exception handler.

Basically other types of exception handle register saving in the different ways, for example a syscall doesn't really need to preserve anything bar enough to get it to return.
sakya
Posts: 190
Joined: Fri Apr 28, 2006 5:48 pm
Contact:

Post by sakya »

Hi! :)

Can someone help me with this exception infos, please?

Code: Select all

Exception details&#58;

Exception - Breakpoint
EPC       - 0880CF6C / LightMP3.text + 00008F6C
Cause     - 10000024
Status    - 60008613
BadVAddr  - 00000000
zr&#58;00000000 at&#58;2008FF00 v0&#58;00000000 v1&#58;00000001
a0&#58;09FBD3C0 a1&#58;00000000 a2&#58;BDB4A78E a3&#58;88090000
t0&#58;48C6334C t1&#58;00000002 t2&#58;88090000 t3&#58;08C6337C
t4&#58;91A2B3C5 t5&#58;91A20000 t6&#58;08A80000 t7&#58;08920000
s0&#58;00000000 s1&#58;00000005 s2&#58;00000064 s3&#58;08A90000
s4&#58;0880CF44 s5&#58;08AC0000 s6&#58;09FBD5FC s7&#58;00000050
t8&#58;088F0000 t9&#58;088F0000 k0&#58;09FBFF00 k1&#58;00000000
gp&#58;08919A60 sp&#58;09FBD3A8 fp&#58;00000028 ra&#58;08817120
I tried to disasm my prx with this command:

Code: Select all

prxtool --disasm -r 00008F6C lightmp3.prx
but this doesen't seem to help me (I don't know ASM...). How can I trace the exception to understand where it's raised?

And another question...
I tried to compile my app with -g to use psp-addr2line to get the line of code which raised the exception (if I understant well) but I get this errors:

Code: Select all

system/exception.o&#58; In function `ExceptionHandler'&#58;
system/exception.c&#58;47&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `_ftext'
system/exception.c&#58;67&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `_ftext'
gui/menu.o&#58; In function `drawMenu'&#58;
gui/menu.c&#58;112&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `osl_curFont'
gui/menu.c&#58;99&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `osl_curFont'
gui/menu.c&#58;108&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `osl_curFont'
gui/common.o&#58; In function `drawToolbars'&#58;
gui/common.c&#58;158&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `osl_curFont'
gui/common.o&#58; In function `drawButtonBar'&#58;
gui/common.c&#58;194&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `osl_curFont'
gui/common.o&#58; In function `drawConfirm'&#58;
gui/common.c&#58;272&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `osl_curFont'
gui/common.o&#58; In function `drawWait'&#58;
gui/common.c&#58;289&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `osl_curFont'
gui/common.o&#58; In function `drawHelp'&#58;
gui/common.c&#58;327&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `osl_curFont'
gui/gui_fileBrowser.o&#58; In function `drawUSBmessage'&#58;
gui/gui_fileBrowser.c&#58;52&#58; additional relocation overflows omitted from the output
collect2&#58; ld returned 1 exit status
psp-make&#58; *** &#91;lightmp3.elf&#93; Error 1
Esecuzione terminata
How can I fix those errors?

Many thanks in advance. :)

Ciaooo
Sakya
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

a breakpoint exception? That is usually done by an application on purpose, or
generated by the compiler when there is a division by zero.

That report isn't enough. Post some fragment of asm at least from text+0x8f50 to 0x8f80
sakya
Posts: 190
Joined: Fri Apr 28, 2006 5:48 pm
Contact:

Post by sakya »

Hi! :)
moonlight wrote:a breakpoint exception? That is usually done by an application on purpose, or generated by the compiler when there is a division by zero.
Many thanks, I solved the problem thanks to your advice. :)
I didn't understand what can cause a "beakpoint" (sounded strange to me).
It was a division by zero (wasn't difficult to find it now I know what to search for). :)

Many thanska gain.

Ciaooo
Sakya
coolkehon
Posts: 355
Joined: Mon Oct 20, 2008 5:44 am

Post by coolkehon »

got that same error above with the relocation and i cant figure out how to fix any suggestion would be appreciated

Code: Select all

relocation truncated to fit&#58; R_MIPS_GPREL16 against `_ftext'
full:

Code: Select all

exception/exception.o&#58; In function `ExceptionHandler'&#58;
exception.c&#58;&#40;.text+0xa8&#41;&#58; relocation truncated to fit&#58; R_MIPS_GPREL16 against `_ftext'
collect2&#58; ld returned 1 exit status
make&#58; *** &#91;make.elf&#93; Error 1
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Those kinds of errors tend to be from the small data segment. If you see something like -G8 in your makefile, that's telling the compiler to try to put everything <= 8 bytes in size into the small data segment. This doesn't work very well on newer versions of gcc, so you see -G0 on many projects today.
coolkehon
Posts: 355
Joined: Mon Oct 20, 2008 5:44 am

Post by coolkehon »

still cant find but here is the makefile

Code: Select all

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
PSPBIN = $&#40;PSPSDK&#41;/../bin
MAKE = $&#40;shell make&#41;

TARGET = make
 
EVENTS = events/Callbacks.o \
		 events/sys_redraw.o \
		 events/control_callbacks.o 
		 
IMPORTS = ../controls/control_bridge.o \
		  ../initconf/initconf_lib.o 
		  
EXPORTS = exports/exports.o \
		  exports/graphics.o 

OBJS = 	main.o \
		exception/exception.o \
		global.o \
		MagnificentG.o \
		Logger.o \
		MagControls.o \
		$&#40;IMPORTS&#41; \
		$&#40;EVENTS&#41; \
		$&#40;EXPORTS&#41; 

INCDIR =
LIBDIR =

#CFLAGS = -O3 -frename-registers -ffast-math -fomit-frame-pointer -g -G0 -Wall
CFLAGS = -G4 -Wall -O2 -g -G0
CXXFLAGS = $&#40;CFLAGS&#41; -fexceptions -frtti 
#-fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;  

LDFLAGS =

INCS = -I/usr/local/pspdev/include \
	   -I/usr/local/pspdev/psp/include \
	   -I/usr/local/pspdev/psp/sdk/include 

CFLAGS &#58;= $&#40;INCS&#41;

STDLIBS = 
STDLIBS += -lGL -lpspvfpu -lpspirkeyb 
STDLIBS += -lpspctrl -lpspumd -lpsprtc -lpsppower 
STDLIBS += -lpspaudio -lpsphprm -lpspgu -lpspgum 
STDLIBS += -lpspmpeg -lpsprtc -lpspsdk -lmikmod 
STDLIBS += -lpspaudiocodec -lpspatrac3 -lpng 
STDLIBS += -lAac -lFLAC -lvorbisidec -lmad 
STDLIBS += -lpspusb -lpspusbstor -lpspkubridge 
STDLIBS += -lpspsystemctrl_user -lpspinit 
STDLIBS += -lc -lm -lz -lstdc++ -ljpeg 

YOURLIBS = -lSDL_image -lSDL_gfx -lSDL_ttf -lSDL -lconfig++ -lanytype -lunzip 

LIBS = $&#40;YOURLIBS&#41; $&#40;STDLIBS&#41; 

PSP_FW_VERSION = 500
BUILD_PRX = 1
PRX_EXPORTS = exports.exp

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = MagnificentG
PSP_EBOOT_ICON = NULL

include ../locations.mak
include $&#40;PSPSDK&#41;/lib/build.mak

dump&#58; $&#40;shell psp-objdump -t make.elf > objdump.txt&#41;

s_file&#58;	
	$&#40;shell psp-build-exports -s exports.exp&#41;

install&#58;
	mkdir -p $&#40;MAIN_LOC&#41;
	cp -fT EBOOT.PBP $&#40;MAIN_DEST&#41;
	 
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

That's a really non-standard PSP project makefile. :)

I noticed you have both -G4 and -G0 in the CFLAGS line. Remove the -G4 and see what happens - I don't know which would have priority, the -G4 or the -G0. Just use one.
coolkehon
Posts: 355
Joined: Mon Oct 20, 2008 5:44 am

Post by coolkehon »

tried all of that to no aval it still doesnt work even removed both
coolkehon
Posts: 355
Joined: Mon Oct 20, 2008 5:44 am

Post by coolkehon »

so is there no solution for this and if it helps i'm using cpp for the whole project but this file is .c i externed the initExceptionHandler() function so that it would be c
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Well, take a look at the DaedalusX64 makefile - DX64 uses the exception handler as well.
coolkehon
Posts: 355
Joined: Mon Oct 20, 2008 5:44 am

Post by coolkehon »

started using psplink but now how do i i calc with the calc $epc-$mod because it says mod not found

Code: Select all

host0&#58;/> Exception - Bus error &#40;data&#41;
Thread ID - 0x04DB5479
Th Name   - user_main
Module ID - 0x00CDFA6D
Mod Name  - "desktop"
EPC       - 0x08F5565C
Cause     - 0x1000001C
BadVAddr  - 0x80020166
Status    - 0x20008613
zr&#58;0x00000000 at&#58;0x09040000 v0&#58;0x00000000 v1&#58;0x00000078
a0&#58;0x09046890 a1&#58;0x00000001 a2&#58;0x09F7FE10 a3&#58;0x00000154
t0&#58;0x00000078 t1&#58;0x00000000 t2&#58;0x00000000 t3&#58;0x00000000
t4&#58;0x0000003F t5&#58;0x0904C490 t6&#58;0x00000000 t7&#58;0x00000078
s0&#58;0x0904C450 s1&#58;0x09020000 s2&#58;0x00000000 s3&#58;0x09F7FEE0
s4&#58;0x09030000 s5&#58;0x00000013 s6&#58;0xDEADBEEF s7&#58;0xDEADBEEF
t8&#58;0x00000154 t9&#58;0x880107E0 k0&#58;0x09F7FF00 k1&#58;0x00000000
gp&#58;0x09026C00 sp&#58;0x09F7FDE8 fp&#58;0x09F7FEA0 ra&#58;0x08F55950
calc $epc-$mod
Unknown register 'mod'
Error could not calculate address
 
willow :--)
Posts: 107
Joined: Sat Jan 13, 2007 11:50 am

Post by willow :--) »

Sakya, would you recommend me to use your prx for my use case?

I have lots of stupid crashes in my application. It heavily relies on external content and I cannot control everything. I want my game to smoothly handle errors, and quit, rather than crash + having to reboot the PSP.

I use JGE, which already has exception handling code, but as it's been mentioned, this works only on 1.xx in kernel mode:
http://code.google.com/p/jge/source/bro ... in.cpp#170

I like the idea of an external prx, and just want to make sure I'm using the best || most recent || most convenient solution out there

Edit: tried it, it does exactly what I want, thanks :)
Post Reply