Skip to content

Instantly share code, notes, and snippets.

@epetousis
Created December 29, 2011 12:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epetousis/1533869 to your computer and use it in GitHub Desktop.
Save epetousis/1533869 to your computer and use it in GitHub Desktop.
Tools for loading a kernel panic image into the kernel of OSX
#Instead of building everything manually, you could use this script. Just open Terminal and write chmod 755, press the space bar and then drag this file in.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$DIR"
echo "Compiling load_panic_image.c"
gcc load_panic_image.c -o load_panic_image
echo "Finished compiling load_panic_image.c"
echo "Compiling panic_test.c"
gcc panic_test.c -o panic_test
echo "Finished compiling panic_test.c"
echo "Done! Newly compiled commands can be found at $DIR"
// load_panic_image.c
// Use this to load an image into the kernel to be displayed when OSX kernel panics.
// To build this, type in terminal:
// gcc load_panic_image.c -o load_panic_image
#define PROGNAME "load_panic_image"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
int
main(int argc, char **argv)
{
int ret, fd;
char *buf;
size_t oldlen = 0, newlen;
struct stat sb;
int mib[3] = { CTL_KERN, KERN_PANICINFO, KERN_PANICINFO_IMAGE };
if (argc != 2) {
fprintf(stderr, "usage: %s <kraw image file path>\n", PROGNAME);
exit(1);
}
if (stat(argv[1], &sb) < 0) {
perror("stat");
exit(1);
}
newlen = sb.st_size;
buf = (char *)malloc(newlen); // assume success
fd = open(argv[1], O_RDONLY); // assume success
ret = read(fd, buf, sb.st_size); // assume success
close(fd);
if (sysctl(mib, 3, NULL, (void *)&oldlen, buf, newlen)) {
perror("sysctl");
}
exit(ret);
}
// Use this to display the kernel panic image.
// This command can only run with sudo.
// To build this, type in terminal:
// gcc panic_test.c -o panic_test
#include <sys/types.h>
#include <sys/sysctl.h>
#define KERN_PANICINFO_TEST (KERN_PANICINFO_IMAGE + 2)
int main(void)
{
size_t oldnewlen = 0;
int mib[3] = { CTL_KERN, KERN_PANICINFO, KERN_PANICINFO_TEST };
return sysctl(mib, 3, NULL, (void*)&oldnewlen, NULL, oldnewlen);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment