noob help - code crashes when exiting function

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

Moderators: cheriff, TyRaNiD

Post Reply
ronniebeck
Posts: 4
Joined: Wed Jan 04, 2006 2:40 pm

noob help - code crashes when exiting function

Post by ronniebeck »

I have some code that seems to crash when the function returns. I lack the knowledge to debug it. Can some one suggest why my code dies once the function dirList() returns?

Code: Select all

#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspumd.h>
#include <pspiofilemgr.h>
#include <string.h>
#include <stdio.h>

PSP_MODULE_INFO&#40;"Get Dir", 0, 1, 1&#41;;

#define printf pspDebugScreenPrintf


/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	sceKernelExitGame&#40;&#41;;

	return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid;

	cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;

	sceKernelSleepThreadCB&#40;&#41;;

	return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
	int thid = 0;

	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;

typedef struct listDir&#123;
	int entCount;
	char dir&#91;100&#93;&#91;1000&#93;;
&#125;listDir;


int dirList&#40;char *root, listDir *directorylist&#41;
&#123;
	int dfd, count=0;
	dfd = sceIoDopen&#40;root&#41;;
	SceIoDirent dir;
	printf&#40;"debug&#58; Entered dirList&#40;&#41;\n"&#41;;
	while&#40;sceIoDread&#40;dfd, &dir&#41; > 0&#41;
	&#123;
		if&#40;dir.d_stat.st_attr == FIO_SO_IFDIR&#41;
		&#123;
			if&#40; dir.d_name&#91;0&#93; != '.'&#41;
			&#123;
				strcpy&#40;directorylist->dir&#91;count&#93;,dir.d_name&#41;;
				printf&#40;"debug&#58; dir=%s\ndebug dir in struct=%s\n",dir.d_name,directorylist->dir&#91;count&#93;&#41;;
			&#125; else &#123;
			//printf&#40;"file&#58; %s\n",dir.d_name&#41;;
			&#125;
		&#125;
		count++;
		directorylist->entCount=count;
	&#125;
	printf&#40;"debug&#58; struct entity count is %i\n",directorylist->entCount&#41;;
	sceIoDclose &#40;dfd&#41;;
	printf&#40;"debug&#58; Leaving dirList&#40;&#41;\n"&#41;;
	return 0;
&#125;

/*int listFile&#40;char *file&#41;
&#123;
	SceIoStat fileStat;
	sceIoGetstat&#40;file, &fileStat&#41;;
	printf&#40;"File&#58; %lli",fileStat.st_size&#41;;
&#125;*/

int main&#40;&#41;
&#123;
	char dir&#91;100&#93;;
	int count=0;
	listDir mylist;
	pspDebugScreenInit&#40;&#41;;
	SetupCallbacks&#40;&#41;;
	printf&#40;"Version 0.16\n"&#41;;
	sprintf&#40;dir,"ms0&#58;/"&#41;;
	mylist.entCount=0;
	int whocares=dirList&#40;dir,&mylist&#41;;
	printf&#40;"We seem to crash here.\nTurning psp off.....but I am not sure why.\n"&#41;;
	sleep&#40;3&#41;;
	printf&#40;"Found %d objects\n",mylist.entCount&#41;;
	printf&#40;"debug&#58; struct entity count is %i\n",mylist.entCount&#41;;
	while&#40;mylist.entCount > count&#41;
	&#123;
		printf&#40;"Dir&#58; %s\n", mylist.dir&#91;count&#93;&#41;;
		count++;
	&#125;

	
	return 0;
&#125;
Fanjita
Posts: 217
Joined: Wed Sep 28, 2005 9:31 am

Post by Fanjita »

If I remember rightly (I'm posting from work without much access to reference material) you need to initialise the "size" member of the dirinfo structure. It doesn't do any harm to memset the whole thing to zero, either.

I seem to recall people posting previous problems with the sceIoDread functions when using a dirinfo structure on the stack - you might have more joy using it off the stack - but I suspect that those are all related to not initialising ".size".

This same principle, of initialising the "size" member, applies to many other functions in the API, too.

EDIT: Forgot to mention why I think this causes the crash. If you haven't set up the size of the on-stack structure, then you're wide open to stack overflow which could corrupt the on-stack return address.
Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you!
ronniebeck
Posts: 4
Joined: Wed Jan 04, 2006 2:40 pm

Post by ronniebeck »

Hmmm can you suggest where I might learn how to do that?
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

Fanjita, unfortunately there isn't actually any size member in sceIoDirEntry, well there is but it is the size of the file ;) It just seems to be a bug in the MS driver as it works fine on all other devices such as flash and umd. ronniebeck I would just suggest setting the stack SceIoDirent variable to static and it at least shouldn't crash. Of course whether this is storing up trouble for later I wouldn't like to speculate :)
jonny
Posts: 351
Joined: Thu Sep 22, 2005 5:46 pm
Contact:

Post by jonny »

experienced the same problem with sceIoDread

the solution i've found is place a static global sceIoDirEntry variable and call sceIoDread always and only with this variable (all *seems* stable now :)

otherwise it badly crash, not only with sceIoDirEntry on the stack but even if you malloc an array of sceIoDirEntry in the heap
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Works fine for me on the stack or malloced as long as I memset the structure to 0 first, ie.

Code: Select all

SceIoDirent dir;
memset&#40;&dir, 0, sizeof dir&#41;;
ok = sceIoDread&#40;dfd, &dir&#41;;
Jim
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

In general, I'd recommend using newlib functions when possible, rather than calling Sce* directly, because in many cases we've already identified and worked around these sorts of quirks. In this case, opendir/readdir was fixed here.
Post Reply