Developing Linux Kernel with Netbeans

Introduction#

This tutorial shows how to develop Linux kernel with Netbeans. It contains not only information how to create simple "hello world" module, but complete step-by-step guide how-to:
  • build Kernel trunk for developing,
  • debug kernel start up,
  • debug parts of kernel.

Developing Linux Kernel with Netbeans_第1张图片

Netbeans IDE#Pre requirements #

It's one of two most important thing :) you need to get it before proceed. Download IDE from  http://www.netbeans.org  (we use 7.0 version) - You don't need to download full version. After installation go to Tools -> Plugins  and install (if not installed) following additions:
  • C/C++
  • Gdbserver

Configuration#

Working with C/C++ projects may require tuning JVM, and assigning greater heap size. We use following options added in  <Netbeans Install Dir>/etc/netbeans.conf  under key  netbeans_default_options
-J-XX:+UseCompressedOops -J-XX:+UnlockExperimentalVMOptions -J-XX:+UseG1GC 
above will reduce memory usage in 64bit virtual machines (by using 32bit pointers), unlock experimental features, and turn on quite good G1 Garbage Collector
-J-mx1g -J-XX:MaxPermSize=412m -J-Xms512m
and those sets maximum heap size to 1GB, maximum Perm size to 412 MB, and initial heap size to 512 MB

Linux Kernel Sources#

This is 2nd most important things. You may download source archive, or grab current version from git (when we wrote this, we used this command)
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git linux-2.6

Configure kernel#

Execute
make oldconfig
to configure kernel as your current configuration.

Now open configuration editor, if you have Qt/KDE installed call

make xconfig
in other cases call
make menuconfig
Disable all unneeded features, drivers, etc. If you don't make this parsing of sources, and build time may drastically increase - remove such things like Blluetooth, WiFi, all file systems except Ext2/3/4, NFS, sound cards support, PPP support, etc. Just keep this what you need.
Select at least following options
Kernel hacking
|- Debug Filesystem
|- Kernel debugging
|- Verbose BUG() reporting
|- Compile the kernel with debug info
|- Compile the kernel with frame pointers
Options  Compile the kernel with debug info Compile the kernel with frame pointers  are crucial.

Additionally you make edit Makefile and replace all -O2 to -O1 to reduce optimization.

At this point Your kernel source should be ready for working, but do not open your project in Netbeans yet.

qEmu / KVM#

qEmu  or  KVM  are virtual machines, we will use to run kernel.  qEmu  mainly doesn't support hardware acceleration of virtualization tasks, but have ability to emulate more architectures.  KVM  may be faster, but will only emulate your host system architecture. At least one of this must be installed.

KVM - Hardware acceleration of virtualization#

KVM  stands for Kernel Virtual Machine; it uses processor extension, found on almost all Intel(r) and AMD(r), to speed up running in virtual machine. In order to give support for  KVM  You need to:
  • Compile kvm modules in your system kernel. Probably your host kernel has it, if not enable it in configuration editor, giving support for Intel or AMD or both technologies.
  • Load modules as root invoke commands modprobe kvm-intel, or modprobe kvm-amd.
  • You need to have special privileges to use KVM as normal user, in many systems add your user to
kvm  group (sometimes you will need to relog).

Please bear in mind, that depending on systems, kvm may be called by different commands:

kvm, quem-kvm    

Otehrs#

  • Set of developer tools - including gcc, make
  • small and fast Linux distribution - it will be used as our test environment, we prefer Gentoo stage 3,
because it starts fast and may be nicely customized form useless services or start options. But... the best distribution is always those you know!

Building with Netbeans#

Note
In this tutorial we use mainly  qemu-system-x86_64  command to invoke qemu/kvm virtual machine, both of those have same set of options (at least for this tutorial), depending on virtual machine You choose You may replace  qemu-system-x86_64 , by
kvm, qemu-kvm

Compile kernel#

You should first build your kernel. Go to source tree and invoke
    make -j5
The  -j5  informs  make  how much tasks should it span. We use 5 tasks, bear in mind following rule  number of task = number of CPU or cores + 1

Running QEmu#

We use following call (from kernel source directory)
qemu-system-x86_64 -hda /dev/zero -kernel arch/x86/boot/bzImage --no-acpi -s -S
options have following meaning:
-hda /dev/zero
Use  /dev/zero as your disk (at this moment we don't want usable hard disk)
-kernel arch/x86/boot/bzImage
Loads specified kernel
--no-acpi
Do not use ACPI
-S
Do not start CPU
-s
Open GDB server at port 1234
If you develop x86 (32 bit) you may call instead of  qemu-system-x86_64 , just  qemu

Now invoke above command without debug help (no -s -S). You should see how kernel starts, and crashes as it have no root file system.

Developing Linux Kernel with Netbeans_第2张图片
Opening Linux kernel in Netbeans
#

  • Choose File -> New project, select C/C++ Project From Existing Sources, click Next.
  • Enter (or browse for) path where your kernel source exists.
  • Select Custom configuration. Click Next.
  • Disable Clean and Build after Finish (You have already kernel build). Click Next.
  • On Build actions step, point Build result to vmlinux file, which should resides in your source directory (after build). Click Next.
  • Click Next till last step. Do not finish yet!

You may need to do following action fast (to reduce Netbeans memory consumption on project parsing).

Right click on Your newly created project, Choose Code Assistance, unmark C/C++ Code Assistance.

Developing Linux Kernel with Netbeans_第3张图片


Netbeans should not parse code, ok. Now, ight click on Your newly created project, Choose Code Assistance, and select Configure code assistance.

  • Choose Advance mode, click Next.
  • Select Explore executable or library file, click Next.
  • In Select executable or library file browse for vmlinux (like when you had configured project), click Next.
  • Select Configure Folder Properties, click Next, then Finish.
Now enable Code Assistance for this project (right click on Your newly created project, Choose  Code Assistance , mark  C/C++ Code Assistance ). Netbeans should start scanning and parsing.
Now You are ready to debug Linux kernel in Netbeans.

Lunching and debugging#

Actually I prefer building kernel in console instead of using Netbeans, but you may build it with Netbeans.
  • Open console and invoke qemu, this time use debugging switches (-s -S). Qemu should freeze.
  • Select Debug -> Attach debugger..., or click on small arrow on right of Debugging button on toolbar.
  • Select Gdbserver and enter Host: 127.0.0.1, Port: 1234

Developing Linux Kernel with Netbeans_第4张图片
You attached to debugger - You should see Disassembly window and.

As the test, close Disassembly window, open ipc/shm.c, navigate to shm_init method, and put breakepoint on 1st line in this method. Click continue (or press F5). You should see result as on the top of this article!


 reference: Developing Linux Kernel with Netbeans

你可能感兴趣的:(linux,kernel,Netbeans,debugging)