Commit 9552c972 authored by Rusty Russell's avatar Rusty Russell

breakpoint: new module.

Thanks to Jeremy Kerr for the idea!
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 75786a73
......@@ -34,6 +34,7 @@ MODS_WITH_SRC := antithread \
avl \
bdelta \
block_pool \
breakpoint \
btree \
ccan_tokenizer \
charset \
......
../../licenses/CC0
\ No newline at end of file
#include <string.h>
#include "config.h"
/**
* breakpoint - break if the program is run under gdb.
*
* This code allows you to insert breakpoints within a program. These will
* do nothing unless your program is run under GDB.
*
* License: CC0 (Public domain)
*
* Example:
* #include <ccan/breakpoint/breakpoint.h>
*
* int main(void)
* {
* breakpoint();
* return 0;
* }
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/compiler\n");
return 0;
}
return 1;
}
/* CC0 (Public domain) - see LICENSE file for details
*
* Idea for implementation thanks to stackoverflow.com:
* http://stackoverflow.com/questions/3596781/detect-if-gdb-is-running
*/
#include <ccan/breakpoint/breakpoint.h>
bool breakpoint_initialized;
bool breakpoint_under_debug;
/* This doesn't get called if we're under GDB. */
static void trap(int signum)
{
breakpoint_initialized = true;
}
void breakpoint_init(void)
{
struct sigaction old, new;
new.sa_handler = trap;
new.sa_flags = 0;
sigemptyset(&new.sa_mask);
sigaction(SIGTRAP, &new, &old);
kill(getpid(), SIGTRAP);
sigaction(SIGTRAP, &old, NULL);
if (!breakpoint_initialized) {
breakpoint_initialized = true;
breakpoint_under_debug = true;
}
}
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_BREAKPOINT_H
#define CCAN_BREAKPOINT_H
#include <ccan/compiler/compiler.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
void breakpoint_init(void) COLD;
extern bool breakpoint_initialized;
extern bool breakpoint_under_debug;
/**
* breakpoint - stop if running under the debugger.
*/
static inline void breakpoint(void)
{
if (!breakpoint_initialized)
breakpoint_init();
if (breakpoint_under_debug)
kill(getpid(), SIGTRAP);
}
#endif /* CCAN_BREAKPOINT_H */
#include <ccan/breakpoint/breakpoint.h>
#include <ccan/breakpoint/breakpoint.c>
#include <ccan/tap/tap.h>
int main(void)
{
/* This is how many tests you plan to run */
plan_tests(2);
breakpoint();
ok1(breakpoint_initialized);
ok1(!breakpoint_under_debug);
/* This exits depending on whether all tests passed */
return exit_status();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment