Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@joelagnel
joelagnel / ignore-stdin-for-secs.sh
Created September 14, 2023 00:37
Script to ignore stdin for a $1 seconds
#!/bin/sh
# Check if at least one numeric arg is passed
if [ $# -eq 0 ]
then
echo "Usage: $0 <seconds>"
exit 1
fi
# Read and discard input for 1 second
#!/bin/sh
# Save this file as "init" in the root of your CPIO archive,
# and chmod +x it. This will chroot into an HDD if one is
# available. Or it will just give a prompt.
mkdir -p /proc /sys /d /pstore /dev
mount -t proc none /proc
mount -t sysfs none /sys
mount -t debugfs none /d
mount -t debugfs none /sys/kernel/debug/
@joelagnel
joelagnel / run-qemu.sh
Last active November 30, 2023 04:43
run-qemu.sh script to run qemu for kernel development
#!/bin/bash
# Author: Joel Fernandes <joel@joelfernandes.org>
#
# Usage, run-qemu : Runs with busybox chrooting into a debian qemu.img
# run-qemu --rt : Set all Qemu vCPU threads as RT and pin them to different CPUs.
# run-qemu --rcu : Run qemu with rcutorture boot parameters
#
# run-qemu --cpio : Runs only with busy-box initramfs (no chrooting)
# run-qemu --debian : Boot into a debian rootfs (see end of this file for how to build debian images)
# If neither --cpio nor --debian is passed, then it will try to chroot into a qemu.img file.
@joelagnel
joelagnel / hrtimer_trace_parse.py
Last active April 11, 2023 18:36
Parse Traces to find top hrtimer users who actually have timers fire
#!/usr/bin/python3
# Run for tracing:
# With full screen video playing on device, run:
# trace-cmd record -e timer:hrtimer_start -R stacktrace -e hrtimer_cancel -e timer:hrtimer_expire_entry -- sleep 5
# trace-cmd report > trace.txt
# Pass the trace to this script.
# The script will produce a filter_trace.txt which only shows hrtimer events that actually fired and
# did not get canceled.
# The script will also output the top-most frequent stacks that queued a timer.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/slab.h>
static char *crash_type = "null";
module_param(crash_type, charp, S_IRUGO | S_IWUSR);
MODULE_LICENSE("GPL");
#!/bin/bash
function cleanup {
echo "Exiting script, killing child processes..."
kill $pid1 $pid2
# Remove the cgroups
cgdelete -g cpu:group1
cgdelete -g cpu:group2
exit 1
}
#!/bin/bash
function cleanup {
echo "Exiting script, killing child processes..."
kill $pid1 $pid2
exit 1
}
trap cleanup EXIT

Kernel Debug Tricks of the trade

NOTE: Updated on 3/11/2023.

Introduction

Debugging the kernel is a difficult task. Thankfully there are quite a few built-in kernel debugging tools and techniques that help one quickly identify the source and/or the cause of an issue.

Enable debug information

@joelagnel
joelagnel / rt-spinners.c
Created January 26, 2023 18:03
A test to check RT throttling
// Author: Steven Rostedt
//
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>