/* THESE FUNCTIONS ADDED FOR THE DeskLib 2.30 [RM/32] RELEASE.
   THEY ARE NOT PRESENT IN THE ORIGINAL v2.30 VERSION OF DESKLIB. */

/*
NB, A warning to people using CMHG IRQ veneers - I have discovered to my
cost that Desk_BackTrace functions fail to walk the stack provided by
these veneers, and so give an address exception... I suspect that the fp
pointer isn't NULL when it should be, or something.
 */


#ifndef __BackTrace_h
#define __BackTrace_h


#include <stdio.h>
#include "kernel.h"
#include "DeskLib:File.h"


#ifdef __cplusplus
	extern "C"	{
#endif


void BackTrace_Simple(file_handle fp);
/* Simple 26/32 bit compliant backtrace. Will output to the file handle given,
   or VDU if the handle is zero.
   Note - this is a DeskLib file handle, not a CLib one.
   Added by Rick, July 2015.
*/
int  backtrace_getpcmask(void);
void backtrace_getcurrentframe(void *);



void	BackTrace_SupportCurrentFrame( _kernel_unwindblock *frame);
/*
Read the current values of fp, sl and pc (so that _kernel_unwind() can
be called).
 */

void	BackTrace_OutputToStdErr( void);
/*
Generates a stack backtrace on stderr.
 */


void	BackTrace_OutputToStreamWithPrefix( FILE* stream, const char* prefix);
/*
Sends a backtrace to 'stream', prefixing each line with 'prefix'.
 */


typedef int (*backtrace_printf_fn)( void* reference, const char* format, ...);

void	BackTrace_OutputToFFunctionWithPrefix( backtrace_printf_fn fn, void* reference, const char* prefix);
/*
Sends a backtrace to 'fn', prefixing each line with 'prefix'.
 */


extern unsigned int	BackTrace_GetPC( void);
/*
Returns PC for caller - returns ONLY PC in 26 bit or 32 bit modes.
 */

extern unsigned int	BackTrace_GetSL( void);
/*
Returns value of stack-limit register.
 */


#define	BackTrace_GetPC2() ((void*) ( Desk_BackTrace_GetPC() & (0x3fffffc)))
/*
Strips off status flags and processor mode from the current PC.
 */


int	BackTrace_GetNestingDepth( void);
/*
Returns the current function-nesting depth. Only functions which set up
a stack-frame are detected, and note that Shared C Lib stack-extension
functions create a stack-frame.
 */


typedef struct	{
	int		n;
	unsigned int**	functions;
	}
	BackTrace_functionlist;

#define	BackTrace_MAXFNS 256


const BackTrace_functionlist*	BackTrace_GetCurrentFunctions( void);
/*
Returns a pointer to an internal object containing array of current
functions. Each entry in the array is the address of the save
instruction used to create each stack-frame. This instruction is usually
a 'STM ...'.
'functions[0]' is address of most recent save instruction. 'n' is number
of functions found.

A maximum of BackTrace_MAXFNS are found.
 */



const char*	BackTrace_GetFunctionName( const void* ptr);
/*
Returns function name if ptr is <= 4 words after start of function and
the name is embedded in code.
Otherwise returns NULL.
 */


#ifdef __cplusplus
}
#endif

#endif
