forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Exception Handler for Kernel 3.xx

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
sakya



Joined: 28 Apr 2006
Posts: 190

PostPosted: Fri Jan 04, 2008 8:57 pm    Post subject: Exception Handler for Kernel 3.xx Reply with quote

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:
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:
#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
Back to top
View user's profile Send private message Visit poster's website
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sat Jan 05, 2008 1:28 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
sakya



Joined: 28 Apr 2006
Posts: 190

PostPosted: Sat Jan 05, 2008 1:58 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
crazyc



Joined: 17 Jun 2005
Posts: 410

PostPosted: Sat Jan 05, 2008 1:59 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat Jan 05, 2008 7:12 am    Post subject: Reply with quote

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. :)
Back to top
View user's profile Send private message AIM Address
SamuraiX



Joined: 31 Jan 2006
Posts: 76
Location: USA

PostPosted: Sat Jan 05, 2008 1:40 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
crazyc



Joined: 17 Jun 2005
Posts: 410

PostPosted: Sun Jan 06, 2008 12:40 am    Post subject: Reply with quote

Two things. In your init code, you have
Code:
PspDebugRegBlock *exception_regs;
That should not be a pointer, and I should have put
Code:
/*
 * 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 (c) 2005 James Forshaw <tyranid@gmail.com>
 *
 * $Id$
 */
at the top of exception_asm.S, so if you could do that. Also, an idea. Add
Code:
pspDebugScreenPrintf(" / %s.text + %X\n", module_info.modname, regs->epc-(int)&_ftext);
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.
Back to top
View user's profile Send private message
SamuraiX



Joined: 31 Jan 2006
Posts: 76
Location: USA

PostPosted: Sat Jan 12, 2008 12:27 pm    Post subject: Reply with quote

crazyc wrote:
Also, an idea. Add
Code:
pspDebugScreenPrintf(" / %s.text + %X\n", module_info.modname, regs->epc-(int)&_ftext);
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.
Back to top
View user's profile Send private message Visit poster's website
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sat Jan 12, 2008 9:37 pm    Post subject: Reply with quote

SamuraiX wrote:
crazyc wrote:
Also, an idea. Add
Code:
pspDebugScreenPrintf(" / %s.text + %X\n", module_info.modname, regs->epc-(int)&_ftext);
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).
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sat Jan 12, 2008 11:32 pm    Post subject: Reply with quote

Of course in prxtool you can pass the --reloc parameter and get it to the location it really is in memory ;)
Back to top
View user's profile Send private message
M.Jackson



Joined: 10 Sep 2007
Posts: 85

PostPosted: Sat Jan 19, 2008 3:28 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sat Jan 19, 2008 3:53 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
sakya



Joined: 28 Apr 2006
Posts: 190

PostPosted: Sun Feb 17, 2008 11:10 pm    Post subject: Reply with quote

Hi! :)

Can someone help me with this exception infos, please?
Code:
Exception details:

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


I tried to disasm my prx with this command:
Code:
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:
system/exception.o: In function `ExceptionHandler':
system/exception.c:47: relocation truncated to fit: R_MIPS_GPREL16 against `_ftext'
system/exception.c:67: relocation truncated to fit: R_MIPS_GPREL16 against `_ftext'
gui/menu.o: In function `drawMenu':
gui/menu.c:112: relocation truncated to fit: R_MIPS_GPREL16 against `osl_curFont'
gui/menu.c:99: relocation truncated to fit: R_MIPS_GPREL16 against `osl_curFont'
gui/menu.c:108: relocation truncated to fit: R_MIPS_GPREL16 against `osl_curFont'
gui/common.o: In function `drawToolbars':
gui/common.c:158: relocation truncated to fit: R_MIPS_GPREL16 against `osl_curFont'
gui/common.o: In function `drawButtonBar':
gui/common.c:194: relocation truncated to fit: R_MIPS_GPREL16 against `osl_curFont'
gui/common.o: In function `drawConfirm':
gui/common.c:272: relocation truncated to fit: R_MIPS_GPREL16 against `osl_curFont'
gui/common.o: In function `drawWait':
gui/common.c:289: relocation truncated to fit: R_MIPS_GPREL16 against `osl_curFont'
gui/common.o: In function `drawHelp':
gui/common.c:327: relocation truncated to fit: R_MIPS_GPREL16 against `osl_curFont'
gui/gui_fileBrowser.o: In function `drawUSBmessage':
gui/gui_fileBrowser.c:52: additional relocation overflows omitted from the output
collect2: ld returned 1 exit status
psp-make: *** [lightmp3.elf] Error 1
Esecuzione terminata

How can I fix those errors?

Many thanks in advance. :)

Ciaooo
Sakya
Back to top
View user's profile Send private message Visit poster's website
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Mon Feb 18, 2008 12:11 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
sakya



Joined: 28 Apr 2006
Posts: 190

PostPosted: Mon Feb 18, 2008 12:52 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Mon Jun 15, 2009 5:29 am    Post subject: Reply with quote

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

Code:
relocation truncated to fit: R_MIPS_GPREL16 against `_ftext'


full:
Code:
exception/exception.o: In function `ExceptionHandler':
exception.c:(.text+0xa8): relocation truncated to fit: R_MIPS_GPREL16 against `_ftext'
collect2: ld returned 1 exit status
make: *** [make.elf] Error 1
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Mon Jun 15, 2009 6:47 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Mon Jun 15, 2009 7:00 am    Post subject: Reply with quote

still cant find but here is the makefile

Code:
PSPSDK=$(shell psp-config --pspsdk-path)
PSPBIN = $(PSPSDK)/../bin
MAKE = $(shell make)

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 \
      $(IMPORTS) \
      $(EVENTS) \
      $(EXPORTS)

INCDIR =
LIBDIR =

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

LDFLAGS =

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

CFLAGS := $(INCS)

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 = $(YOURLIBS) $(STDLIBS)

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 $(PSPSDK)/lib/build.mak

dump: $(shell psp-objdump -t make.elf > objdump.txt)

s_file:   
   $(shell psp-build-exports -s exports.exp)

install:
   mkdir -p $(MAIN_LOC)
   cp -fT EBOOT.PBP $(MAIN_DEST)
   
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Mon Jun 15, 2009 10:10 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Mon Jun 15, 2009 12:59 pm    Post subject: Reply with quote

tried all of that to no aval it still doesnt work even removed both
Back to top
View user's profile Send private message MSN Messenger
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Tue Jun 16, 2009 2:35 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Tue Jun 16, 2009 5:15 am    Post subject: Reply with quote

Well, take a look at the DaedalusX64 makefile - DX64 uses the exception handler as well.
Back to top
View user's profile Send private message AIM Address
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Wed Jun 17, 2009 3:18 am    Post subject: Reply with quote

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

Code:
host0:/> Exception - Bus error (data)
Thread ID - 0x04DB5479
Th Name   - user_main
Module ID - 0x00CDFA6D
Mod Name  - "desktop"
EPC       - 0x08F5565C
Cause     - 0x1000001C
BadVAddr  - 0x80020166
Status    - 0x20008613
zr:0x00000000 at:0x09040000 v0:0x00000000 v1:0x00000078
a0:0x09046890 a1:0x00000001 a2:0x09F7FE10 a3:0x00000154
t0:0x00000078 t1:0x00000000 t2:0x00000000 t3:0x00000000
t4:0x0000003F t5:0x0904C490 t6:0x00000000 t7:0x00000078
s0:0x0904C450 s1:0x09020000 s2:0x00000000 s3:0x09F7FEE0
s4:0x09030000 s5:0x00000013 s6:0xDEADBEEF s7:0xDEADBEEF
t8:0x00000154 t9:0x880107E0 k0:0x09F7FF00 k1:0x00000000
gp:0x09026C00 sp:0x09F7FDE8 fp:0x09F7FEA0 ra:0x08F55950
calc $epc-$mod
Unknown register 'mod'
Error could not calculate address
 
Back to top
View user's profile Send private message MSN Messenger
willow :--)



Joined: 13 Jan 2007
Posts: 126

PostPosted: Mon Sep 14, 2009 2:14 pm    Post subject: Reply with quote

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/browse/trunk/JGE/JGE/src/main.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 :)
_________________
Wagic. Play that card game against an AI on your PSP
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group