1

Ftrace is controlled by manipulating the debug fs files. But how is it possible ? How kernel be aware of writing in this files and start its required actions?for exmple if 1 is written to tracing_on , kernel turns on tracing . Is it something related to a special file structure or kernel subscribes to changes of debugfs or I'm missing something obvious ?

0

I think I found it . according to this link In the kernel, a character-type device is represented by struct cdev, a structure used to register it in the system. Most driver operations use three important structures: struct file_operations, struct file and struct inode. As mentioned above, the character device drivers receive unaltered system calls made by users over device-type files. Consequently, implementation of a character device driver means implementing the system calls specific to files: open, close, read, write, lseek, mmap, etc. These operations are described in the fields of the struct file_operations structure:

struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
[...]
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
[...]
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
[...]

so we can initialize above function pointers with our desired functions. for example for tracing_on this is the initialization in kernel/trace/trace.c and the functions are implemented in the same file:

static const struct file_operations rb_simple_fops = {
.open       = tracing_open_generic_tr,
.read       = rb_simple_read,
.write      = rb_simple_write,
.release    = tracing_release_generic_tr,
.llseek     = default_llseek,
};

and also this is the code for creating this directory:

trace_create_file("tracing_on", 0644, d_tracer,
          tr, &rb_simple_fops);
  • The code in /sys/kernel/debug/tracing is implemented by the tracefs file system. You can look at the code in fs/tracefs. The tracefs file system is a pseudo file system, which means there's no actual hardware behind it (like debugfs and tmpfs). By writing 1 into tracing_on, it triggers several system calls (sys_open, sys_write, sys_close), which will follow those functions you point out, and eventually you get to rb_simple_write() which will call tracer_tracing_on() which starts enables the ring buffer to allow recording. – nevets May 27 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.