Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make profile handle longjmps correctly #2444

Merged
merged 4 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ Obj STEVES_TRACING;
#endif

#define CHECK_RECURSION_BEFORE \
CheckRecursionBefore(); \
HookedLineIntoFunction(func);
HookedLineIntoFunction(func); \
CheckRecursionBefore();

#define CHECK_RECURSION_AFTER \
DecRecursionDepth(); \
Expand Down
84 changes: 81 additions & 3 deletions src/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "bool.h"
#include "calls.h"
#include "code.h"
#include "funcs.h"
#include "error.h"
#include "hookintrprtr.h"
#include "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 @@ -353,6 +422,7 @@ static inline void outputStat(Stat stat, int exec, int visited)
profileState.minimumProfileTick;
}
ticks -= ticksDone;
outputFilenameIdIfRequired(nameid);
fprintf(
profileState.Stream,
"{\"Type\":\"%c\",\"Ticks\":%d,\"Line\":%d,\"FileId\":%d}\n",
Expand All @@ -369,6 +439,7 @@ static inline void outputStat(Stat stat, int exec, int visited)
}
}
else {
outputFilenameIdIfRequired(nameid);
fprintf(profileState.Stream, "{\"Type\":\"%c\",\"Line\":%d,\"FileId\":%d}\n",
exec ? 'E' : 'R', (int)line, (int)nameid);
profileState.lastOutputted.line = line;
Expand Down Expand Up @@ -457,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 @@ -518,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 @@ -586,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 @@ -784,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 @@ -798,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
5 changes: 5 additions & 0 deletions src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,8 @@ static void SySetInitialGapRootPaths(void)
*F RegisterSyLongjmpObserver( <func> )
** Register a function to be called before longjmp is called.
** returns 1 on success, 0 if the table of functions is already full.
** This function is idempotent -- if a function is passed multiple times
** it is still only registered once.
*/

enum { signalSyLongjmpFuncsLen = 16 };
Expand All @@ -859,6 +861,9 @@ Int RegisterSyLongjmpObserver(voidfunc func)
{
Int i;
for (i = 0; i < signalSyLongjmpFuncsLen; ++i) {
if (signalSyLongjmpFuncs[i] == func) {
return 1;
}
if (signalSyLongjmpFuncs[i] == 0) {
signalSyLongjmpFuncs[i] = func;
return 1;
Expand Down