๐ŸŒŒ[Pintos] VSCode๋ฅผ ํ™œ์šฉํ•œ Pintos ๋””๋ฒ„๊น…

์ด๊ฐ•์šฑยท2022๋…„ 12์›” 21์ผ
8

๐ŸŒŒ Pintos

๋ชฉ๋ก ๋ณด๊ธฐ
6/6

Pintos ๊ณผ์ •์ด ๋งˆ๋ฌด๋ฆฌ ๋˜์—ˆ๋‹ค.
์ง„ํ–‰ํ•˜๋ฉด์„œ ํฐ ๋„์›€์„ ๋ฐ›์•˜๋˜ debug ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๊ณต์œ ํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

Native Debug

0-1. pintos-kaist ํ”„๋กœ์ ํŠธ ์ง„ํ–‰์„ ์œ„ํ•œ EC2 ์ดˆ๊ธฐ setting์ด ์™„๋ฃŒ ๋˜์–ด์žˆ์–ด์•ผ ํ•œ๋‹ค.

    $ sudo apt update
  	$ sudo apt install -y gcc make qemu-system-x86 python3
  	$ sudo apt install -y gdb

0-2. VSCode์—์„œ open folder > pintos-kaist๋ฅผ ํ†ตํ•ด ํ˜„์žฌ window์˜ ์ตœ์ƒ์œ„ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ pintos-kaist๋กœ ๋งŒ๋“  ํ›„, ์•„๋ž˜์˜ ๊ณผ์ •์„ ์ง„ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

  1. VSCode์—์„œ Native Debug extension์„ ์„ค์น˜ํ•œ๋‹ค.

  1. pintos ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์— .vscode ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค.

  2. .vscode ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— launch.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค.
    ํ”„๋กœ์ ํŠธ ๋ณ„๋กœ executable ์˜ value๋ฅผ ์กฐ์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    e.g. project2: user programs ์— ๋Œ€ํ•˜์—ฌ,
    "executable": "${workspaceRoot}/userprog/build/kernel.o"

{
  "configurations": [
    {
      "type": "gdb",
      "request": "attach",
      "name": "Attach to gdbserver : threads",
      "executable": "${workspaceRoot}/threads/build/kernel.o",
      "target": "localhost:1234",
      "remote": true,
      "cwd": "${workspaceRoot}",
      "valuesFormatting": "parseText"
    }
  ]
}

  1. ํ˜„์žฌ ์ง„ํ–‰ ์ค‘์ธ ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ (e.g. pintos-kaist/threads/ ) ์— pintos๋ฅผ build ํ•œ๋‹ค. (make)

  2. debug ํ•˜๊ณ  ์‹ถ์€ .c ํŒŒ์ผ์— ๋ธŒ๋ ˆ์ดํฌ ํฌ์ธํŠธ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

  1. ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์˜ build ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์—์„œ pintos ํ…Œ์ŠคํŠธ๋ฅผ ์ง์ ‘ ์‹คํ–‰ํ•œ๋‹ค. script commands์— --gdb๋ฅผ ๋ฐ˜๋“œ์‹œ ํฌํ•จ์‹œ์ผœ์•ผ ํ•œ๋‹ค. (e.g. pintos --gdb -- -q run alarm-multiple).

  2. ๊ทธ๋Ÿฌ๋ฉด qemu๋Š” ํ„ฐ๋ฏธ๋„์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ถœ๋ ฅ์„ ๋ฑ‰์œผ๋ฉด์„œ, ์šฐ๋ฆฌ๊ฐ€ ์‹คํ–‰ํ•  gdb๊ฐ€ ์—ฐ๊ฒฐ๋˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ ค์ค€๋‹ค.

    qemu-system-x86_64: warning: TCG doesn't support requested feature: CPUID.01H:ECX.vmx [bit 5]
  3. VScode ๋งจ ์™ผ์ชฝ์˜ "Run and Debug" ํƒญ์— ๊ฐ€์„œ Attach to gdbserver configuration์œผ๋กœ debug๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

  1. 6๋ฒˆ์—์„œ ์„ค์ •ํ•œ breakpoint์—์„œ debug๋ฅผ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค๋ฅธ debug ๋ฐฉ๋ฒ•๋“ค

printf()

์ตœ๊ณ ์˜ ๊ทผ๋ณธ debug.
๋ชจ๋ฅด๊ฒ ์œผ๋ฉด ๋‹ค ์ฐ์–ด๋ณด๋Š” ๊ฒƒ๋„ ๋ฐฉ๋ฒ•์ด๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋Š”์ง€ ๋˜ํ•œ ๊ทธ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์•„๋ฌด ์ถœ๋ ฅ์ด๋‚˜ ํ•ด๋ณด๋ฉด์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. (e.g. printf("์—ฌ๊ธฐ ์˜ค๋‹ˆ...?"); )

ASSERT()

์œ„์— ์†Œ๊ฐœ๋œ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์—์„œ๋Š” ์ธ๊ฐ„์ธ ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ๋ณ€์ˆ˜๋“ค์˜ ๊ฐ’์„ ํ™•์ธํ•˜๋ฉด์„œ ์˜ค๋ฅ˜๋ฅผ ์ฐพ๋Š”๋‹ค.
ASSERT() ๋งคํฌ๋กœ๋Š” ๊ทธ ํ™•์ธ๊นŒ์ง€ ์ปดํ“จํ„ฐ์—๊ฒŒ ์‹œ์ผœ๋ฒ„๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ฐ„๊ณผํ•˜๊ธฐ ์‰ฝ์ง€๋งŒ ์ž˜ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์ด๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์„ ์ด์œ ๊ฐ€ ์—†๋‹ค. (๋‚ด๊ฐ€ ์ง์ ‘ ํ™•์ธํ•  ์ด์œ ๊ฐ€ ์—†๋‹ค.)

ASSERT()์˜ ์ธ์ž๋กœ ๋“ค์–ด๊ฐ€๋Š” ์‹์ด true ์ผ ๋•Œ๋งŒ ํ•ด๋‹น ASSERT()๋ฅผ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ๊ทธ ๊ฐ’์ด false๋ผ๋ฉด ์ปค๋„์€ PANIC์— ๋น ์ง„๋‹ค. PANIC ๋ฉ”์„ธ์ง€์—๋Š” ๊ฒ€์‚ฌ์‹, ์‹คํ–‰๋œ ํŒŒ์ผ, ๋ผ์ธ ๋ฒˆํ˜ธ, backtrace๊ฐ€ ํฌํ•จ๋œ๋‹ค. ์ด๋ฏธ skeleton code์— ๋งŽ์€ ASSERT()๊ฐ€ ์ž‘์„ฑ๋˜์–ด ์žˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•˜๋Š” ์ฝ”๋“œ์—์„œ๋„ ASSERT()๋ฅผ ์ ๊ทน์ ์œผ๋กœ ํ™œ์šฉํ•ด ์‹ค์ˆ˜๋ฅผ ์ค„์ด๊ณ  debug๋ฅผ ์šฉ์ดํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

C/C++ compiler๋Š” NULL Pointer Exception ๊ฐ™์€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด์ฃผ์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ NULL ํฌ์ธํ„ฐ๋ฅผ ๋ฐœ๊ฒฌํ•˜์ง€ ๋ชปํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ํฌ์ธํ„ฐ ์‚ฌ์šฉ์‹œ์— ๋งค์šฐ ์ด์ƒํ•œ ์ฃผ์†Œ(e.g. 0x600)๋กœ Page fault๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด์„œ kernel์ด ์ฃฝ๋Š”๋‹ค. ๊ฐ€์Šด์ด ๋‹ต๋‹ตํ•ด์ง€๊ณ  ์ •์‹ ์ด ์•„๋“ํ•ด์ง€๋Š” ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์„, ASSERT()๋ฅผ ํ™œ์šฉํ•ด ์ตœ๋Œ€ํ•œ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด, ์–ด๋–ค ํ•จ์ˆ˜ ์•ˆ์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ ค๊ณ  ํ•  ๋•Œ, ์ธ์ž๋กœ ๋ฐ›์€ ๋ณ€์ˆ˜๊ฐ€ ๋‹น์—ฐํžˆ ์ง€์ผœ์•ผํ•  ์†์„ฑ๋“ค์„ ํ•จ์ˆ˜์˜ ์ฝ”๋“œ๊ฐ€ ์‹œ์ž‘ํ•˜๊ธฐ ์ „ ASSERT()๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์˜ค๋ฅ˜์˜ ์›์ธ์„ ๊ฑฐ์Šฌ๋Ÿฌ ์˜ฌ๋ผ๊ฐ€๊ธฐ๊ฐ€ ๋งค์šฐ ์šฉ์ดํ•ด์ง„๋‹ค. ASSERT()์— ์‚ฌ์šฉ๋œ ์‹์ด ์–ด๋–ค ์ด์œ ๋กœ ์ฐธ์ด ๋˜์ง€ ๋ชปํ–ˆ๋Š”์ง€๋ฅผ ๊ณ„์† ๊ณ ๋ฏผํ•˜๋‹ค๋ณด๋ฉด ์›์ธ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ฑดํˆฌ๋ฅผ ๋นˆ๋‹ค.

profile
I think I think too much.

3๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2024๋…„ 4์›” 25์ผ

์ตœ๊ณ ์—์š”!

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2024๋…„ 11์›” 20์ผ

pintOS project ์ˆ˜ํ–‰ ์ค‘์— ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ๋””๋ฒ„๊น… ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ