Skip to content

Instantly share code, notes, and snippets.

View larytet's full-sized avatar

Arkady Miasnikov larytet

View GitHub Profile
@larytet
larytet / gist:21fb14d5a71f2870d56d
Last active August 29, 2015 14:23
GreaseMonkey script for Yad2: Update the ads. If you have posted lot of ads this script is for you. Also opens all posted ads for review and editing.
// ==UserScript==
// @name Yad2 Ad update
// @namespace yad2
// @description Simulates click on "kaftor3"
// @include http://my.yad2.co.il/MyYad2/MyOrder/Yad2.php
// ==/UserScript==
// Use chromium-browser --enable-easy-off-store-extension-install to install offline
// in Chrome
/*
@larytet
larytet / define_symbol_usage.stp
Created June 16, 2017 20:03
Define symbol in STAP
@define cc1 %( "/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/cc1" %)
global insns
probe perf.hw.instructions.process(@cc1).counter("foo") {}
probe process(@cc1).function("*").return {
insns[execname(),tid()] = @perf("foo") // implicit max()
}
stap -D MAXSKIPPED=0 -D MAXTRYLOCK=1000000 -D TRYLOCKDELAY=1 -g -e 'global ar%; function w_ar() {ar[tid()]=tid();} %{long long counter;u8 shm[256];static void* w_shm(void);static void* w_shm() {memset(shm, 0, sizeof(shm));return shm;} %} probe syscall.close{w_ar();%{ {counter++;w_shm();} %}} probe syscall.close.return {w_ar();%{ {counter++;w_shm();} %}} probe syscall.open{w_ar();%{ {counter++;w_shm();} %}} probe syscall.open.return{w_ar();%{ {counter++;w_shm();} %}} probe syscall.dup2.return{w_ar();%{ {counter++;w_shm();} %}} probe syscall.dup2.return{w_ar();%{ {counter++;w_shm();} %}} probe syscall.read.return{w_ar();%{ {counter++;w_shm();} %}} probe syscall.read{w_ar();%{ {counter++;w_shm();} %}} probe end { %{ {printk("\n%lli\n", counter);} %}}'
stap -D MAXSKIPPED=0 -D MAXTRYLOCK=1000000 -D TRYLOCKDELAY=1 -g -e '%{long long counter;u8 shm[256];static void* w_shm(void);static void* w_shm() {memset(shm, 0, sizeof(shm));return shm;} %} probe syscall.close{%{ {counter++;w_shm();} %}} probe syscall.close
@larytet
larytet / exec.stp
Created July 7, 2017 03:17
SystemTap exec probes
probe kprocess.exec
{
%{HIT_MAP_INC(HIT_MAP_KPROCESS_EXEC)%}
tid = tid()
if (stringat(filename,0) == 0x22) // filename starts with a quotation mark
{
MAP_SYSCALL_EXEC_NAME[tid] = filename
MAP_SYSCALL_EXEC_ARGV[tid] = args
}
else // failed to recog the filename, trigger do_execve
@larytet
larytet / SystemTap_tests.sh
Last active July 7, 2017 16:43
Tight echo loop
#!/bin/bash
function echo_loop()
{
file=echo_file_`date +%s%N`
echo $file
echo > $file
counter=1
end=$((SECONDS+10))
while [ $SECONDS -lt $end ]; do
@larytet
larytet / kernel_debug.sh
Created July 10, 2017 12:59
Kernel debug memo
wget https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux
chmod +x extract-vmlinux
sudo ./extract-vmlinux /boot/vmlinuz-`uname -r` > vmlinux
crash vmlinux
@larytet
larytet / syscalls_counter.stp
Last active July 10, 2017 17:04
Collect system number of syscalls using STAP
// Use MAXMAPENTRIES to set the maximum size of the array
// for example sudo stap -D MAXMAPENTRIES=40000 ./src/driver/load_measurement.stp
global probe_frequency%
global processes%
probe begin
{
printf("Ctrl-C to print the results\n");
}
@larytet
larytet / shared_memory.h
Last active July 12, 2017 13:01
Shared memory API targeting SystemTap
/*
Shared memory (SHM_FIFO) is a byte ring buffer in the virtual memory. Driver pushes the data to
the tail of the FIFO (producer) and user space application reads the data from the head of the
buffer. Driver always places data structures in consecutive memory. If there is not enough
room to place the whole structure into the ring buffer without wrap around driver places special
"skip" code until the last byte of the allocated virtual memory and writes the structure
starting with offset zero. Application knows to handle the wrap around correctly by checking
if there is enough space for a strcuture of minimum size and skipping the data to the end of the
virtual memory.
@larytet
larytet / alloc_free.c
Created July 14, 2017 17:35
Memory allocation in the kernel
static void *rvmalloc(unsigned long size)
{
void *mem;
unsigned long adr;
size = PAGE_ALIGN(size);
mem = vzalloc(size); //syscalls_trace_buffer;//vmalloc(size);
# if (SHM_RESERVE_PAGES > 0)
if (mem) {
// memset(mem, 0, size); vzalloc() will zero the memory