Debugging on Linux
===
###### tags: `Debug` `QCT`
## Command Line Tools
### Disk
* check mount point of device partition
```
df -h
```
* check file system type of device partition
```
sudo parted
(parted) print list
```
* check LVM
```
sudo lvmdiskscan
sudo pvscan
sudo vgscan
sudo lvscan
```
* modify disk partition
```
sudo fdisk -l
sudo fdisk /dev/sda
sudo partprobe
```
### File
* ```du -ah```
### Network
* netstat
```
netstat -nlptu
-l, --listening (Show only listening sockets)
-n, --numeric (Show numerical addresses)
-p, --programs (Show the PID and name of the program)
```
### Log
* tailf
**tailf** will print out the last 10 lines of a file and then wait for the file to grow.
* systemd日誌
- [journalctl工具基础介绍](https://blog.51cto.com/13598893/2072212)
```journalctl -u httpd.service```
```journalctl _UID=33 --since today```
```journalctl -p err```
```journalctl -f```
* syslog
紀錄process或kernel message的檔案。
### Process
* ```ps```: 顯示process的user ID, group ID, PID, parent PID, TTY, memory usage, status等。
* ```top```: process依據CPU負載排序顯示。
* ```kill```: 作業系統給process送TERM訊號,process自行結束。
* ```kill -9```: 作業系統給process送KILL訊號,process被作業系統強迫結束。
* strace: trace **system calls** and signals
```strace -i [CMD ARG1 ARG2]```
* [GDB](https://hackmd.io/@derailment/gdb)
### Kernel Message
Kernel輸出的訊息,從console, syslog, dmesg觀察。
* [dmesg 指令的用法](https://imsardine.github.io/2016/11/06/dmesg-command/)
開機時,kernel 被載入記憶體,接著 module/driver 開始驅動硬體,過程中會輸出大量的訊息 (message);開機完成後,kernel 偶爾也會產生一些有助於診斷問題 (diagnostic) 的 message,例如 I/O 發生問題、USB 裝置熱插拔時。
```dmesg -Hw```
## Glossary
### CPU架構
* i386 (x86)
Intel 32-bit 架構通稱。
* x86_64
64-bit 架構通稱,能執行i386指令。
### Thread & Process
* Kernel最小可管理的程序運行實例為thread。
* Process是由一個或多個thread組成的程序運行實例,所有thread共享process配到的記憶體。
### Real-time Process
比一般process優先級更高,在主動釋放CPU或被其他優先級更高的real-time process搶佔之前,一直佔有CPU。
### User Space & Kernel Space
應用程式能訪問的虛擬記憶體為user space, 只有內核才能訪問的虛擬記憶體為kernel space。
### Crash
應用程式或作業系統突然停止運行。
* 應用程式crash要用core dump
* Core Dump
當程序(Process)運行的過程中異常終止或崩潰時,會把記憶體快照、暫存器訊息(程式計數器、堆疊指標)、記憶體管理信息、其他處理器和作業系統狀態存到檔案。
* 作業系統crash要用crash dump
* Crash Dump
保存內核(Kernel)的暫存器和記憶體快照。