#include <signal.h>
#include <stdlib.h>

#include "tests.h"
#include "debug.h"
#include "util.h"
#include "perf-hooks.h"

static void sigsegv_handler(int sig __maybe_unused)
{
	pr_debug("SIGSEGV is observed as expected, try to recover.\n");
	perf_hooks__recover();
	signal(SIGSEGV, SIG_DFL);
	raise(SIGSEGV);
	exit(-1);
}


static void the_hook(void *_hook_flags)
{
	int *hook_flags = _hook_flags;
	int *p = NULL;

	*hook_flags = 1234;

	/* Generate a segfault, test perf_hooks__recover */
	*p = 0;
}

int test__perf_hooks(int subtest __maybe_unused)
{
	int hook_flags = 0;

	signal(SIGSEGV, sigsegv_handler);
	perf_hooks__set_hook("test", the_hook, &hook_flags);
	perf_hooks__invoke_test();

	/* hook is triggered? */
	if (hook_flags != 1234) {
		pr_debug("Setting failed: %d (%p)\n", hook_flags, &hook_flags);
		return TEST_FAIL;
	}

	/* the buggy hook is removed? */
	if (perf_hooks__get_hook("test"))
		return TEST_FAIL;
	return TEST_OK;
}
dd309931f'/><select name='h' onchange='this.form.submit();'>
<option value='emaclite-cleanup'>emaclite-cleanup</option>
<option value='master'>master</option>
<option value='nds-private-remove' selected='selected'>nds-private-remove</option>
<option value='packet-loop-back'>packet-loop-back</option>
<option value='packet-rx-pump-back'>packet-rx-pump-back</option>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>net-next plumbings</td><td class='sub right'>Tobias Klauser</td></tr></table>
<table class='tabs'><tr><td>
<a href='/cgit.cgi/linux/net-next.git/?h=nds-private-remove'>summary</a><a href='/cgit.cgi/linux/net-next.git/refs/?h=nds-private-remove&amp;id=b6878eaf75176e9acb6463ea1e50edddd309931f'>refs</a><a class='active' href='/cgit.cgi/linux/net-next.git/log/net/dccp/input.c?h=nds-private-remove'>log</a><a href='/cgit.cgi/linux/net-next.git/tree/net/dccp/input.c?h=nds-private-remove&amp;id=b6878eaf75176e9acb6463ea1e50edddd309931f'>tree</a><a href='/cgit.cgi/linux/net-next.git/commit/net/dccp/input.c?h=nds-private-remove&amp;id=b6878eaf75176e9acb6463ea1e50edddd309931f'>commit</a><a href='/cgit.cgi/linux/net-next.git/diff/net/dccp/input.c?h=nds-private-remove&amp;id=b6878eaf75176e9acb6463ea1e50edddd309931f'>diff</a></td><td class='form'><form class='right' method='get' action='/cgit.cgi/linux/net-next.git/log/net/dccp/input.c'>
<input type='hidden' name='h' value='nds-private-remove'/><input type='hidden' name='id' value='b6878eaf75176e9acb6463ea1e50edddd309931f'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/cgit.cgi/linux/net-next.git/log/?h=nds-private-remove&amp;id=b6878eaf75176e9acb6463ea1e50edddd309931f'>root</a>/<a href='/cgit.cgi/linux/net-next.git/log/net?h=nds-private-remove&amp;id=b6878eaf75176e9acb6463ea1e50edddd309931f'>net</a>/<a href='/cgit.cgi/linux/net-next.git/log/net/dccp?h=nds-private-remove&amp;id=b6878eaf75176e9acb6463ea1e50edddd309931f'>dccp</a>/<a href='/cgit.cgi/linux/net-next.git/log/net/dccp/input.c?h=nds-private-remove&amp;id=b6878eaf75176e9acb6463ea1e50edddd309931f'>input.c</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th class='left'>Age</th><th class='left'>Commit message (<a href='/cgit.cgi/linux/net-next.git/log/net/dccp/input.c?h=nds-private-remove&amp;id=b6878eaf75176e9acb6463ea1e50edddd309931f&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Files</th><th class='left'>Lines</th></tr>