Skip to content

Commit

Permalink
Recover from longjmp Error handling in profiling
Browse files Browse the repository at this point in the history
Previously, profiling did not detect when Error caused one or more
functions to exit. This lead to incorrect and very deep stack
traces.
  • Loading branch information
ChrisJefferson committed May 13, 2018
1 parent 390c096 commit ec886d7
Showing 1 changed file with 79 additions and 3 deletions.
82 changes: 79 additions & 3 deletions src/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <src/bool.h>
#include <src/calls.h>
#include <src/code.h>
#include <src/funcs.h>
#include <src/error.h>
#include <src/hookintrprtr.h>
#include <src/io.h>
Expand Down Expand Up @@ -95,7 +96,6 @@
** never marked as executed.
*/


/****************************************************************************
**
** Store the current state of the profiler
Expand Down Expand Up @@ -146,11 +146,56 @@ struct ProfileState
** clear */
UInt profiledPreviously;

Int LongJmpOccurred;

// We store the value of RecursionDepth each time we enter a function.
// This is the only way to detect if GAP has left a function by performing
// a longjmp.
// We need to store the actual values, as RecursionDepth can increase
// by more than one when a GAP function is called
Obj visitedDepths;
} profileState;

/* We keep this seperate as it is exported for use in other files */
UInt profileState_Active;


static void ProfileRegisterLongJmpOccurred(void)
{
profileState.LongJmpOccurred = 1;
}

// This function is called when we detect a longjmp occurred, and
// outputs a 'return' into the profile for any function which was
// jumped over.
// It is fine for this function to be called when a longjmp has not
// occurred, or when no function was longjmped over.
static void CheckLeaveFunctionsAfterLongjmp(void)
{
if (!profileState.LongJmpOccurred)
return;

#ifdef HPCGAP
if (profileState.profiledThread != TLS(threadID))
return;
#endif

profileState.LongJmpOccurred = 0;

Int pos = LEN_PLIST(profileState.visitedDepths);
Int depth = GetRecursionDepth();

while (pos > 0 && INT_INTOBJ(ELM_PLIST(profileState.visitedDepths, pos)) > depth) {
// Give dummy values if we do not know
fprintf(profileState.Stream,
"{\"Type\":\"O\",\"Fun\":\"nameless\",\"Line\":-1,"
"\"EndLine\":-1,\"File\":\"<missing filename>\","
"\"FileId\":-1}\n");
PopPlist(profileState.visitedDepths);
pos--;
}
}

static inline void outputFilenameIdIfRequired(UInt id)
{
if (id == 0) {
Expand Down Expand Up @@ -213,10 +258,32 @@ void HookedLineOutput(Obj func, char type)
}

void enterFunction(Obj func)
{ HookedLineOutput(func, 'I'); }
{
#ifdef HPCGAP
if (profileState.profiledThread != TLS(threadID))
return;
#endif
CheckLeaveFunctionsAfterLongjmp();
PushPlist(profileState.visitedDepths, INTOBJ_INT(GetRecursionDepth()));
HookedLineOutput(func, 'I');
}

void leaveFunction(Obj func)
{ HookedLineOutput(func, 'O'); }
{
#ifdef HPCGAP
if (profileState.profiledThread != TLS(threadID))
return;
#endif
// Do not crash if we exit the function in which
// Profile was originally called. The profiling
// package can handle such profiles.
if (LEN_PLIST(profileState.visitedDepths) > 0) {
PopPlist(profileState.visitedDepths);
}
CheckLeaveFunctionsAfterLongjmp();

HookedLineOutput(func, 'O');
}

/****************************************************************************
**
Expand Down Expand Up @@ -306,6 +373,8 @@ static inline void outputStat(Stat stat, int exec, int visited)
UInt line;
int nameid;

CheckLeaveFunctionsAfterLongjmp();

Int8 ticks = 0, newticks = 0;

// Explicitly skip these two cases, as they are often specially handled
Expand Down Expand Up @@ -459,6 +528,7 @@ void enableAtStartup(char * filename, Int repeats, TickMethod tickMethod)
ActivateHooks(&profileHooks);

profileState_Active = 1;
RegisterSyLongjmpObserver(ProfileRegisterLongJmpOccurred);
profileState.profiledPreviously = 1;
#ifdef HPCGAP
profileState.profiledThread = TLS(threadID);
Expand Down Expand Up @@ -520,7 +590,10 @@ Obj FuncACTIVATE_PROFILING(Obj self,
return Fail;
}

memset(&profileState, 0, sizeof(profileState));

OutputtedFilenameList = NEW_PLIST(T_PLIST, 0);
profileState.visitedDepths = NEW_PLIST(T_PLIST, 0);

if ( ! IsStringConv( filename ) ) {
ErrorMayQuit("<filename> must be a string",0,0);
Expand Down Expand Up @@ -588,6 +661,7 @@ Obj FuncACTIVATE_PROFILING(Obj self,
}

profileState_Active = 1;
RegisterSyLongjmpObserver(ProfileRegisterLongJmpOccurred);
profileState.profiledPreviously = 1;
#ifdef HPCGAP
profileState.profiledThread = TLS(threadID);
Expand Down Expand Up @@ -786,6 +860,7 @@ static Int InitLibrary (
/* init filters and functions */
InitGVarFuncsFromTable( GVarFuncs );

profileState.visitedDepths = NEW_PLIST(T_PLIST, 0);
OutputtedFilenameList = NEW_PLIST(T_PLIST, 0);
/* return success */
return 0;
Expand All @@ -800,6 +875,7 @@ static Int InitKernel (
{
InitHdlrFuncsFromTable( GVarFuncs );
InitGlobalBag(&OutputtedFilenameList, "src/profile.c:OutputtedFileList");
InitGlobalBag(&profileState.visitedDepths, "src/profile.c:visitedDepths");
return 0;
}

Expand Down

0 comments on commit ec886d7

Please sign in to comment.