--- nono/debugger/debugger_private.h 2026/04/29 17:04:30 1.1.1.1 +++ nono/debugger/debugger_private.h 2026/04/29 17:04:37 1.1.1.3 @@ -1,14 +1,19 @@ // // nono -// Copyright (C) 2020 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // #pragma once -#include "header.h" +#include "branchhistory.h" #include "bus.h" #include "cvprompt.h" #include "textscreen.h" +#include + +using ConstStringPair = std::pair; +using HelpMessages = std::vector; // 諸情報付きのアドレス struct saddr_t @@ -31,17 +36,6 @@ struct saddr_t // addr がデータアクセスなら true。 bool data = false; - // コンストラクタ - saddr_t() {} - // コピーコンストラクタ - saddr_t(const saddr_t& lhs) { - addr = lhs.addr; - logical = lhs.logical; - search = lhs.search; - super = lhs.super; - data = lhs.super; - } - bool IsLogical() const { return logical; } bool IsSearch() const { return search; } bool IsSuper() const { return super; } @@ -68,47 +62,93 @@ class Console; class DebuggerMD; class Disasm; -class Debugger +class Debugger final { friend class DebuggerMD_m680x0; friend class DebuggerMD_m88xx0; private: // 型 - typedef void (Debugger::*cmdfunc_t)(); + enum CommandAction { + Stay, // デバッガプロンプトに留まる + Leave, // デバッガプロンプトを抜けて VM を実行する + Quit, // デバッガとの接続を終了する + }; + using cmdfunc_t = void (Debugger::*)(); typedef struct { const char *name; cmdfunc_t func; + CommandAction action; } cmddef_t; + // ブレークポイント種別 + enum BreakpointType { + Unused = 0, + Address, // このアドレスに来る直前 + Memory, // このアドレスをアクセスした直後 + Exception, // この例外を検出した直後 + Instruction, // この命令を実行する直前 + }; + // ブレークポイント typedef struct { - bool enable; - bool ismemory; - uint32 addr; - uint32 count; + BreakpointType type {}; // 種別 + // 種別ごとのパラメータ + union { + uint32 addr {}; // ターゲットアドレス (Address/Memory) + struct { + int vec1; // ベクタ番号 (開始) + int vec2; // ベクタ番号 (終了) + }; + struct { + uint32 inst; // 命令ワード + uint32 mask; // マスク + }; + }; + + uint32 matched {}; // 成立回数 (積算) + + // skip はユーザ指定値。ただし + // -1 なら常にスキップ、つまりブレークせずカウントのみ行う、 + // 0 ならスキップなし、つまり成立ごとにブレーク(これがデフォルト)、 + // n(>0) なら n 回の成立をスキップし、(n+1) 回目でブレークの意。 + int32 skip {}; // スキップ回数 (ユーザ指定値) + int32 skipremain {}; // 残りスキップ回数 (0 で成立) } breakpoint_t; static const int MAX_BREAKPOINTS = 8; public: Debugger(); - virtual ~Debugger() { } + ~Debugger() { } void Init(); void ThreadRun(); bool Check(); + void NotifyException(int vector); + + // ブレークポイントモニタを更新 + void MonitorBreakpoint(TextScreen& monitor); private: - bool MainLoop(); - cmddef_t *ParseCmd(); + bool Accept(); + bool AcquirePrompt(); + CommandAction MainLoop(); + bool CheckAllBreakpoints(); + bool CheckBreakpointInst(breakpoint_t&); + void ParseCmdbuf(); // コマンド名は大文字小文字を区別する関係でスネークスタイル。 void cmd_b(); + void cmd_bi(); + void cmd_bm(); + void cmd_bv(); void cmd_b_list(); + void cmd_b_set(BreakpointType); void cmd_brhist(); void cmd_bx(); void cmd_c(); void cmd_d(); void cmd_dt(); void cmd_D(); + void cmd_exhist(); void cmd_h(); void cmd_L(); void cmd_m(); @@ -117,29 +157,29 @@ class Debugger void cmd_minus(); void cmd_n(); void cmd_q(); - void cmd_r(); - void cmd_ra(); - void cmd_rc(); - void cmd_rf(); - void cmd_rm(); - void cmd_ro(); void cmd_s(); void cmd_so(); void cmd_show(); void cmd_t(); - void cmd_unknown(); - int AddBreakpoint(uint32 addr, bool ismemory); - void Continue(); + int AddBreakpoint(const breakpoint_t&); + void RecalcInstMask(); bool GetAddr(uint32& addr, uint32& lastaddr); void cmd_d_common(MemdumpMode mode); + void cmd_hist_common(BranchHistory& hist, uint64 flag); void cmd_m_common(MemdumpMode mode); + bool ParseVerbHex(const char *arg, uint32 *valp); bool ParseAddr(const char *arg, uint32_t *addrp); bool SetNBreakpoint(); - void HelpMain(); + static const HelpMessages HelpMsgMain; + void Help(const HelpMessages msgs); + + // モニターを更新して表示 + void ShowMonitor(IMonitor& monitor); + // テキストスクリーンを表示 + void ShowTextScreen(TextScreen& screen); - void ShowMonitor(TextScreen& monitor); // アドレスをダンプ表示用に整形して返す bool FormatDumpAddr(saddr_t addr, std::string& addrstr); @@ -149,16 +189,18 @@ class Debugger const char *prompt; // プロンプト文字列 std::string cmdbuf {}; // 現在のコマンドライン std::string last_cmdbuf {}; // 直前のコマンドライン - int ac = 0; - std::string strav[10] { {}, }; - const char *av[10] {}; + std::vector args {}; uint32 pc = 0; uint32 nextpc = 0; std::vector ir {}; // この後実行する命令のバイナリ列 - breakpoint_t bpoint[MAX_BREAKPOINTS] {}; + std::array bpoint {}; bool bc_enable = false; uint32 bc_addr = 0; + uint32 bi_inst = 0; + int bi_inst_bytes = 0; + int bi_need_bytes = 0; + int bv_vector = -1; uint32 d_last_addr = 0; uint32 m_last_addr = 0; bool n_enable = false; @@ -174,8 +216,7 @@ class Debugger // 一度VMを実行して再びプロンプトに来たら true bool is_continued = false; - static cmddef_t cmdtable[]; - static cmddef_t cmdtable_unknown; + static std::vector cmdtable; }; // デバッガの CPU 依存部分 @@ -184,9 +225,6 @@ class DebuggerMD public: DebuggerMD(Debugger *arg) { parent = arg; - - // 奇数にしておくことで初回に必ずアドレス変換を起こさせる。 - last_lpage.addr = 0xffffffff; } virtual ~DebuggerMD() { } @@ -223,13 +261,15 @@ class DebuggerMD // 現在のレジスタセットを内部にバックアップする virtual void BackupRegs() = 0; - // レジスタの一覧を cons に出力 - virtual void ShowRegMain(Console *cons) = 0; - virtual void ShowRegCtrl(Console *cons) = 0; - virtual void ShowRegFPU(Console *cons) = 0; - virtual void ShowRegMMU(Console *cons) = 0; - virtual void ShowRegATC(Console *cons) = 0; - virtual void ShowRegOther(Console *cons) = 0; + // レジスタ表示系のコマンドを実行。処理すれば true を返す。 + virtual bool ShowRegister(Console *cons, + const std::vector& args) = 0; + // レジスタ表示系コマンドのヘルプメッセージを取得 + virtual const HelpMessages& GetRegisterHelp() const = 0; + + // ブランチ履歴・例外履歴(オブジェクト)を取得 + virtual BranchHistory& GetBrHist() const = 0; + virtual BranchHistory& GetExHist() const = 0; // 直近の命令でアクセスした laddr が一致したら true を返す // 1 命令で複数にアクセスする場合を考慮して、md 側で判断する @@ -253,12 +293,13 @@ class DebuggerMD virtual std::string FormatDisasmLive(const std::string& mnemonic, const std::vector& bin) = 0; + // ベクタ番号の最大値 (+1) + int vector_max = 0; + // 1命令の最小ビット数 + uint inst_bits = 0; + protected: Debugger *parent; // 親クラス - - // 前回アクセス時の論理ページ。.addr == 0xffffffff なら無効。 - saddr_t last_lpage; - uint32 last_ppage; }; #define NORM "\x1b[0m"