Skip to content

Instantly share code, notes, and snippets.

@a9QrX3Lu
Created February 9, 2021 11:23
Show Gist options
  • Select an option

  • Save a9QrX3Lu/7807496cf55052712c9940b573c96709 to your computer and use it in GitHub Desktop.

Select an option

Save a9QrX3Lu/7807496cf55052712c9940b573c96709 to your computer and use it in GitHub Desktop.
ftrace
#! /bin/bash
# Trace system calls in [ftrace] binary file
# Output to ftrace_out.log
tracefs=/sys/kernel/debug/tracing
gcc -o ftrace ftrace.c
echo nop > ${tracefs}/current_tracer # Use NOP tracer at first
echo 0 > ${tracefs}/tracing_on # Disable tracing
echo > ${tracefs}/trace # Clear trace file
./ftrace
cat ${tracefs}/trace > ftrace_out.log
#! /bin/bash
# Trace system calls in [ftrace] binary file
# Output to ftrace_out.log
tracefs=/sys/kernel/debug/tracing
gcc -o ftrace ftrace.c
echo nop > ${tracefs}/current_tracer # Use NOP tracer at first
echo 0 > ${tracefs}/tracing_on # Disable tracing
echo > ${tracefs}/trace # Clear trace file
./ftrace
cat ${tracefs}/trace > ftrace_out.log
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <dirent.h>
#include <assert.h>
#include <stdint.h>
#define MAX_PATH 256
const char *tracing_file(const char *file_name) {
static char trace_file[MAX_PATH + 1];
static char tracefs[] = "/sys/kernel/debug/tracing";
snprintf(trace_file, MAX_PATH, "%s/%s", tracefs, file_name);
return trace_file;
}
int main() {
char buf[4096];
char line[64] = {0};
char pid_str[64] = {0};
int current_tracer_fd = open(tracing_file("current_tracer"), O_WRONLY);
int set_ftrace_pid_fd = open(tracing_file("set_ftrace_pid"), O_WRONLY);
int trace_marker_fd = open(tracing_file("trace_marker"), O_WRONLY);
int tracing_on_fd = open(tracing_file("tracing_on"), O_WRONLY);
int write_count = 0;
char filename[64] = {0};
// format pid
write_count = sprintf(pid_str, "%d\n", getpid());
if (current_tracer_fd < 0 || set_ftrace_pid_fd < 0 ||
trace_marker_fd < 0 || tracing_on_fd < 0) {
printf("Open files failed");
exit(-1);
}
//[tracing]# echo $$ > set_ftrace_pid
write_count = sprintf(line, "%d\n", getpid());
write(set_ftrace_pid_fd, line, write_count);
//[tracing]# echo function_graph > current_tracer
strcpy(line, "function_graph");
write(current_tracer_fd, line, strlen(line));
for (int i = 0;i < 1000; i++) {
sprintf(filename, "/mnt/pmem%s/%d.tmp", "1.3", i);
if (i == 999) {
//[tracing]# echo 1 > tracing_on
write(tracing_on_fd, "1", 1);
//[tracing]# echo "syscall trace begin" > trace_marker
strcpy(line, "Syscall trace begin");
write(trace_marker_fd, line, strlen(line));
}
/*
* Make Your system Call Here!
*/
open(filename, O_CREAT);
if (i == 999) {
//[tracing]# echo "syscall trace end" > trace_marker
strcpy(line, "Syscall trace end");
write(trace_marker_fd, line, strlen(line));
//[tracing]# echo 0 > tracing_on
write(tracing_on_fd, "0", 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment