/* Function profiling code V2.13 17/2/08
   Copyright 2008 Jeffrey Lee
   This file is part of WOUM.
   WOUM is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   WOUM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with WOUM.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _JPROF2_H
#define _JPROF2_H

typedef struct {
	char *name; /* Function name */
	int *adr; /* Function address. If *adr != instr, profiling is enabled for the function. */
	int func_entered; /* Time function was entered, or 0 if not currently running */
	int time_total; /* Total time spent in function */
	int count; /* Number of times function has been called */
	int func_r14; /* R14 of the function */
	int *code; /* Profiling code for the function, or 0 if profiling fully disabled */
	int instr; /* Instruction that was replaced */
} jprof2_struct; /* Profiling data struct */

extern jprof2_struct *jprof2_funcs; /* Array of profiling info */
extern int jprof2_numfuncs; /* Number of functions in the array */
extern int jprof2_ticspersec; /* Number of clock ticks per second (for interpreting the time_total values by) */

extern void jprof2_init(); /* Initialise system */
extern int jprof2_forceadd(void *func,char *name); /* Forcibly add a function to the list to profile. Designed to allow profiling of libscl functions, may crash computer if you try it on other functions without knowing how the code works. Does nothing if a function with the same name/address already exists. jprof2_init() must have been called first, but no profiling must have been started yet. Does not make a copy of the name ptr. Returns !0 on failure. */
extern int jprof2_profile(char *name); /* Try to profile the given function, returns !0 on failure */
extern int jprof2_stop(char *name); /* Try to stop profiling the given function. Returns 0 for full success and 1 for partial success (profiling stopped, but the function is currently running so profiling code can't be deleted) */
extern void jprof2_reset(char *name); /* Reset profiling info for given func, or all funcs if name=0 */
extern int jprof2_shutdown(); /* Shut down system. Returns !0 for failure. */

/* jprof2asm.s */
extern int jprof2_nextfunc(int adr,int prof); /* Return address of next function after 'adr' which, or 0 for none. If 'prof' is 1, only profilable functions will be returned. */
extern char *jprof2_funcname(int adr); /* Return name of function adr, or 0 for none. adr must point to the start of the function! */
extern int *jprof2_codeblock; /* Block of code to create profiling code from */
extern int jprof2_codeblock_entry; /* Offset of entry point */
extern int jprof2_codeblock_instr; /* Offset for the 1st instruction to be inserted */
extern int jprof2_codeblock_branch; /* Offsets for the branch instructions to be inserted */
extern int jprof2_codeblock_struct,jprof2_codeblock_timeptr; /* Offsets for the structure ptr & time ptrs to be inserted */
extern int jprof2_codeblock_length; /* Length of the code block */ 

#endif
