# 在本機Linux建置除錯Linux5.0 kernel 環境 OS: Ubuntu 22.04 LTS CPU: Intel® Core™ i5-10300H CPU @ 2.50GHz ## 1. 重新編譯核心 ### 1.1 安裝編譯核心的軟體 ```bash sudo apt-get install libncurses-dev flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf ``` ### 1.2 安裝(舊版本)gcc 由於Linux kernel會綁gcc的版本,太新的gcc(例如我的是11.2.0)會無法編譯核心 但是系統似乎還是太新,無法用簡報做法安裝gcc-5(apt install gcc-5),因此使用以下方法: 比7更舊的gcc版本,會放在一個叫xenial的repo裡,我們要把它加進```/etc/apt/sources.list```(但我不知道為什麽): ``` sudo vim /etc/apt/sources.list ``` 寫入: ``` deb http://dk.archive.ubuntu.com/ubuntu/ xenial main deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe ``` 然後再下載: ``` sudo apt update sudo apt install gcc-5 --allow-insecure-repositories sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 550 ``` 我們下載gcc-5的地方被判定: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY... 最後會顯示錯誤訊息 ex:``` The following signatures couldn't be verified because the public key is not available: NO_PUBKEY```,這會使我們無法取得gcc-5,因此加入後面那項參數允許從不安全的repo安裝東西 作法參考: [Ubuntu 20.04 - gcc version lower than gcc-7 | askUbuntu](https://askubuntu.com/questions/1235819/ubuntu-20-04-gcc-version-lower-than-gcc-7) [How can I get past a “Repository is not signed…” message when attempting to upgrade Kali Linux from a mounted disk image? | StackExchange](https://superuser.com/questions/1331936/how-can-i-get-past-a-repository-is-not-signed-message-when-attempting-to-upgr) ### 1.3 切換不同版本 gcc 的方法 執行: ``` sudo update-alternatives --config gcc ``` 會有選單讓我們選要用哪個:(來自教授投影片)  ### 1.4 用gcc-5編譯linux5.0 到osdi裡面的linux-5.0目錄,執行 ```make -j8``` 這裡我遇到錯誤: ``` In file included from scripts/selinux/mdp/mdp.c:49:0: ./security/selinux/include/classmap.h:249:2: error: #error New address family defined, please update secclass_map. #error New address family defined, please update secclass_map. ^ In file included from scripts/selinux/genheaders/genheaders.c:19:0: ./security/selinux/include/classmap.h:249:2: error: #error New address family defined, please update secclass_map. #error New address family defined, please update secclass_map. ^ ``` 解決辦法: 1. **找到出錯的的 genheader.c 和 mdp.c 文件** 刪除引入的header file: ```#include <sys/socket.h>``` 2. **找到classmap.h** 引入header file: ```#include <linux/socket.h>``` 作法參考: [编译错误 error New address family defined, please update secclass_map.解决 | CSDN](https://blog.csdn.net/zhangpengfei991023/article/details/109672491) 然後再重新編譯就可以了(會要等一下,但不會太久) ## 2. Qemu相關 ### 2.1 configure 到放qemu的目錄底下(教授的資料夾就是 /osdi/qemu-6.0.0/)執行: ```bash ./configure ``` ### 2.2 編譯 同目錄執行: ```bash make ``` (會弄超久) ### 2.3 下載x86模擬器 終端機再輸入: ``` sudo apt-get install qemu-system-x86 ``` ## 3. Eclipse 設置 ### 3.1 下載 Eclipse **Eclipse** 步驟在:[Eclipse IDE for C/C++ Developers installation on Ubuntu 22.04 | LinuxConfig](https://linuxconfig.org/eclipse-ide-for-c-c-developers-installation-on-ubuntu-22-04) 注意版本型號,教學中其中有個指令是: ```sudo tar xf eclipse-cpp-2022-03-R-linux-gtk-x86_64.tar.gz -C /opt``` 要把 ```eclipse-cpp-2022-03-R-linux-gtk-x86_64.tar.gz``` 改成自己下載的版本(最新是2022-09-R),反正就看下載到了什麽 **CDT** 步驟在:[在Eclipse安裝CDT(C/C++ Development Tool)套件 | 大叔筆記](https://sites.google.com/a/cnsrl.cycu.edu.tw/da-shu-bi-ji/tools/zaieclipseanzhuangcdtccdevelopmenttooltaojian) 網站最前面說去某個網站看p2後的連結那裡忽略,反正install的地方就是貼 https://download.eclipse.org/tools/cdt/releases/latest/ ### 3.2 修改 Debug configure 開啟eclipse,選擇上方run >> debug configure  右邊選 Attach to Application,左邊更改名稱,選擇vmlinux位置(應在.../osdi/linux5.0/vmlinux)  在 Debugger 裡面改:  最後 Source 裡面要 Add Absolute File Path ### 3.3 開始偵錯 在 osdi 目錄下執行 ```./dbg-Linux5.0-in-QEMU.sh``` 開啟 eclipse,在 Debugger History 裡面找到剛剛的設置  在下方的 Debugger Console 裡面輸入```file (vmlinux的位置)``` (應在.../osdi/linux5.0/vmlinux)  之後就可以開始除錯了
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up