Wednesday, September 17, 2014

Capture and monitor dmesg continuously

Greetings folks!

If you are working on device drivers for Linux/Android we do rely on dmesg for almost all of the times. But you know the dmesg buffer is limited, its circular buffer. So what ever overflows the buffer is lost for good (?). We feel need to increase the buffer size but its not trivial task and also most of the times enough is just not enough!

Here are some simple scripts for Linux to log dmesg continuously and dump into file and at the same time you can monitor changes in dmesg!

Give exe permissions to script and run as bellow,
$sudo ./ldmesg.sh

#!/bin/bash
# log dmesg to local dmesg.log file with human readable time stamp
while true; do
sleep 1
dmesg -Tc >> dmesg.log;
done
view raw ldmesg.sh hosted with ❤ by GitHub
Now, dmesg is getting continuously logged in dmesg.log file in current directory. Next script needs to be run in another terminal tab/window which will monitor changes in dmesg (actually dmesg.log).

Give exe permissions to script and run as bellow,
$./mdmesg.sh


#!/bin/bash
# monitor changes in dmesg (actually dmesg.log)on stdout
watch "cat dmesg.log | tail -20"
view raw mdmesg.sh hosted with ❤ by GitHub
Image bellow shows dmesg being monitored.

For Android developers, here is ldmesg modified to use it over adb,
#!/bin/bash
# log dmesg to local dmesg.log file with human
# readable time stamp for Android
while true; do
sleep 1
adb shell dmesg -Tc >> dmesg.log;
done
view raw ldmesg_adb.sh hosted with ❤ by GitHub
 
-- Cheers!!

0 comments:

Post a Comment