ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
callstack.c File Reference

Go to the source code of this file.

Data Structures

struct  ReturnCachePtr
 

Macros

#define RESTORE_RETURNVAL   0x8fbf0000
 
#define RESTORE_RETURNVAL_MASK   0xffff0000
 
#define RESTORE_RETURNVAL2   0xdfbf0000
 
#define RESTORE_RETURNVAL3   0x7bbf0000
 
#define ADJUST_STACKP_C   0x27bd0000
 
#define ADJUST_STACKP_C_MASK   0xffff0000
 
#define ADJUST_STACKP_V   0x03a1e821
 
#define ADJUST_STACKP_V_MASK   0xffffffff
 
#define SET_UPPER_C   0x3c010000
 
#define SET_UPPER_C_MASK   0xffff0000
 
#define OR_LOWER_C   0x34210000
 
#define OR_LOWER_C_MASK   0xffff0000
 
#define SET_LOWER_C   0x34010000
 
#define SET_LOWER_C_MASK   0xffff0000
 
#define RETURN   0x03e00008
 
#define CALL(f)   (0x0c000000 | (((int) (f)) >> 2))
 
#define HASH_SIZE   256
 
#define HASH(ra)   ((((int) (ra)) >> 2) & (HASH_SIZE - 1))
 
#define TRUE   1
 
#define FALSE   0
 

Typedefs

typedef int Bool
 

Functions

unsigned int * ps2GetReturnAddress ()
 
unsigned int * ps2GetStackPointer ()
 
int main ()
 
void ps2GetStackTrace (unsigned int *results, int max)
 

Variables

static ReturnCacheRec returnCache [HASH_SIZE]
 

Macro Definition Documentation

◆ ADJUST_STACKP_C

#define ADJUST_STACKP_C   0x27bd0000

Definition at line 56 of file callstack.c.

◆ ADJUST_STACKP_C_MASK

#define ADJUST_STACKP_C_MASK   0xffff0000

Definition at line 57 of file callstack.c.

◆ ADJUST_STACKP_V

#define ADJUST_STACKP_V   0x03a1e821

Definition at line 62 of file callstack.c.

◆ ADJUST_STACKP_V_MASK

#define ADJUST_STACKP_V_MASK   0xffffffff

Definition at line 63 of file callstack.c.

◆ CALL

#define CALL (   f)    (0x0c000000 | (((int) (f)) >> 2))

Definition at line 86 of file callstack.c.

◆ FALSE

#define FALSE   0

Definition at line 112 of file callstack.c.

◆ HASH

#define HASH (   ra)    ((((int) (ra)) >> 2) & (HASH_SIZE - 1))

Definition at line 104 of file callstack.c.

◆ HASH_SIZE

#define HASH_SIZE   256

Definition at line 93 of file callstack.c.

◆ OR_LOWER_C

#define OR_LOWER_C   0x34210000

Definition at line 74 of file callstack.c.

◆ OR_LOWER_C_MASK

#define OR_LOWER_C_MASK   0xffff0000

Definition at line 75 of file callstack.c.

◆ RESTORE_RETURNVAL

#define RESTORE_RETURNVAL   0x8fbf0000

Definition at line 40 of file callstack.c.

◆ RESTORE_RETURNVAL2

#define RESTORE_RETURNVAL2   0xdfbf0000

Definition at line 46 of file callstack.c.

◆ RESTORE_RETURNVAL3

#define RESTORE_RETURNVAL3   0x7bbf0000

Definition at line 51 of file callstack.c.

◆ RESTORE_RETURNVAL_MASK

#define RESTORE_RETURNVAL_MASK   0xffff0000

Definition at line 41 of file callstack.c.

◆ RETURN

#define RETURN   0x03e00008

Definition at line 84 of file callstack.c.

◆ SET_LOWER_C

#define SET_LOWER_C   0x34010000

Definition at line 80 of file callstack.c.

◆ SET_LOWER_C_MASK

#define SET_LOWER_C_MASK   0xffff0000

Definition at line 81 of file callstack.c.

◆ SET_UPPER_C

#define SET_UPPER_C   0x3c010000

Definition at line 68 of file callstack.c.

◆ SET_UPPER_C_MASK

#define SET_UPPER_C_MASK   0xffff0000

Definition at line 69 of file callstack.c.

◆ TRUE

#define TRUE   1

Definition at line 109 of file callstack.c.

Typedef Documentation

◆ Bool

typedef int Bool

Definition at line 106 of file callstack.c.

Function Documentation

◆ main()

int main ( )

Definition at line 45 of file callstacktest.c.

46 {
47  testFunction1();
48 
49  while(1)
50  {
51  }
52 
53  return 0;
54 }
int testFunction1()
Definition: callstacktest.c:40

References actAlign, framebuffer_t::address, zbuffer_t::address, ARRAY_ENTRIES, padButtonStatus::btns, CreateSave(), packet_t::data, data, dma_channel_fast_waits(), DMA_CHANNEL_GIF, dma_channel_initialize(), dma_channel_send_chain(), dma_channel_send_normal(), dma_channel_shutdown(), DMA_CHANNEL_toIPU, dma_wait_fast(), DOUBLE_BYTE, draw_clear(), draw_init_env(), draw_setup_environment(), zbuffer_t::enable, fontstudio_load_ini(), fontstudio_parse_ini(), fontstudio_unload_ini(), fontx_load(), fontx_unload(), GRAPH_ALIGN_BLOCK, GRAPH_ALIGN_PAGE, graph_initialize(), graph_shutdown(), graph_vram_allocate(), graph_vram_free(), graph_wait_vsync(), GS_PSM_32, framebuffer_t::height, impress, init_drawing_environment(), init_gs(), init_texture(), InitCB(), initializePad(), krom_k, krom_u, LoadModules(), loadModules(), InitCBParam::m_DrawPck, InitCBParam::m_pData, InitCBParam::m_pInfo, InitCBParam::m_TexAddr, InitCBParam::m_XFerPck, framebuffer_t::mask, zbuffer_t::mask, MAX_SIZE, MC_ATTR_SUBDIR, mc_Format, mc_Free, mc_Type, MC_TYPE_MC, MC_TYPE_XMC, mcDir, mcGetDir(), mcGetInfo(), mcInit(), mcSync(), zbuffer_t::method, MPEG_BITSTREAM_FILE, MPEG_Destroy(), MPEG_Initialize(), MPEG_Picture, new_pad, NULL, old_pad, packet, packet_free(), packet_init(), PACKET_NORMAL, PAD_CIRCLE, PAD_CROSS, PAD_DOWN, PAD_L1, PAD_L2, PAD_L3, PAD_LEFT, PAD_R1, PAD_R2, PAD_R3, PAD_RIGHT, PAD_SELECT, PAD_SQUARE, PAD_START, PAD_STATE_DISCONN, PAD_STATE_FINDCTP1, PAD_STATE_STABLE, PAD_TRIANGLE, PAD_UP, padBuf, paddata, padEnterPressMode(), padExitPressMode(), padGetPortMax(), padGetSlotMax(), padGetState(), padInit(), padPortOpen(), padRead(), padSetActDirect(), port, framebuffer_t::psm, render(), padButtonStatus::rjoy_h, run_demo(), s_MPEGDataSize, s_pMPEGData, s_pTransferPtr, sceSdInit(), sceSdSetParam(), SD_PARAM_MVOLL, SD_PARAM_MVOLR, SD_VOICE, SetDMA(), SifExitRpc(), SifInitRpc(), SifLoadModule(), SINGLE_BYTE, SleepThread(), slot, sndStreamOpen(), sndStreamPlay(), STREAM_END_CLOSE, STREAM_STEREO, testFunction1(), padButtonStatus::triangle_p, framebuffer_t::width, and zbuffer_t::zsm.

Referenced by ps2GetStackTrace().

◆ ps2GetReturnAddress()

unsigned int* ps2GetReturnAddress ( )

Referenced by ps2GetStackTrace().

◆ ps2GetStackPointer()

unsigned int* ps2GetStackPointer ( )

Referenced by ps2GetStackTrace().

◆ ps2GetStackTrace()

void ps2GetStackTrace ( unsigned int *  results,
int  max 
)

Definition at line 119 of file callstack.c.

120 {
121  unsigned int* ra;
122  unsigned int* ra_limit;
123  unsigned int* sp;
124  unsigned int inst;
125  unsigned int mainCall;
126  unsigned short const_upper;
127  unsigned short const_lower;
128  int ra_offset;
129  int sp_adjust;
130  Bool found_ra_offset, found_sp_adjust;
131  Bool found_const_upper, found_const_lower;
132  ReturnCachePtr rc;
133 
136  mainCall = CALL(main);
137 
138  while (ra && max)
139  {
140  rc = &returnCache[HASH(ra)];
141  if (rc->returnAddress != ra)
142  {
143  found_ra_offset = FALSE;
144  found_sp_adjust = FALSE;
145  found_const_upper = FALSE;
146  found_const_lower = FALSE;
147  const_upper = 0;
148  const_lower = 0;
149  rc->returnAddress = ra;
150  ra_limit = (unsigned int *) 0x200000;
151  ra_offset = 0;
152  sp_adjust = -1;
153 
154  while ((!found_ra_offset || !found_sp_adjust) && ra < ra_limit)
155  {
156  inst = *ra;
157  /* look for the offset of the PC in the stack frame */
159  {
160  ra_offset = inst & ~RESTORE_RETURNVAL_MASK;
161  found_ra_offset = TRUE;
162  }
163  else if ((inst & RESTORE_RETURNVAL_MASK) == RESTORE_RETURNVAL2)
164  {
165  ra_offset = inst & ~RESTORE_RETURNVAL_MASK;
166  found_ra_offset = TRUE;
167  }
168  else if ((inst & RESTORE_RETURNVAL_MASK) == RESTORE_RETURNVAL3)
169  {
170  ra_offset = inst & ~RESTORE_RETURNVAL_MASK;
171  found_ra_offset = TRUE;
172  }
173  else if ((inst & ADJUST_STACKP_C_MASK) == ADJUST_STACKP_C)
174  {
175  sp_adjust = inst & ~ADJUST_STACKP_C_MASK;
176  found_sp_adjust = TRUE;
177  }
178  else if ((inst & ADJUST_STACKP_V_MASK) == ADJUST_STACKP_V)
179  {
180  sp_adjust = 0;
181  found_sp_adjust = TRUE;
182  }
183  else if ((inst & SET_UPPER_C_MASK) == SET_UPPER_C)
184  {
185  const_upper = inst & ~SET_UPPER_C_MASK;
186  const_lower = 0;
187  found_const_upper = TRUE;
188  }
189  else if ((inst & OR_LOWER_C_MASK) == OR_LOWER_C)
190  {
191  const_lower = inst & ~OR_LOWER_C_MASK;
192  found_const_lower = TRUE;
193  }
194  else if ((inst & SET_LOWER_C_MASK) == SET_LOWER_C)
195  {
196  const_lower = inst & ~SET_LOWER_C_MASK;
197  const_upper = 0;
198  found_const_lower = TRUE;
199  }
200  else if (inst == RETURN)
201  ra_limit = ra + 2;
202  ra++;
203  }
204 
205  if (sp_adjust == 0 && (found_const_upper || found_const_lower))
206  sp_adjust = (const_upper << 16) | const_lower;
207  rc->raOffset = ra_offset;
208  rc->spAdjust = sp_adjust;
209  }
210  /* if something went wrong, punt */
211  if (rc->spAdjust <= 0)
212  {
213  *results++ = 0;
214  break;
215  }
216 
217  ra = (unsigned int *) sp[rc->raOffset>>2];
218  sp += rc->spAdjust >> 2;
219 
220  if (ra == 0)
221  {
222  *results++ = 0;
223  break;
224  }
225 
226  *results++ = ((unsigned int) ra) - 8;
227  if (ra[-2] == mainCall)
228  {
229  *results++ = 0;
230  break;
231  }
232  max--;
233  }
234 }
#define ra
Definition: as_reg_compat.h:87
#define sp
Definition: as_reg_compat.h:85
static ReturnCacheRec returnCache[HASH_SIZE]
Definition: callstack.c:102
#define ADJUST_STACKP_C
Definition: callstack.c:56
#define ADJUST_STACKP_V_MASK
Definition: callstack.c:63
#define RESTORE_RETURNVAL_MASK
Definition: callstack.c:41
#define CALL(f)
Definition: callstack.c:86
#define RESTORE_RETURNVAL3
Definition: callstack.c:51
#define OR_LOWER_C
Definition: callstack.c:74
#define RESTORE_RETURNVAL2
Definition: callstack.c:46
#define HASH(ra)
Definition: callstack.c:104
#define RETURN
Definition: callstack.c:84
#define RESTORE_RETURNVAL
Definition: callstack.c:40
#define OR_LOWER_C_MASK
Definition: callstack.c:75
#define TRUE
Definition: callstack.c:109
#define FALSE
Definition: callstack.c:112
unsigned int * ps2GetReturnAddress()
#define SET_LOWER_C
Definition: callstack.c:80
unsigned int * ps2GetStackPointer()
#define SET_LOWER_C_MASK
Definition: callstack.c:81
#define SET_UPPER_C_MASK
Definition: callstack.c:69
#define ADJUST_STACKP_C_MASK
Definition: callstack.c:57
#define ADJUST_STACKP_V
Definition: callstack.c:62
int main()
Definition: callstacktest.c:45
#define SET_UPPER_C
Definition: callstack.c:68
int Bool
Definition: callstack.c:106
#define max(a, b)
Definition: standard.h:57

References ADJUST_STACKP_C, ADJUST_STACKP_C_MASK, ADJUST_STACKP_V, ADJUST_STACKP_V_MASK, CALL, FALSE, HASH, main(), max, OR_LOWER_C, OR_LOWER_C_MASK, ps2GetReturnAddress(), ps2GetStackPointer(), ra, RESTORE_RETURNVAL, RESTORE_RETURNVAL2, RESTORE_RETURNVAL3, RESTORE_RETURNVAL_MASK, RETURN, returnCache, SET_LOWER_C, SET_LOWER_C_MASK, SET_UPPER_C, SET_UPPER_C_MASK, sp, and TRUE.

Referenced by TestFunction3().

Variable Documentation

◆ returnCache

ReturnCacheRec returnCache[HASH_SIZE]
static

Definition at line 102 of file callstack.c.

Referenced by ps2GetStackTrace().