Skip to content

gdb デバッグ

Tomoki Nakamura edited this page Jun 1, 2022 · 3 revisions

gdb デバッグ

概要

  • 実マシン上の gdb と 鬼斬を TCP 通信することで、鬼斬上で実行しているバイナリのデバッグ情報を取得することができる。

前提とする環境

  • 以下の環境を想定する
    • 鬼斬の実行環境:Windows 上の Visual Studio
    • 実行するバイナリの ISA :RISC-V
    • クロスコンパイラ環境:Windows 上の wsl または Ubuntu
    • gdb の可視化:Windows または Ubuntu 上の Visual Studio Code

準備

鬼斬上でのパラメータ設定

  • System/mode に EmulationWithDebug を指定する
    • 指定方法には以下の2通りがある
    1. 実行時に読み込む param.xml に以下の記述を追記
    <Simulator>
        <System Mode='EmulationWithDebug' SimulationInsns="10G" SkipInsns="100G" />
    </Simulator>
    1. DefaultParam.xml の記述を書き換え
    • 以下に示す System ノードを探し、Mode の部分を "Simulation" から "EmulationWithDebug" に書き換え
    <System
      Mode = "Simulation" 
      SimulationCycles = "0"
      SimulationInsns = "0"
      SkipInsns = "0"
    >
  • 実行するバイナリの指定
    • 後述の launch.json 内 "program" で指定するバイナリと同一のファイルを指定する

gdb 環境の準備

  1. 実行するプログラムを含むフォルダを作成する
  2. .vscode/ 以下に launch.json を作成
    • vscode で上記のフォルダを開き、上側メニュータブから "実行(R)" → "構成の追加" → "Edge: launch" をクリックすると、ディレクトリごと作成してくれる
  3. launch.json を書き換え
    • Windows 上の wsl 環境の場合
    {
        "version": "0.2.0",
        "configurations": [
            {
            "name": "(riscv-remote) Launch",
            "type": "cppdbg",
            "request": "launch",
            "args": [],
            "cwd": "/",
            "stopAtEntry": false,
            "environment": [],
            "externalConsole": false,
    
            // ターゲットバイナリを指定
            // [注意] wsl から見たパスを指定
            "program": "/mnt/c/Users/tomokin.MTL/Documents/git/HelloWorld/bin/a.out",
            "pipeTransport": {
                // gdb のパスを指定
                // [注意] wsl から見たパスを指定
                "debuggerPath": "~/opt/gcc/riscv64-elf/11.1/bin/riscv64-unknown-elf-gdb",
                "pipeProgram": "C:\\Windows\\system32\\bash.exe",
                "pipeArgs": ["-c"],
                "pipeCwd": "/"
            },
            "sourceFileMap": {
                "/mnt/c": "C:\\"
            },
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    // XX.XX.XXX.XX は 鬼斬を実行するマシンの IP アドレスを入力
                    // 5555 は 鬼斬の gdb 通信で指定するポート番号を入力 (デフォルトは 5555)
                    "text": "target remote XX.XX.XXX.XX:5555",
                    }
                ]
            }
        ]
    }
    • Ubuntu 上の場合
    {
        "version": "0.2.0",
        "configurations": [
            {
            "name": "(riscv-remote) Launch",
            "type": "cppdbg",
            "request": "launch",
            "args": [],
            "cwd": "${workspaceRoot}",
            "stopAtEntry": false,
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
    
            // RISC-V の gdb を指定
            "miDebuggerPath": "~/opt/gcc/riscv64-elf/11.1/bin/riscv64-unknown-elf-gdb",
    
            // ターゲットバイナリを指定
            "program": "~/Documents/git/HelloWorld/bin/a.out",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    // XX.XX.XXX.XX は 鬼斬を実行するマシンの IP アドレスを入力
                    // 5555 は 鬼斬の gdb 通信で指定するポート番号を入力 (デフォルトは 5555)
                    "text": "target remote XX.XX.XXX.XX:5555",
                    }
                ]
            }
        ]
    }

実行手順

  1. まず Visual Studio 上で鬼斬を実行する
    • "EmulationWithDebug" mode に正しく指定できていれば、以下の内容のウインドウが開く
    Onikiri Version 2.9000
    Emulator ... initialized.
    Resources ... initialized.
    Waiting for host GDB access...
    
  2. 次に gdb を起動する
    • Visual Studio Code 上で、左メニューから "実行とデバッグ" → "デバッグの開始" をクリックする
    • 正しく設定できていれば、鬼斬を実行している Visual Studio のウインドウが、以下の表示になる
    Onikiri Version 2.9000
    Emulator ... initialized.
    Resources ... initialized.
    Waiting for host GDB access... connected.    
    
    • このとき、break point を設定していなければ、exit まで実行が進む
Clone this wiki locally