Linuxã«ã¼ãã«Hack: GDBã¨KVMã«ããã«ã¼ãã«ãããã°
ããã¾ã§UML(User Mode Linux)ã§ã«ã¼ãã«ãããã°ã§ããç°å¢ã使ã£ã¦ãã«ã¼ãã«ã®è§£æçãè¡ã£ã¦ãããUMLã®ç°å¢ã¯ãVMWare Serverä¸ã®Ubuntu 8.0.4ã«æ§ç¯ãã¦ããããæè¿ãèµ·åããªã£ã¦ãã¾ã£ããããã¾ã§KVM(Kernel-based Virtual Machine)ã«èå³ããã£ããã®ã®ãæå ã«ã¯Intel VT対å¿CPUãç¡ãããã£ã¨è©¦ããªãç¶æ³ã ã£ããããã§ãIntel VT対å¿ã®ãã·ã³ãæ°ãã«è³¼å ¥ãããã®ãã·ã³ã«Linuxã«ã¼ãã«éçºç°å¢ãä½ããã¨ã«ããã
ä½è«: è³¼å ¥ãããã·ã³ã«ã¤ãã¦
è³¼å ¥ãããã·ã³ã¹ããã¯ã¯ã以ä¸ã®éãão'zzioã®XRã·ãªã¼ãºãhttp://www.ozzio.jp/html/ozzio_xr_specs.htm ãã®ãã·ã³ã«Ubuntu 10.10ãã¤ã³ã¹ãã¼ã«ããã
- CPU: Intel Core i7 870 (8MB L3ãã£ãã·ã¥/2.93GHz)
- ã¡ã¢ãª: DDR-3 SDRAM(PC3-10800) 2GB
- HDD: 500GB Serial ATA
- ãããªã«ã¼ã: NVIDIA GeForce GT220 / 1GB
- ãã©ã¤ã: DVDã¹ã¼ãã¼ãã«ããã©ã¤ã
- LAN: ã®ã¬ããã(10/100/1000) LAN
OSç¡ãã¢ãã«ã§8ä¸åã ã£ããã¡ã¢ãªã¼ã追å ã§2GBè²·ã£ãã®ã§9ä¸ãããã«ã¯ãªã£ãããã©ãããã®å¤æ®µã§ããã ãã®ã¹ããã¯ãæã«å ¥ãæ代ã«ãªã£ãã®ã ãªã¨æåãããè³¼å ¥ããæã¯å ¨ãæ°ã«ãã¦ããªãã£ããããªã«ã¼ãããããªã«ã¼ãã¯Ubuntu 10.10ã¨ãç¸æ§ãè¯ãããã§ãä»ã®ã¨ããå¿«é©ã«ä½¿ãã¦ãããNVIDIAã®ãã©ã¤ããã¤ã³ã¹ãã¼ã«ãããã¨ã§ããã¥ã¢ã«ãã£ã¹ãã¬ã¤ãç°¡åã«è¨å®ã§ããã
KVMã®ã¤ã³ã¹ãã¼ã«
ãã¦ãããããKVMã®ã¤ã³ã¹ãã¼ã«ã«å
¥ãã次ã®ãã¼ã¸ãåèã«ãã¦ã¤ã³ã¹ãã¼ã«ãããhttp://www.atmarkit.co.jp/flinux/rensai/kvm02/kvm02a.html
KVMã¨ãã¦èµ°ãããOSã¯ãUbuntu 10.10ã«ããã
以ä¸ã®æé ã§ã¤ã³ã¹ãã¼ã«ããã
% sudo apt-get install kvm bridge-utils % mkdir ~/kvm % cd ~/kvm
50GBã®ä»®æ³ãã£ã¹ã¯ãä½æã
% kvm-img kvm-img create -f qcow2 ubuntu.img 50GB
ãã©ã¤ãã«Ubuntu 10.10ã®ãã£ã¹ã¯ãå ¥ãã¦ããã以ä¸ãå®è¡ãUbuntuã®ã¤ã³ã¹ãã¼ã©ãèµ·åãããç¹ã«ä½ãèããã»ã¼ããã©ã«ãã®è¨å®ã§ã¤ã³ã¹ãã¼ã«ãããå¤æ´ããè¨å®ã¨ãã¦ã¯ããã¹ã¯ã¼ããå ¥åããªãã¦ãèªåãã°ã¤ã³ã§ããããã«ãããã¨ããã®VMã¯èªåãã使ããªãããèµ·åã®é½åº¦ãã¹ã¯ã¼ããå ¥ããã®ã¯é¢åãªã®ã§ã
% kvm -hda ubuntu.img -cdrom /dev/cdrom -boot d -m 384 -monitor stdio
ã¤ã³ã¹ãã¼ã«å®äºãã²ã¹ãOSãèµ·åãã¦ã¿ããæå ã®ç°å¢ã§ã¯10ç§ã»ã©ã§Ubuntuãèµ·åããããããªã«ãéãèµ·åã§ããã®ãã¨ãé©ããã
% kvm -hda ubuntu.img -boot c -m 384 -monitor stdio
ãããã°ã·ã³ãã«(vmlinux)ã®æºå
ãã®è¨äºã«ããã¨ãGDBããKVMããªã¢ã¼ããããã°ã§ãããããã
http://d.hatena.ne.jp/big-eyed-hamster/20091211/1260540819
ãããã°ããã«ã¯GDBã«Linuxã«ã¼ãã«ã®ã·ã³ãã«æ å ±ãèªã¿è¾¼ã¾ããå¿ è¦ããããã·ã³ãã«æ å ±ã¯vmlinuxã«å ¥ã£ã¦ããã
ãããã次ã®ãã¼ã¸ã®æ
å ±ã«ããã¨ãUbuntuã«ã¯vmlinuxã¯æä¾ããã¦ããããèªåã§ã«ã¼ãã«ããã«ããã¦ç¨æããå¿
è¦ããããããã
http://www.crashcourse.ca/introduction-linux-kernel-programming/intermission-building-vmlinux-file-under-ubuntu-free-lesson
ããã§ãKVMã«ã¤ã³ã¹ãã¼ã«ããUbuntuã®ã«ã¼ãã«ã¨åä¸ã®ã«ã¼ãã«ããã«ããããã¨ã§vmlinuxãã¡ã¤ã«ãç¨æãããã¨ã«ããã
Ubuntu 10.10ã«ã¼ãã«ã®gitããªã¼ã¯https://wiki.ubuntu.com/Kernel/SourceCodeãè¦ãã¨ãgit://kernel.ubuntu.com/ubuntu/ubuntu-maverick.git ã®æ¨¡æ§ã
% sudo apt-get build-dep linux-image-$(uname -r) % git clone git://kernel.ubuntu.com/ubuntu/ubuntu-maverick.git
ã«ã¼ãã«ã®å³å¯ãªãã¼ã¸ã§ã³ã調ã¹ãã
$ uname -a Linux xr 2.6.35-22-generic #35-Ubuntu SMP Sat Oct 16 20:36:48 UTC 2010 i686 GNU/Linux
調ã¹ããã¼ã¸ã§ã³ãgit checkoutããã
% cd ubuntu-maverick % git checkout Ubuntu-2.6.35-22.35
説æã«å¾ããcleanãã¦ããã«ã¼ãã«ããã«ããããã©ããããããã ãã§ã«ã¼ãã«ã並åãã«ããã¦ãããããã ããã«ãä¸ã«topã³ãã³ãã§ç¢ºèªããã¨ccãåæã«8ã¤ãããå®è¡ããã¦ãããã¨ã確èªããã
% fakeroot debian/rules clean % fakeroot debian/rules binary-generic
ãã«ãå®äºãdebian/build/build-generic/vmlinuxãä½æãããã
GDBããã®ãªã¢ã¼ããããã°
GDBããKVMããªã¢ã¼ããããã°ããã«ã¯ãKVMã®èµ·åæã«-sãªãã·ã§ã³ãæå®ããå¿ è¦ããããman kvmãè¦ãã¨ãä»ã«ãæ§ã ãªèµ·åãªãã·ã§ã³ãããããã ã
% kvm -hda ubuntu.img -boot c -s -m 384 -monitor stdio
GDBãããªã¢ã¼ããããã°ãã¦ã¿ããfileã³ãã³ãã§vmlinuxãã¡ã¤ã«ãæå®ããã«ã¼ãã«ã®ã·ã³ãã«æ å ±ããã¼ãããããã¬ã¼ã¯ãã¤ã³ãã®æå®ãèãããä»åã¯ç¹ã«ãããã°ãããæã¯ç¡ãã®ã§ããã¬ã¼ã¯ãããã¨ããããé¢æ°ãªããªãã§ãè¯ããããè¨ãã°ãKVMã®Ubuntuã®ãã¡ã¤ã«ã·ã¹ãã ã¯ãããã©ã«ãã®è¨å®ã§ã¤ã³ã¹ãã¼ã«ããã®ã§ãExt4ã«ãªã£ã¦ãããé¢æ°åãããã¦ããã¯å¼ã°ãããã¨ãããã¨ã§ãext4_writepageãæå®ãã¦ã¿ããã¨ã«ããã-sãªãã·ã§ã³ã§èµ·åããKVMã¯ãããã©ã«ã1234ãã¼ãã§åãä»ããã®ã§ãããã«å¯¾ãã¦æ¥ç¶ãæ¥ç¶ã§ããç¬éãnative_safe_haltã§åæ¢ããããããããã®ãªã®ãã
% gdb (gdb) file ~/ubuntu-maverick/debian/build/build-generic/vmlinux Reading symbols from /home/fixme/ubuntu-maverick/debian/build/build-generic/vmlinux...done. (gdb) b ext4_writepage Breakpoint 1 at 0xc029b91e: file /home/fixme/ubuntu-maverick/fs/ext4/inode.c, line 2681. (gdb) target remote localhost:1234 Remote debugging using localhost:1234 native_safe_halt () at /home/fixme/ubuntu-maverick/arch/x86/include/asm/irqflags.h:50 50 }
å¦çãç¶è¡ããããã°ããããã¨ãè¨å®ãããã¬ã¼ã¯ãã¤ã³ãã§åæ¢ãããããã¯ãã¬ã¼ã¹ãã¡ããã¨åããã
(gdb) c Continuing. Breakpoint 1, ext4_writepage (page=0xc110d7c0, wbc=0xd6521e98) at /home/fixme/ubuntu-maverick/fs/ext4/inode.c:2681 2681 trace_ext4_writepage(inode, page); (gdb) bt #0 ext4_writepage (page=0xc110d7c0, wbc=0xd6521e98) at /home/fixme/ubuntu-maverick/fs/ext4/inode.c:2681 #1 0xc01e0760 in __writepage (page=0xc110d7c0, wbc=0xd6521e98, data=0xd55cc3b0) at /home/fixme/ubuntu-maverick/mm/page-writeback.c:999 #2 0xc01e16d6 in write_cache_pages (mapping=0xd55cc3b0, wbc=<value optimized out>, writepage=<value optimized out>, data=0xd55cc3b0) at /home/fixme/ubuntu-maverick/mm/page-writeback.c:932 #3 0xc01e18b4 in generic_writepages (mapping=0xc110d7c0, wbc=0xd6521e98) at /home/fixme/ubuntu-maverick/mm/page-writeback.c:1019 #4 0xc02d28e9 in journal_submit_inode_data_buffers ( journal=<value optimized out>, commit_transaction=<value optimized out>) at /home/fixme/ubuntu-maverick/fs/jbd2/commit.c:226 #5 journal_submit_data_buffers (journal=<value optimized out>, commit_transaction=<value optimized out>) at /home/fixme/ubuntu-maverick/fs/jbd2/commit.c:257 #6 0xc02d2ebf in jbd2_journal_commit_transaction ( journal=<value optimized out>) at /home/fixme/ubuntu-maverick/fs/jbd2/commit.c:508 #7 0xc02d8104 in kjournald2 (arg=<value optimized out>) at /home/fixme/ubuntu-maverick/fs/jbd2/journal.c:159 #8 0xc01659e4 in kthread (_create=0xd52b7d1c) at /home/fixme/ubuntu-maverick/kernel/kthread.c:78 #9 0xc010363e in ?? ()
ãããã«
KVMãç¨ãããã¨ã§ãUMLã¨åæ§ã«GDBã§ã«ã¼ãã«ããããã°ã§ãããã¨ã確èªã§ããããã¼ããè¯ããªã£ãã®ãããããUMLãããæç¶ã²ã¹ãOSã®èµ·åæéãéããªã£ãã®ã§ãã«ã¼ãã«éçºç°å¢ã¨ãã¦ãå®ç¨çãªã¬ãã«ã«éãã¦ããã
ããã¾ã§ã§ããã°ãã¡ã¤ã³ã©ã¤ã³ã®ã«ã¼ãã«ã«å¯¾ãã¦ãåæ§ã®ãããã°ã¯é£ãããªãã¯ããªã®ã§ãä»å¾ã¯ãããã§ããããã«ãã¦ã¿ããã