Last active
January 16, 2022 07:32
-
-
Save amodm/ea4fcbddd6516c5aa599d47b1686e760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Sample code to test what happens when a debugger sets a breakpoint. | |
* | |
* Written for #WeekendDevPuzzle dated 2022-01-15 | |
* https://twitter.com/amodm/status/1482206401103818756 | |
* | |
* Steps to execute this: | |
* # compile program | |
* gcc -o testdebugcrash testdebugcrash.c | |
* # run program | |
* ./testdebugcrash | |
* # (another terminal) attach debugger and set breakpoint (if gdb) | |
* gdb -p $(ps aux | grep testdebugcrash | grep -v grep | awk '{ print $2 }') | |
* # inside debugger | |
* break crashtest | |
* continue | |
* # (another terminal) kill debugger without giving it a chance to clean up | |
* kill -9 $(ps aux | grep gdb | grep -v grep | awk '{ print $2 }') | |
* # observe if testdebugcrash ends cleanly, or crashes | |
*/ | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <unistd.h> | |
// this is the address where we set breakpoint | |
void crashtest() { | |
printf("weird! we seemed to have survived the crash test\n"); | |
} | |
// simple helper to print the 1st 4 bytes at breakpoint site | |
// in serial order | |
void print_bytecode() { | |
uint8_t *bytecodes = (uint8_t*)*crashtest; | |
printf("function bytecode 0x%02x, 0x%02x, 0x%02x, 0x%02x\n", | |
bytecodes[0] & 0xff, | |
bytecodes[1] & 0xff, | |
bytecodes[2] & 0xff, | |
bytecodes[3] & 0xff | |
); | |
} | |
int main() { | |
printf("starting. please attach & set a breakpoint\n"); | |
print_bytecode(); | |
// let's sleep for enough time to allow debugger to attach | |
// and set a breakpoint | |
sleep(30); | |
// let's see if bytecode at the breakpoint site, changed | |
printf("checking for bytecode again\n"); | |
print_bytecode(); | |
printf("invoking breakpoint-ed function\n"); | |
crashtest(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment