Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use printk in kernel

I am trying to implement my own new schedule(). I want to debug my code.

Can I use printk function in sched.c?

I used printk but it doesn't work. What did I miss?

like image 981
user1967799 Avatar asked Jan 11 '13 22:01

user1967799


2 Answers

Do you know how often schedule() is called? It's probably called faster than your computer can flush the print buffer to the log. I would suggest using another method of debugging. For instance running your kernel in QEMU and using remote GDB by loading the kernel.syms file as a symbol table and setting a breakpoint. Other virtualization software offers similar features. Or do it the manual way and walk through your code. Using printk in interrupt handlers is typically a bad idea (unless you're about to panic or stall).

If the error you are seeing doesn't happen often think of using BUG() or BUG_ON(cond) instead. These do conditional error messages and shouldn't happen as often as a non-conditional printk

Editing the schedule() function itself is typically a bad idea (unless you want to support multiple run queue's etc...). It's much better and easier to instead modify a scheduler class. Look at the code of the CFS scheduler to do this. If you want to accomplish something else I can give better advice.

like image 83
Jesus Ramos Avatar answered Sep 24 '22 21:09

Jesus Ramos


It's not safe to call printk while holding the runqueue lock. A special function printk_sched was introduced in order to have a mechanism to use printk when holding the runqueue lock (https://lkml.org/lkml/2012/3/13/13). Unfortunatly it can just print one message within a tick (and there cannot be more than one tick when holding the run queue lock because interrupts are disabled). This is because an internal buffer is used to save the message.

You can either use lttng2 for logging to user space or patch the implementation of printk_sched to use a statically allocated pool of buffers that can be used within a tick.

like image 35
2 revs Avatar answered Sep 23 '22 21:09

2 revs