While debugging a kernel module that we were developing, we wanted a kernel with some hacking options enabled, specially the kmemleak feature. Here is how we got that done. We were using Ubuntu i386 12.04.2 on a VirtualBox guest virtual machine running on a Windows 7 host.
- Make sure you have atleast 7GB of free space on your system. While creating the VM, I set my disk size (dynamically expanding) to 20GB.
- Also make sure you do this on the latest Long Term Support (LTS) version of the Ubuntu supported at the time.
- Install VirtualBox Guest editions if using a VM so that it clipboard sharing is enabled making it easier for you to copy and paste command from the Host OS to Guest OS.
- Update the system. This is essential as you need the very latest collection of userpace programs, kernel, and userspace-kernelspace libraries which are know to correctly work together at the time you try this. This is necessary becuase when you download the kernel sources, it will be of the latest kernel which should match the one already running on your system successully with other programs - hence when this kernel is recompiled, nothing else would stop working does to version mismatch:
- sudo apt-get install update
- sudo apt-get install upgrade
- Also install all the update suggested by the GUI update manager
- Check the kernel version by executing the command: uname -aMine was: Linux anurag-VirtualBox 3.5.0-28-generic #48~precise1-Ubuntu SMP Wed Apr 24 21:43:05 UTC 2013 i686 i686 i386 GNU/Linux
- Install the required tools, libraries and the kernel sources (more than 600MB of download):
- sudo apt-get install git-core libncurses5 libncurses5-dev libelf-dev asciidoc binutils-dev linux-source qt3-dev-tools libqt3-mt-dev dpkg-dev fakeroot build-essential crash kexec-tools makedumpfile kernel-wedge kernel-package
- sudo apt-get source linux-image-$(uname -r)
- sudo apt-get build-dep linux-image-$(uname -r)
- If you check your home directory, you will find the following files and directories:
- Change directory to ~/linux-lts-quantal-3.5.0
- Enable permissions on the scripts:
- sudo chmod a+x debian/scripts/*
- sudo chmod a+x debian/scripts/misc/*
- Clean the build directory:
- sudo fakeroot debian/rules clean
- Edit the configurations and enable the required features
- sudo fakeroot debian/rules editconfigs
- Ignore configurations for all architectures except for i386/config.flavour.generic - for this architectures, enable the kmemleak option under Kernel Hacking and save the configuration file and exit.
- Start the build:
- sudo fakeroot debian/rules clean
- sudo fakeroot debian/rules binary-headers binary-generic
- Compilation will take a few hours
- Change directory to ~ (your home directory). There you will find few newly created .deb files:
- Install the kernel image, kernel headers and reboot:
- sudo dpkg -i *.deb
- sudo reboot
- Check if kmemleak has been enabled, execute sudo ls /sys/kernel/debug/kmemleak and if you don't get the error "No such file or directory", it means kmemleak is now enabled
- Check the kernel version again using uname -a. The kernel compilation date will now have changed:Linux anurag-VirtualBox 3.5.0-28-generic #48~precise1 SMP Thu May 9 00:13:36 IST 2013 i686 i686 i386 GNU/Linux
- Refer to kmemleak.txt in the Documentation directory of linux kernel sources to know how to use Kmemleak:
- To check if kmemleak has started after your computer has started, check the kernel messages:
dmesg | grep kmemleak
You will see two messages like so:
[ 3.840286] kmemleak: Kernel memory leak detector initialized
[ 3.840364] kmemleak: Automatic memory scanning thread started
- If you had selected the kmemleak test module option, you will be able to load that module and artificially create orphan obecjts to test if your kmemleak is working or not:
sudo insmod /lib/modules/3.5.0-28-generic/kernel/mm/kmemleak-test.ko
- Before you can write configuration options (for example to trigger scan: sudo echo "scan" > /sys/kernel/debug/kmemleak), you have to make the file writeable:
sudo chmod 777 /sys/kernel/debug/kmemleak
You will have to do this every time after a reboot.
how to do same above steps on android kitkat? please guide me. thank you.ReplyDelete
I havent tried compiling android before.. so I dont have any idea. Although there has to be a similar config file that you could modify to do the same.
If you are talking abut detecting leaks in android Java apps then kmemleak is not relevant to you. Java has automatic garbage collection and it has its own profiling tools that you could use.
kmemleak is only for kernel modules coded in C.
It is also not useful for detecting memory leaks for userspace programs and libraries coded in C.
Thank you for your quick reply and help. Of course I am aware about GC in java but I wanna check kernel level memory leakage in android so, I have only one option that kmemleak. any way let me do some experiment by my own. Do you know any other kernel level memory leakage detector tool? Let me know.ReplyDelete
I think the right commands are:ReplyDelete
sudo apt-get update
sudo apt-get upgrade