mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
readme: more info on kernel panics, oops an backtraces.
myinsmod: use either finit or init
This commit is contained in:
@@ -4,7 +4,11 @@
|
||||
. Debugging
|
||||
.. link:hello.c[]
|
||||
.. link:hello2.c[]
|
||||
. Panic and friends
|
||||
.. link:panic.c[]
|
||||
.. link:oops.c[]
|
||||
.. link:warn_on.c[]
|
||||
.. link:warn_on.c[]
|
||||
. Module utils
|
||||
.. link:params.c[]
|
||||
.. link:vermagic.c[]
|
||||
@@ -40,6 +44,8 @@
|
||||
.. link:kstrto.c[]
|
||||
. Misc
|
||||
.. link:ring0.c[]
|
||||
. ARM
|
||||
.. link:pmccntr.c[]
|
||||
. Hardware device drivers
|
||||
.. link:pci_min.c[]
|
||||
.. link:pci.c[]
|
||||
|
||||
19
kernel_module/dump_stack.c
Normal file
19
kernel_module/dump_stack.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
|
||||
static int myinit(void)
|
||||
{
|
||||
pr_info("dump_stack myinit\n");
|
||||
dump_stack();
|
||||
pr_info("dump_stack after\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void myexit(void)
|
||||
{
|
||||
pr_info("panic myexit\n");
|
||||
}
|
||||
|
||||
module_init(myinit)
|
||||
module_exit(myexit)
|
||||
MODULE_LICENSE("GPL");
|
||||
19
kernel_module/oops.c
Normal file
19
kernel_module/oops.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
|
||||
static int myinit(void)
|
||||
{
|
||||
pr_info("oops myinit\n");
|
||||
*(int *)0 = 0;
|
||||
pr_info("oops after\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void myexit(void)
|
||||
{
|
||||
pr_info("oops myexit\n");
|
||||
}
|
||||
|
||||
module_init(myinit)
|
||||
module_exit(myexit)
|
||||
MODULE_LICENSE("GPL");
|
||||
@@ -1,28 +1,17 @@
|
||||
/*
|
||||
It will happen eventually, so you might as well learn do deal with it.
|
||||
|
||||
TODO: how to scroll up to see full trace? Shift + Page Up does not work as it normally does:
|
||||
https://superuser.com/questions/848412/scrolling-up-the-failed-screen-with-kernel-panic
|
||||
|
||||
The alternative is to get the serial data out streamed to console or to a file:
|
||||
|
||||
- https://superuser.com/questions/269228/write-qemu-booting-virtual-machine-output-to-a-file
|
||||
- http://www.reactos.org/wiki/QEMU#Redirect_to_a_file
|
||||
*/
|
||||
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
|
||||
static int myinit(void)
|
||||
{
|
||||
pr_info("panic init\n");
|
||||
pr_info("panic myinit\n");
|
||||
panic("hello panic");
|
||||
pr_info("panic after\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void myexit(void)
|
||||
{
|
||||
pr_info("panic cleanup\n");
|
||||
pr_info("panic myexit\n");
|
||||
}
|
||||
|
||||
module_init(myinit)
|
||||
|
||||
@@ -24,33 +24,35 @@ typedef struct {
|
||||
|
||||
void ring0_get_control_regs(Ring0Regs *ring0_regs)
|
||||
{
|
||||
#ifdef __x86_64__
|
||||
#if defined(__x86_64__)
|
||||
__asm__ __volatile__ (
|
||||
"mov %%cr0, %%rax\n\t"
|
||||
"mov %%eax, %0\n\t"
|
||||
"mov %%cr2, %%rax\n\t"
|
||||
"mov %%eax, %1\n\t"
|
||||
"mov %%cr3, %%rax\n\t"
|
||||
"mov %%eax, %2\n\t"
|
||||
: "=m" (ring0_regs->cr0),
|
||||
"=m" (ring0_regs->cr2),
|
||||
"=m" (ring0_regs->cr3)
|
||||
: /* no input */
|
||||
: "%rax"
|
||||
"mov %%cr0, %%rax;"
|
||||
"mov %%eax, %0;"
|
||||
"mov %%cr2, %%rax;"
|
||||
"mov %%eax, %1;"
|
||||
"mov %%cr3, %%rax;"
|
||||
"mov %%eax, %2;"
|
||||
: "=m" (ring0_regs->cr0),
|
||||
"=m" (ring0_regs->cr2),
|
||||
"=m" (ring0_regs->cr3)
|
||||
:
|
||||
: "%rax"
|
||||
);
|
||||
#elif defined(__i386__)
|
||||
__asm__ __volatile__ (
|
||||
"mov %%cr0, %%eax\n\t"
|
||||
"mov %%eax, %0\n\t"
|
||||
"mov %%cr2, %%eax\n\t"
|
||||
"mov %%eax, %1\n\t"
|
||||
"mov %%cr3, %%eax\n\t"
|
||||
"mov %%eax, %2\n\t"
|
||||
: "=m" (ring0_regs->cr0),
|
||||
"=m" (ring0_regs->cr2),
|
||||
"=m" (ring0_regs->cr3)
|
||||
: /* no input */
|
||||
: "%eax"
|
||||
"mov %%cr0, %%eax;"
|
||||
"mov %%eax, %0;"
|
||||
"mov %%cr2, %%eax;"
|
||||
"mov %%eax, %1;"
|
||||
"mov %%cr3, %%eax;"
|
||||
"mov %%eax, %2;"
|
||||
: "=m" (ring0_regs->cr0),
|
||||
"=m" (ring0_regs->cr2),
|
||||
"=m" (ring0_regs->cr3)
|
||||
:
|
||||
: "%eax"
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,17 +7,19 @@
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
|
||||
#define init_module(module_image, len, param_values) syscall(__NR_init_module, module_image, len, param_values)
|
||||
#define finit_module(fd, param_values, flags) syscall(__NR_finit_module, fd, param_values, flags)
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const char *params;
|
||||
int fd;
|
||||
int fd, use_finit;
|
||||
size_t image_size;
|
||||
struct stat st;
|
||||
void *image;
|
||||
|
||||
/* CLI handling. */
|
||||
if (argc < 2) {
|
||||
puts("Usage ./prog mymodule.ko [args]");
|
||||
puts("Usage ./prog mymodule.ko [args="" [use_finit=0]");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (argc < 3) {
|
||||
@@ -25,16 +27,33 @@ int main(int argc, char **argv) {
|
||||
} else {
|
||||
params = argv[2];
|
||||
}
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
fstat(fd, &st);
|
||||
image_size = st.st_size;
|
||||
image = malloc(image_size);
|
||||
read(fd, image, image_size);
|
||||
close(fd);
|
||||
if (init_module(image, image_size, params) != 0) {
|
||||
perror("init_module");
|
||||
return EXIT_FAILURE;
|
||||
if (argc < 4) {
|
||||
use_finit = 0;
|
||||
} else {
|
||||
use_finit = (argv[3][0] != '0');
|
||||
}
|
||||
|
||||
/* Action. */
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
if (use_finit) {
|
||||
puts("finit");
|
||||
if (finit_module(fd, params, 0) != 0) {
|
||||
perror("finit_module");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
close(fd);
|
||||
} else {
|
||||
puts("init");
|
||||
fstat(fd, &st);
|
||||
image_size = st.st_size;
|
||||
image = malloc(image_size);
|
||||
read(fd, image, image_size);
|
||||
close(fd);
|
||||
if (init_module(image, image_size, params) != 0) {
|
||||
perror("init_module");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
free(image);
|
||||
}
|
||||
free(image);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
23
kernel_module/warn_on.c
Normal file
23
kernel_module/warn_on.c
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
Prints a backtrace.
|
||||
*/
|
||||
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
|
||||
static int myinit(void)
|
||||
{
|
||||
pr_info("warn_on init\n");
|
||||
WARN_ON("warn_on do it");
|
||||
pr_info("warn_on after\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void myexit(void)
|
||||
{
|
||||
pr_info("warn_on cleanup\n");
|
||||
}
|
||||
|
||||
module_init(myinit)
|
||||
module_exit(myexit)
|
||||
MODULE_LICENSE("GPL");
|
||||
Reference in New Issue
Block a user