--- nono/debugger/debugger.cpp 2026/04/29 17:04:40 1.1.1.5 +++ nono/debugger/debugger.cpp 2026/04/29 17:05:20 1.1.1.14 @@ -4,131 +4,286 @@ // Licensed under nono-license.txt // -#include "console.h" -#include "debugger_private.h" +// +// デバッガ +// + +// +// VM スレッド デバッガスレッド HostCOM +// condvar +// | | |<--- 入力 +// | |<----------------------| +// | | RxCallback +// | | +// | |---------------------->| +// | | HostCOMDevice::Tx() |---> 出力 + +// |<---------------------------| +// | is_prompt = true; | デバッガスレッドからプロンプトを出したい +// | Message(MPU_TRACE_ALL); | 場合は MPU (VM) をトレースモードにする。 +// | | その際 is_prompt を立てておくことで止まる。 +// | | +// |--------------------------->| +// | condvar REQUEST_PROMPT | VM スレッドからプロンプトを出したい場合 +// | | (上述の例も含む) は条件変数で通知。 +// | | +// |<---------------------------| +// | condvar prompt_released | プロンプトを出している間 VM スレッドは +// | | 条件変数で待機しているので、これを起こす +// | | ことで実行再開。 + +#include "debugger.h" +#include "debugger_hd64180.h" #include "debugger_m680x0.h" #include "debugger_m88xx0.h" +#include "hostcom.h" #include "mainapp.h" +#include "memdump.h" #include "mystring.h" -#include "mythread.h" +#include "power.h" +#include "scheduler.h" +#include "syncer.h" +#include "uimessage.h" +#include "vectortable.h" #include -#include - -static Debugger *gDebugger; -BreakpointMonitor *gBreakpointMonitor; -CVPrompt *gCVPrompt; +#include +#include +#if defined(HAVE_BSD_STDIO_H) +#include +#endif -static void *debugger_run(void *); +static int readfunc(void *, char *, int); +static int writefunc(void *, const char *, int); -// 初期化。 -// この時点で VM が初期化されていること。 -void -debugger_init() +// コンストラクタ +Debugger::Debugger() + : inherited(OBJ_DEBUGGER) { - gDebugger = new Debugger(); - gDebugger->Init(); + // ベクタテーブル + pVectorTable.reset(new VectorTable(gMainApp.GetVMType())); - gBreakpointMonitor = new BreakpointMonitor(); - gCVPrompt = new CVPrompt(); + // ブレークポイントモニタ + bpoint_monitor.func = ToMonitorCallback(&Debugger::MonitorUpdateBpoint); + bpoint_monitor.SetSize(60, 9); + bpoint_monitor.Regist(ID_MONITOR_BREAKPOINT); + + // メモリダンプモニタ + for (int i = 0, end = memdump_monitor.size(); i < end; i++) { + int objid = OBJ_MPU_MEMDUMP(i); + int monid = ID_MONITOR_MEMDUMP(i); + memdump_monitor[i].reset(new MemdumpMonitor(objid, monid)); + } - // デバッガスレッド起動 - pthread_t th; - pthread_create(&th, NULL, debugger_run, NULL); + if (gMainApp.Has(VMCap::LUNA)) { + // XP 空間のメモリダンプモニタ + for (int i = 0, end = xpmemdump_monitor.size(); i < end; i++) { + int objid = OBJ_XP_MEMDUMP(i); + int monid = ID_MONITOR_XPMEMDUMP(i); + xpmemdump_monitor[i].reset(new MemdumpMonitor(objid, monid)); + } + } } -// デバッガスレッドのエントリポイント -static void * -debugger_run(void *dummy) +// デストラクタ +Debugger::~Debugger() { - PTHREAD_SETNAME("Debugger"); - pthread_detach(pthread_self()); + // fclose(fflush) にあたり hostcom へのアクセスが発生するので、 + // hostcom より先に片付けておかなければならない。 + Close(); - gDebugger->ThreadRun(); - return NULL; + if ((bool)hostcom) { + hostcom->SetRxCallback(NULL); + hostcom->SetAcceptCallback(NULL); + } + + TerminateThread(); } -// コンストラクタ -Debugger::Debugger() +bool +Debugger::Create() { - if (gMPU680x0) { - md = new DebuggerMD_m680x0(this, gMPU680x0->GetCPU()); - } else if (gMPU88xx0) { - md = new DebuggerMD_m88xx0(this, gMPU88xx0->GetCPU()); - } else { - throw "unknown mpu"; + // ホストドライバを作成 + hostcom.reset(new HostCOMDevice(this, "Debugger")); + if ((bool)hostcom == false) { + return false; } + + hostcom->SetRxCallback(ToDeviceCallback(&Debugger::RxCallback)); + hostcom->SetAcceptCallback(ToDeviceCallback(&Debugger::AcceptCallback)); + + return true; } -// コマンドライン引数によって動作を決めるところ。 -// 名前がこれでいいのかはあるけど。 +// ログレベル設定 void +Debugger::SetLogLevel(int loglevel_) +{ + inherited::SetLogLevel(loglevel_); + + // ホストドライバを従属させる + if ((bool)hostcom) { + hostcom->SetLogLevel(loglevel_); + } +} + +// 初期化 +bool Debugger::Init() { + if (inherited::Init() == false) { + return false; + } + + syncer = GetSyncer(); + + if (gMainApp.Has(VMCap::M88K)) { + md_mpu.reset(new DebuggerMD_m88xx0(this)); + } else { + md_mpu.reset(new DebuggerMD_m680x0(this)); + } + if (gMainApp.Has(VMCap::LUNA)) { + md_xp.reset(new DebuggerMD_hd64180(this)); + } + // とりあえずメインプロセッサに固定 + curmd = md_mpu.get(); + + // md_* が用意できたので MemdumpMonitor にセットする。 + // これらは Monitor なので Init() より前に用意してなければならないが、 + // 一方で md は諸々が落ち着いた後のここでないと用意できない。うーん。 + // ここはバス別のモニタなので curmd ではなく md_{mpu,xp} を参照する。 + for (int i = 0, end = memdump_monitor.size(); i < end; i++) { + auto *mem = memdump_monitor[i].get(); + mem->InitMD(md_mpu.get()); + } + if (gMainApp.Has(VMCap::LUNA)) { + for (int i = 0, end = xpmemdump_monitor.size(); i < end; i++) { + auto *mem = xpmemdump_monitor[i].get(); + mem->InitMD(md_xp.get()); + } + } + // -d なら CPU 起動時点で停止してプロンプトを待つ if (gMainApp.debug_on_start) { - md->ReqSet(CPU_REQ_PROMPT); + is_pause = true; + // この時点ではまだ MPU にメッセージを送ることはできない } - // -b ならブレークポイント設定 + // -b [,][,] ならブレークポイント設定 for (auto& str : gMainApp.debug_breakaddr) { breakpoint_t bp; - - md->ReqSet(CPU_REQ_TRACE); - - // , で分離できたら を取り出す - int pos = str.find(','); - if (pos != std::string::npos) { - std::string skipstr = str.substr(pos + 1); - bp.skip = atoi(skipstr.c_str()); - - // XXX これ出来るんだっけ? - str[pos] = '\0'; + std::string cpustr; + std::string addrstr; + std::string skipstr; + + // "," で分離 + auto arr = string_split(str, ','); + if (arr.size() < 2) { + // 1個ならアドレス (0 ってことはないはずだが) + addrstr = arr[0]; + } else if (arr.size() == 2) { + // 2個なら cpu か skip のどちらかが省略。 + // 1つ目が16進数っぽいかどうかで判定する。 + char *end; + strtoul(arr[0].c_str(), &end, 16); + if (end == &arr[0][0]) { + cpustr = arr[0]; + addrstr = arr[1]; + } else { + addrstr = arr[0]; + skipstr = arr[1]; + } + } else if (arr.size() == 3) { + cpustr = arr[0]; + addrstr = arr[1]; + skipstr = arr[2]; + } else { + warnx("\"%s\": Invalid breakpoint", str.c_str()); + return false; } - // そしてどちらにしても を取り出す - if (!ParseAddr(str.c_str(), &bp.addr)) { + + // を取り出す + if (!ParseAddr(addrstr.c_str(), &bp.addr)) { warnx("\"%s\": Invalid breakpoint address", str.c_str()); - return; + return false; + } + // を取り出す + if (skipstr.empty() == false) { + bp.skip = atoi(skipstr.c_str()); } // 登録 bp.type = BreakpointType::Address; - AddBreakpoint(bp); + AddBreakpoint(bp, cpustr); } - // どこでやるべか - signal(SIGPIPE, SIG_IGN); + // 入出力を FILE で扱う + cons = funopen(this, readfunc, writefunc, NULL, NULL); + if (cons == NULL) { + warnx("funopen2 failed"); + return false; + } + + return true; } // デバッガスレッド void Debugger::ThreadRun() { - // XXX どこかでコンソールの選択とパラメータの取得 - if (gMainApp.debug_on_console) { - cons = new ConsoleStdio(); - } else { - cons = new ConsoleTCP(); - } - if (cons->Init() == false) { - delete cons; - return; - } - // disp_regs の初期値設定。 // 再接続でも継続してていいような気がするのでループ外で初期化。 disp_regs.clear(); disp_regs.push_back("r"); + // MPU のトレース状態の初期化は vm/mpu* 側のリセット例外で行っている。 + for (;;) { - // 着信待ち - if (Accept() == false) { + // 何か起きるまで待つ + uint32 req; + { + std::unique_lock lock(mtx); + cv_request.wait(lock, [&] { return request != 0; }); + req = request; + request = 0; + } + + if ((req & REQUEST_EXIT)) { break; } - cons->InitEditLine("> "); + if ((req & REQUEST_RXCHAR)) { + // コンソールからの文字入力 + int c; + while ((c = hostcom->Rx()) >= 0) { + Input(c); + } + } + + if ((req & REQUEST_ACCEPT)) { + // TCP 待ち受けに着信があればすぐにプロンプトを出したい + Input('\n'); + } + + if ((req & REQUEST_PROMPT)) { + // VM 停止(したのでプロンプトモードへ) + EnterPrompt(); + } + } + + LeavePrompt(false); + Close(); +} - // デバッガプロンプトにいる間はトレースオン - md->ReqSet(CPU_REQ_TRACE); +// コンソールをクローズする +void +Debugger::Close() +{ + if (cons) { + fclose(cons); + cons = NULL; + } +} +#if 0 bool first = true; for (;;) { // 接続後の1回目だけ実行するもの。 @@ -138,331 +293,511 @@ Debugger::ThreadRun() // greeting はプロンプトが取れる前にもう表示したい。 // 何らかの事故でプロンプトが取れなくても、ここまでは接続 // できてることが分かるように。 - cons->Print("This is debugger console\n"); + fprintf(cons, "This is debugger console\n"); // 接続ごとに初期化する値 - // XXX もうちょっときれいにしたい - d_last_addr = 0xffffffff; - m_last_addr = 0xffffffff; n_enable = false; s_enable = false; t_enable = true; } + } + } - // プロンプトが取れるのを待つ - if (AcquirePrompt() == false) { - break; - } +#endif - // プロンプトに来た時に表示するいつものやつ - pc = md->GetPC(); - d_last_addr = pc; - cmd_minus(); +// スレッドに終了指示 +void +Debugger::Terminate() +{ + std::unique_lock lock(mtx); + request |= REQUEST_EXIT; + cv_request.notify_one(); +} - // コマンド処理のメインループ - auto action = MainLoop(); +// funopen の read コールバック +static int +readfunc(void *cookie, char *buf, int bufsize) +{ + return ((Debugger *)cookie)->ReadFunc(buf, bufsize); +} - // 次回との差分のため、今のレジスタセットをバックアップ - md->BackupRegs(); +// funopen の write コールバック +static int +writefunc(void *cookie, const char *buf, int len) +{ + return ((Debugger *)cookie)->WriteFunc(buf, len); +} - // メインループから戻ったということは Leave か Quit なので - // どちらにしても、ここでプロンプトを手放す。 - gCVPrompt->NotifyRelease(); +// funopen の read コールバックの本体 +int +Debugger::ReadFunc(char *buf, int bufsize) +{ + char *d = buf; + char *end = buf + bufsize; - // quit ならここでループを1段抜ける - if (action == CommandAction::Quit) { - break; - } + for (; d < end; ) { + if ((bool)hostcom == false) { + errno = EIO; + return -1; } - // デバッガを抜けるのでトレースオフ - md->ReqClr(CPU_REQ_TRACE); - - cons->Close(); - if (gMainApp.debug_on_console) { + int c = hostcom->Rx(); + if (c < 0) { break; } + *d++ = c; } - - delete cons; + return (d - buf); } -// コンソールに接続されるのを待つ。 -// うーん、なんだこれ。 -// -// +--- debug_on_console (-D オプション) -// | +- debug_on_start (-d オプション) -// | | 動作 -// 0 0 TCP、Accept で待機。 -// 0 1 TCP、Accpet で待機。 -// 1 0 stdio、起動時にプロンプトで止まらない -> キー入力待ち。 -// 1 1 stdio、起動時にプロンプトで止まる。 -bool -Debugger::Accept() +// funopen の write コールバックの本体 +int +Debugger::WriteFunc(const char *buf, int len) { - if (gMainApp.debug_on_console == false) { - // TCP なら accept が成功するのを待つ - cons->Accept(); - } else { - if (gMainApp.debug_on_start == false) { - // stdio、起動時にプロンプトで止まらない。 - // この場合キー入力かブレークポイントなどで止まった時点で - // プロンプトを出したいのでちょっと面倒なことに。 - for (;;) { - bool is_prompt = gCVPrompt->WaitAcquire(200); - if (is_prompt) { - break; - } + const char *s = buf; + const char *end = buf + len; - int r = cons->Poll(200); - if (r < 0) { - cmd_q(); - return false; - } - if (r > 0) { - break; - } + for (; s < end; ) { + if ((bool)hostcom == false) { + errno = EIO; + return -1; + } + + // ホストスレッドのキューが満杯なら空くまで待つ。うーん…。 + // XXX 無期限で大丈夫だろうか + int c = *s++; + if (c == '\n') { + // ここで LF を CRLF にする? + // (TELNET に出力するのに必要) + while (hostcom->Tx('\r') == false) { + usleep(10); } - } else { - // stdio、起動時にプロンプトで止まる。 - // この場合は無条件に成功でよい。 + } + while (hostcom->Tx(c) == false) { + usleep(10); } } + return (s - buf); +} - return true; +// ホストからの1文字受信通知 (HostCOM スレッドから呼ばれる) +void +Debugger::RxCallback() +{ + // デバッガスレッドに通知 + std::unique_lock lock(mtx); + request |= REQUEST_RXCHAR; + cv_request.notify_one(); } -// プロンプトが取れるのを待つ -// 失敗すれば false を返す -bool -Debugger::AcquirePrompt() +// ホストからの着信通知 (HostCOM スレッドから呼ばれる) +void +Debugger::AcceptCallback() { - md->ReqSet(CPU_REQ_PROMPT); + // デバッガスレッドに通知 + std::unique_lock lock(mtx); + request |= REQUEST_ACCEPT; + cv_request.notify_one(); +} - for (;;) { - bool is_prompt = gCVPrompt->WaitAcquire(200); - if (is_prompt) - break; +// ホストからの1文字入力 +void +Debugger::Input(int c) +{ + // '^@' は捨てる + // (意図的にも入力できるが nc が TELNET オプション扱えなくて送ってくる) + if (c == '\0') { + return; + } - // コンソールを定期観測して、入力があればブレーク要求 - if (cons->Poll()) { - // この入力は drop してみる - if (cons->Gets(cmdbuf) == false) { - // EOF or Error - cmd_q(); - return false; + // HostCOM からは改行で CR が来るようなので LF にしておく + if (c == '\r') { + c = '\n'; + } + + if (is_prompt == false) { + // プロンプトでない時は、Enter か ^C でプロンプトを出す + if (c == '\n' || c == '\x03') { + // MPU に一時停止を要求 + is_pause = true; + scheduler->SendMessage(MessageID::MPU_TRACE_ALL, true); + } + } else { + // プロンプト中なら行入力 + if (c == '\b') { + if (cmdbuf.empty() == false) { + // 簡易バックスペースを出力。 + // XXX 実際どうするんだこれ + fputc('\b', cons); + fputc(' ', cons); + fputc('\b', cons); + fflush(cons); + + cmdbuf.pop_back(); + } + } else if (c == '\n') { + // Enter ならここでコマンド実行 + fputc(c, cons); + fflush(cons); + + auto act = Command(); + + // 次回との差分のため、今のレジスタセットをバックアップ + curmd->BackupRegs(); + + switch (act) { + case CmdAct::Stay: + // プロンプトに留まるならここで、次行のプロンプト? + PrintPrompt(); + break; + case CmdAct::Leave: + // プロンプトを抜ける + LeavePrompt(IsTrace()); + break; + case CmdAct::Quit: + // アプリケーション自体を終了 + LeavePrompt(false); + UIMessage::Post(UIMessage::APPEXIT); } - md->ReqSet(CPU_REQ_PROMPT); + } else if (c < ' ' || c == 0x07f) { + // 他のコントロールコードはとりあえず無視 + } else { + // 通常文字なら、エコーバックして追加 + fputc(c, cons); + fflush(cons); + + cmdbuf.push_back(c); } } - return true; } -// コマンド処理のメインループ。 -// コマンドが Stay ならこの中で処理を継続。 -// コマンドが Leave か Quit ならその値を持って戻る (呼び出し側で処理する)。 -Debugger::CommandAction -Debugger::MainLoop() +// コマンドモード(プロンプト)に入る +void +Debugger::EnterPrompt() { - for (;;) { - cons->Prompt(); - if (cons->Gets(cmdbuf) == false) { - // EOF or Error - return CommandAction::Quit; - } + is_prompt = true; - string_rtrim(cmdbuf); + // ブレークポイント到達メッセージがあればここで表示 + if (bpointmsg.empty() == false) { + fprintf(cons, "%s", bpointmsg.c_str()); + bpointmsg.clear(); + } - if (!cmdbuf.empty()) { - // コマンドが入力されれば次回のために保存 - last_cmdbuf = cmdbuf; - } else { - // 空行が入力されれば直前のコマンドをもう一度 - // 前行がなければ何もせずもう一度プロンプトを表示するかね - if (last_cmdbuf.empty()) - continue; - cmdbuf = last_cmdbuf; + // d/m をいきなり引数なしで実行した時のため、現在地にしておく。 + ResetStickyAddr(); + + // プロンプトのたびに表示するやつ + cmd_minus(); + + PrintPrompt(); +} + +// コマンドモード(プロンプト)から出る。 +// trace はプロンプトから抜ける際のトレース状態の指示。 +// スレッド終了時は false を指定すること。 +void +Debugger::LeavePrompt(bool trace) +{ + // MPU のトレース状態を変更 + scheduler->SendMessage(MessageID::MPU_TRACE_ALL, trace); + + is_prompt = false; + + // 最後に VM スレッドで待機している Exec() を起こす + { + std::unique_lock lock(mtx); + prompt_released = true; + cv_prompt.notify_one(); + } +} + +// プロンプトを表示 +void +Debugger::PrintPrompt() +{ + fprintf(cons, "%s> ", curmd->GetName().c_str()); + fflush(cons); +} + +// コマンド実行 +Debugger::CmdAct +Debugger::Command() +{ + string_rtrim(cmdbuf); + + if (cmdbuf.empty()) { + // 空行なら、直前のコマンドをもう一度。 + // 前行がなければ何もせずもう一度プロンプトを表示するかね。 + if (last_cmdbuf.empty()) { + return CmdAct::Stay; } + cmdbuf = last_cmdbuf; + } else { + // 入力された行を次回のために保存 + last_cmdbuf = cmdbuf; + } - // 行を args に分解 - ParseCmdbuf(); + // 行を args に分解 + ParseCmdbuf(); + cmdbuf.clear(); - // 前回の値が有効なのはコマンドが連続した時だけ + // 前回の値が有効なのはコマンドが連続した時だけ - // コマンドをテーブルから探す - auto it = std::find_if(cmdtable.begin(), cmdtable.end(), - [=](auto x) { return strcmp(args[0].c_str(), x.name) == 0; }); - if (it != cmdtable.end()) { - auto cmd = *it; + // コマンドをテーブルから探す + auto it = std::find_if(cmdtable.begin(), cmdtable.end(), + [=](auto x) { return strcmp(args[0].c_str(), x.name) == 0; }); + if (it != cmdtable.end()) { + auto cmd = *it; - // 見付かれば実行 - (this->*(cmd.func))(); + // 見付かれば実行 + return (this->*(cmd.func))(); + } - // Stay なら引き続きコマンド処理 - if (cmd.action == CommandAction::Stay) { - continue; - } - // それ以外ならメインループ終了 - return cmd.action; + // (コマンドテーブルになくて) "r" から始まっていればレジスタ表示系 + if (args[0][0] == 'r') { + // ShowRegister() は処理したら true を返す。 + // "r" 系コマンドはすべて Stay。 + if (curmd->ShowRegister(cons, args)) { + return CmdAct::Stay; } + } - // (コマンドテーブルになくて) "r" から始まっていればレジスタ表示系 - if (args[0][0] == 'r') { - // ShowRegister() は処理したら true を返す。 - // "r" 系コマンドはすべて Stay。 - if (md->ShowRegister(cons, args)) { - continue; - } + // 知らないコマンドも Stay 相当。 + fprintf(cons, "%s: unknown command\n", args[0].c_str()); + // この行は次回空エンターで再発行しないでいい。 + last_cmdbuf.clear(); + return CmdAct::Stay; +} + +// ブレークポイントとかを調べる。1命令ごとに呼び出される。 +// (VM スレッドから呼ばれる) +void +Debugger::Exec(DebuggerMD *md) +{ + if (Check(md)) { + // この CPU でブレークしたので、ターゲット CPU をこっちに変更。 + ChangeMD(md); + + // 実時間を停止 + syncer->StopRealTime(); + + // 待機 + { + std::unique_lock lock(mtx); + + // notify 前にクリア + prompt_released = false; + + // MPU が一時停止したことをデバッガスレッドへ通知する + request |= REQUEST_PROMPT; + cv_request.notify_one(); + + // デバッガスレッドがプロンプト解放するまで待機 + cv_prompt.wait(lock, [&] { return prompt_released; }); } - // 知らないコマンドも Stay 相当。 - cons->Print("%s: unknown command\n", args[0].c_str()); - continue; + // 実時間を再開 + syncer->StartRealTime(); + } else { + // t (トレース表示) が有効な場合は終了条件にマッチしなかったここで + // レジスタを表示。終了条件にマッチした時は EnterPrompt() で表示する。 + if (t_enable == md) { + assert(md == curmd); + pc = curmd->GetPC(); + cmd_minus(); + // 次回との差分のため今のレジスタセットをバックアップ + curmd->BackupRegs(); + } } - __unreachable(); } -// ブレークポイントとかを調べる。 -// CPU_REQ_TRACE フラグが立っている間1命令ごとに呼び出される。 +// MPU をトレース状態にすべきなら true を返す。 bool -debugger_check() +Debugger::IsTrace() const { - return gDebugger->Check(); + // 有効なブレークポイントがあれば MPU をトレースにする + for (const auto& bp : bpoint) { + if (bp.type != BreakpointType::Unused) { + return true; + } + } + + // この辺のどれかがあれば MPU をトレースにする + return is_pause || (step_type != StepType::None); } -// デバッガ実行中なら命令開始前にメインルーチンから呼ばれる。 +// デバッガ実行中なら命令開始前に VM スレッドから呼ばれる。 // プロンプトに降りるなら true を返す。 bool -Debugger::Check() +Debugger::Check(DebuggerMD *md) { bool is_break = false; // ブレークポイントは他のチェックとは併用になるので先に調べる。 - if (CheckAllBreakpoints()) { + if (CheckAllBreakpoints(md)) { is_break = true; - } - // アドレス指定付き continue もブレークポイントと似た動作なのでこっち。 - // XXX ブレークポイントとして実装するかどうか - if (bc_enable) { - if (bc_addr == md->GetPC()) { - bc_enable = false; - is_break = true; - } - } - // XXX 残りは排他動作のはず + // この次に調べる各種終了条件が来ないうちに先にブレークポイントに + // 到達した場合でも、ブレークポイントによりプロンプトに降りる + // わけなので、実行中のステップ実行をキャンセルする。 + // この場合もブレークポイント側が true なので true を返せばよい。 + step_type = StepType::None; + step_md = NULL; + } + + // ステップ実行系は、ターゲット CPU 側でだけ判定を行い、 + // いずれの場合も終了条件にマッチしたらブレークポイントの成否に関わらず + // true を返せばいい。 + // なお、トレース表示に関してはここではなく Exec() 側でやってある。 + + if (step_md == md) { + bool hit = false; + switch (step_type) { + case StepType::None: + break; + + case StepType::Count: // 命令数指定 + step_count--; + putlog(1, "Check s: --step_count=%d", step_count); + if (step_count == 0) { + hit = true; + } + break; - // いずれの場合も、ステップ実行が終了条件にマッチしたら、ブレークポイント - // の成否に関わらず true を返せばいい。 - // 終了条件が来ないうちに先にブレークポイントに到達した場合でも、ブレーク - // ポイントによりプロンプトに降りるわけなので、実行中のステップ実行を - // キャンセルする。この場合もブレークポイント側が true なので true を - // 返せばよい。 - - // t (トレース表示) が enable なら、終了条件にマッチしなくてもここで - // レジスタを表示。終了条件にマッチする場合に表示するとプロンプトで - // もう一回表示されて二重になってしまうので注意。 - - if (s_enable) { // ステップ実行が.. - s_count--; - if (s_count == 0) { // 成立 - s_enable = false; - return true; - } else if (is_break) { // 非成立だがブレークが成立 - s_enable = false; - return true; - } else if (t_enable) { // どちらも非成立で trace on - cmd_minus(); - } + case StepType::CountSkipSub: // 命令数指定 (サブルーチンをスキップ) + if ((int64)step_addr >= 0) { + // アドレスが指定されていれば、ステップインをスキップ中 + if (step_addr == step_md->GetPC()) { + step_count--; + step_addr = (uint64)-1; + } + } else { + step_count--; + } + if (loglevel >= 1) { + if ((int64)step_addr < 0) { + putlogn("Check n: --step_count=%d", step_count); + } else { + putlogn("Check n: step_addr=$%08x", (uint32)step_addr); + } + } + if (step_count == 0 || is_break/*?*/) { + hit = true; + } else { + // スキップ中でなければ、ステップインが起きるか都度調べる。 + // すでにスキップ中なら到達するまでは何もしない。 + if ((int64)step_addr < 0) { + SetNBreakpoint(); + } + } + break; - } else if (so_enable) { // ステップアウトが.. - if (md->IsStepOut()) { // 成立 - so_enable = false; - return true; - } else if (is_break) { // 非成立だがブレークが成立 - so_enable = false; - return true; - } else if (t_enable) { // どちらも非成立で trace on - cmd_minus(); - } + case StepType::StepOut: // ステップアウト + putlog(1, "Check so"); + if (step_md->IsStepOut()) { + hit = true; + } + break; - } else if (n_enable) { // 次命令まで実行が.. - if (n_breakaddr != 0xffffffff) { - // ステップインをスキップ中 - if (n_breakaddr == md->GetPC()) { - n_count--; + case StepType::Addr: // アドレス指定 + putlog(1, "Check c: step_addr=$%08x", (uint32)step_addr); + if (step_addr == step_md->GetPC()) { + hit = true; } - } else { - // 1命令実行 - n_count--; - } - if (n_count == 0) { // 成立 - n_enable = false; - return true; - } else if (is_break) { // 非成立だがブレークが成立 - n_enable = false; - return true; - } - if (t_enable) { // どちらも非成立で trace on - cmd_minus(); + break; + + case StepType::Time: // 時間指定 + putlog(1, "Check ct"); + if (scheduler->GetVirtTime() >= step_time) { + hit = true; + + // 時間到達は他のと比べて分かりづらいので、 + // ブレークポイントメッセージに便乗して表示(?) + bpointmsg += string_format("%s has passed.\n", + TimeToStr(ct_timespan).c_str()); + } + break; + + default: + PANIC("StepType %d not supported\n", (int)step_type); } - // スキップ中でなければ、ステップインが起きるか都度調べる。 - // すでにスキップ中なら到達するまでは何もしない。 - if (n_breakaddr == 0xffffffff) { - SetNBreakpoint(); + if (hit) { + is_break = true; + step_type = StepType::None; + step_md = NULL; + t_enable = NULL; } } - // ステップ実行系がなければブレークポイントの成否だけ + // デバッガから MPU の一時停止が要求されているか + if (is_pause) { + is_pause = false; + is_break = true; + } + return is_break; } -// ブレークポイントがどれかでも成立するかを調べる。 -// 1つ以上成立してブレークするなら true を返す。 +// md で指定された CPU のブレークポイントがどれかでも成立するかを調べる。 +// 1つ以上成立してブレークするなら、true を返す。 // 1つも成立しておらずブレークしないなら false を返す。 -// 成立すれば内容をここで表示する。 +// このルーチンから cons への出力は使わないこと。(プロンプトにいない時でも +// 呼ばれるので) bool -Debugger::CheckAllBreakpoints() +Debugger::CheckAllBreakpoints(DebuggerMD *md) { bool is_break = false; + int vector; // 命令ごとにクリアする bi_inst = 0; bi_inst_bytes = 0; + // 例外はここでローカルにコピーしてから、クリアする。 + // 厳密には atomic exchange すべきのような。 + vector = md->bv_vector; + md->bv_vector = -1; + // 1つの条件でマッチしても(そこでブレークすること自体は確定するのだが) // 残りの他の条件も成立すればカウントを進める必要があるため、 // 全部処理した上でどれか一つでもブレークしたかで判断する必要がある。 for (int i = 0, end = bpoint.size(); i < end; i++) { auto& bp = bpoint[i]; + if (bp.md != md) { + continue; + } + switch (bp.type) { case BreakpointType::Address: - if (bp.addr != md->GetPC()) { - continue; + // 通常動作中でアドレスが一致すればマッチ + if (bp.addr == bp.md->GetPC() && + bp.md->GetCPUState() == CPUState::Normal) + { + break; } - break; + continue; case BreakpointType::Memory: - if (!md->CheckLEA(bp.addr)) { - continue; + if (bp.md->CheckLEA(bp.addr)) { + break; } - break; + continue; case BreakpointType::Exception: - if (bv_vector >= 0) { - if (bp.vec1 <= bv_vector && bv_vector <= bp.vec2) { + if (vector >= 0) { + if (bp.vec1 <= vector && vector <= bp.vec2) { break; } } continue; case BreakpointType::Instruction: - if (CheckBreakpointInst(bp) == false) { - continue; + if (CheckBreakpointInst(bp)) { + break; } - break; + continue; default: continue; @@ -485,24 +820,31 @@ Debugger::CheckAllBreakpoints() is_break = true; std::string desc; + desc = string_format("cpu=%s ", bp.md->GetName().c_str()); switch (bp.type) { case BreakpointType::Address: - desc = string_format("addr $%08x", bp.addr); + desc += string_format("addr=$%08x", bp.addr); break; case BreakpointType::Memory: - desc = string_format("mem $%08x", bp.addr); + desc += string_format("mem=$%08x", bp.addr); break; case BreakpointType::Exception: { - desc = string_format("excp $%02x", bv_vector); - const char *name = md->GetExceptionName(bv_vector); - if (name != NULL && name[0] != '\0') { + desc += string_format("excp=$%02x", vector); + const char *name; + if (bp.md->arch == CPUArch::HD64180) { + name = MPU64180Device::InterruptName[vector]; + } else { + auto vectortable = pVectorTable.get(); + name = vectortable->GetExceptionName(vector); + } + if (name) { desc += string_format(" \"%s\"", name); } break; } case BreakpointType::Instruction: - desc = string_format("inst %0*x", + desc += string_format("inst=%0*x", bi_inst_bytes * 2, bi_inst >> ((4 - bi_inst_bytes) * 8)); break; @@ -510,11 +852,14 @@ Debugger::CheckAllBreakpoints() assert(false); break; } - cons->Print("breakpoint #%d (%s) reached\n", i, desc.c_str()); - } - // 例外通知は通過ごとに常に下ろしておく - bv_vector = -1; + // 到達メッセージを作成。 + // この時点ではまだコンソールを取得していない可能性があるので + // (-D なしで起動した場合とか)、表示せず用意するだけ。 + // コンソールが取得できたところで表示する。 + bpointmsg += string_format("breakpoint #%d (%s) reached\n", + i, desc.c_str()); + } // ブレークポイントが1つでも成立したかどうかを返す return is_break; @@ -524,28 +869,24 @@ Debugger::CheckAllBreakpoints() bool Debugger::CheckBreakpointInst(breakpoint_t& bp) { - saddr_t laddr; - // uint32 bi_inst が現在の PC 位置の命令データ (左詰め) // bi_inst_bytes が読み込んだバイト数(bi_inst の左からの有効バイト数)。 // bi_need_bytes が現在のブレークポイントで読み込む必要のあるバイト数。 - laddr.addr = md->GetPC(); - laddr.super = md->IsSuper(); - laddr.logical = true; + saddr_t laddr(bp.md->GetPC(), bp.md->IsSuper()); + DebuggerMemoryStream mem(bp.md, laddr, MMULookupMode::True); // 必要なバイト数に達するまで読み足す bi_inst = 0; while (bi_inst_bytes < bi_need_bytes) { - uint64 data = md->PeekFetch(laddr); + uint64 data = mem.Read(bp.md->inst_bytes); if ((int64)data < 0) { return false; } - bi_inst <<= md->inst_bytes * 8; + bi_inst <<= bp.md->inst_bytes * 8; bi_inst |= data; - bi_inst_bytes += md->inst_bytes; - laddr.addr += md->inst_bytes; + bi_inst_bytes += bp.md->inst_bytes; } // 左詰めにする @@ -558,118 +899,147 @@ Debugger::CheckBreakpointInst(breakpoint return false; } -// 例外通知 (MPU からの連絡用) +// 例外通知 (メイン CPU) void -debugger_notify_exception(int vector) +Debugger::NotifyExceptionMain(int vector) { - gDebugger->NotifyException(vector); + auto md = md_mpu.get(); + md->bv_vector = vector; } -// 例外通知 (本体) +// 例外通知 (XP)。 +// ベクタとして割り込み優先順位 (Intmap*) を使う。 void -Debugger::NotifyException(int vector) +Debugger::NotifyExceptionXP(int vector) { - bv_vector = vector; + auto md = md_xp.get(); + md->bv_vector = vector; } // コマンド一覧。 // "r" から始まるレジスタ表示系コマンドはコード中で別処理してある。 /*static*/ std::vector Debugger::cmdtable = { - { "bi", &Debugger::cmd_bi, Debugger::CommandAction::Stay }, - { "bm", &Debugger::cmd_bm, Debugger::CommandAction::Stay }, - { "bv", &Debugger::cmd_bv, Debugger::CommandAction::Stay }, - { "bx", &Debugger::cmd_bx, Debugger::CommandAction::Stay }, - { "b", &Debugger::cmd_b, Debugger::CommandAction::Stay }, - { "brhist", &Debugger::cmd_brhist, Debugger::CommandAction::Stay }, - { "c", &Debugger::cmd_c, Debugger::CommandAction::Leave }, - { "d", &Debugger::cmd_d, Debugger::CommandAction::Stay }, - { "dt", &Debugger::cmd_dt, Debugger::CommandAction::Stay }, - { "D", &Debugger::cmd_D, Debugger::CommandAction::Stay }, - { "disp", &Debugger::cmd_disp, Debugger::CommandAction::Stay }, - { "exhist", &Debugger::cmd_exhist, Debugger::CommandAction::Stay }, - { "hb", &Debugger::cmd_hb, Debugger::CommandAction::Stay }, - { "hr", &Debugger::cmd_hr, Debugger::CommandAction::Stay }, - { "h", &Debugger::cmd_h, Debugger::CommandAction::Stay }, - { "help", &Debugger::cmd_h, Debugger::CommandAction::Stay }, - { "L", &Debugger::cmd_L, Debugger::CommandAction::Stay }, - { "m", &Debugger::cmd_m, Debugger::CommandAction::Stay }, - { "mt", &Debugger::cmd_mt, Debugger::CommandAction::Stay }, - { "M", &Debugger::cmd_M, Debugger::CommandAction::Stay }, - { "n", &Debugger::cmd_n, Debugger::CommandAction::Leave }, - { "nt", &Debugger::cmd_nt, Debugger::CommandAction::Leave }, - { "q", &Debugger::cmd_q, Debugger::CommandAction::Quit }, - { "quit", &Debugger::cmd_q, Debugger::CommandAction::Quit }, - { "reset", &Debugger::cmd_reset, Debugger::CommandAction::Leave }, - { "s", &Debugger::cmd_s, Debugger::CommandAction::Leave }, - { "st", &Debugger::cmd_st, Debugger::CommandAction::Leave }, - { "so", &Debugger::cmd_so, Debugger::CommandAction::Leave }, - { "sot", &Debugger::cmd_sot, Debugger::CommandAction::Leave }, - { "show", &Debugger::cmd_show, Debugger::CommandAction::Stay }, - { "t", &Debugger::cmd_t, Debugger::CommandAction::Leave }, - { "-", &Debugger::cmd_minus, Debugger::CommandAction::Stay }, + { "bi", &Debugger::cmd_bi, }, + { "bm", &Debugger::cmd_bm, }, + { "bv", &Debugger::cmd_bv, }, + { "bx", &Debugger::cmd_bx, }, + { "b", &Debugger::cmd_b, }, + { "brhist", &Debugger::cmd_brhist, }, + { "c", &Debugger::cmd_c, }, + { "ct", &Debugger::cmd_ct, }, + { "d", &Debugger::cmd_d, }, + { "dt", &Debugger::cmd_dt, }, + { "D", &Debugger::cmd_D, }, + { "disp", &Debugger::cmd_disp, }, + { "exhist", &Debugger::cmd_exhist, }, + { "hb", &Debugger::cmd_hb, }, + { "hr", &Debugger::cmd_hr, }, + { "h", &Debugger::cmd_h, }, + { "help", &Debugger::cmd_h, }, + { "L", &Debugger::cmd_L, }, + { "m", &Debugger::cmd_m, }, + { "mt", &Debugger::cmd_mt, }, + { "M", &Debugger::cmd_M, }, + { "mpu", &Debugger::cmd_mpu, }, + { "n", &Debugger::cmd_n, }, + { "nt", &Debugger::cmd_nt, }, + { "q", &Debugger::cmd_q, }, + { "quit", &Debugger::cmd_q, }, + { "reset", &Debugger::cmd_reset, }, + { "s", &Debugger::cmd_s, }, + { "st", &Debugger::cmd_st, }, + { "so", &Debugger::cmd_so, }, + { "sot", &Debugger::cmd_sot, }, + { "show", &Debugger::cmd_show, }, + { "t", &Debugger::cmd_t, }, + { "xp", &Debugger::cmd_xp, }, + { "z", &Debugger::cmd_z, }, + { "-", &Debugger::cmd_minus, }, }; // ヘルプ // h : 一覧を表示 // h : 単独コマンドの詳細を表示 -void +Debugger::CmdAct Debugger::cmd_h() { if (args.size() < 2) { // 引数なしなら一覧表示。 - Help(HelpMsgMain); - return; + ShowHelpList(HelpListMain); + return CmdAct::Stay; } // 引数があれば個別の詳細 - bool try_again = false; - std::string cmd = args[1]; - do { - - for (const auto& dict : HelpDetails) { - if (cmd == dict.first) { - const auto& desc = dict.second; - if (desc[0] == '=') { - // "=" 形式なら を探しなおす。 - // 別名で同じヘルプを指すシンボリックリンクみたいなもの。 - cmd = desc.substr(1); - try_again = true; - break; - } - // そうでなければこれを表示して終了 - std::string disp = HelpConvert(dict.second); - cons->Print("%s", disp.c_str()); - return; - } - } - } while (try_again); - cons->Print("invalid command name: %s\n", args[1].c_str()); + const std::string cmd1 = args[1]; + + // curmd の MD 分も併せて検索するため、ローカルに map を作成する。 + // value のほうは実体コピーは不要なのでポインタ。 + std::map helpmap; + for (const auto& pair : HelpDetails) { + helpmap.insert(std::make_pair(pair.first, &pair.second)); + } + for (const auto& pair : curmd->GetHelpReg()) { + helpmap.insert(std::make_pair(pair.first, &pair.second)); + } + + // 検索 + const auto it1 = helpmap.find(cmd1); + if (it1 == helpmap.end()) { + fprintf(cons, "invalid command name: %s\n", cmd1.c_str()); + return CmdAct::Stay; + } + const auto& msg1 = *(it1->second); + + const std::string *msg; + if (msg1[0] != '=') { + // メッセージ本文が "=" から始まってなければこれを採用。 + msg = &msg1; + } else { + // メッセージ本文が "=" の形式なら、 を探し直す。 + // 別名で同じヘルプを指すシンボリックリンクみたいなもの。 + std::string cmd2 = msg1.substr(1); + + // 再検索 (こっちは見付からないはずはない) + const auto it2 = helpmap.find(cmd2); + if (it2 == helpmap.end()) { + fprintf(cons, "Warning: '%s' in '%s' not found\n", + msg1.c_str(), cmd1.c_str()); + return CmdAct::Stay; + } + const auto& msg2 = *(it2->second); + msg = &msg2; + } + + std::string disp = HelpConvert(*msg); + fprintf(cons, "%s", disp.c_str()); + return CmdAct::Stay; } // hb : ブレークポイント系コマンドの一覧を表示 // こいつだけ結構占めるので別階層。 -void +Debugger::CmdAct Debugger::cmd_hb() { - Help(HelpMsgBreakpoints); + return ShowHelpList(HelpListBreakpoints); } // hr : レジスタ表示系コマンドの一覧を表示 // CPU ごとに違うので。 -void +Debugger::CmdAct Debugger::cmd_hr() { - Help(md->GetRegisterHelp()); + return ShowHelpList(curmd->GetHelpListReg()); } -// ヘルプ表示の下請け。 -void -Debugger::Help(const HelpMessages& msgs) +// ヘルプ一覧を表示。 +Debugger::CmdAct +Debugger::ShowHelpList(const HelpMessages& msgs) { - cons->Print("Type \"help \" for indivisual details.\n"); + fprintf(cons, "Type \"help \" for indivisual details.\n"); for (const auto& pair : msgs) { - cons->Print(" %-16s %s\n", pair.first.c_str(), pair.second.c_str()); + fprintf(cons, " %-16s %s\n", pair.first.c_str(), pair.second.c_str()); } + return CmdAct::Stay; } // 個別ヘルプメッセージを出力用に置換。 @@ -707,10 +1077,10 @@ Debugger::HelpConvert(const std::string& } /*static*/ const HelpMessages -Debugger::HelpMsgMain = { +Debugger::HelpListMain = { { "b*", "Set/show Breakpoints (Type \"hb\" for details)" }, { "brhist", "Show branch history" }, - { "c", "Continue" }, + { "c/ct", "Continue" }, { "d/dt/D", "Disassemble" }, { "disp", "Set register group to show" }, { "exhist", "Show exception history" }, @@ -727,7 +1097,7 @@ Debugger::HelpMsgMain = { }; /*static*/ const HelpMessages -Debugger::HelpMsgBreakpoints = { +Debugger::HelpListBreakpoints = { { "b", "Show all breakpoints" }, { "b arg..","Set/Delete breakpoint" }, { "bm", "Set memory breakpoint" }, @@ -758,11 +1128,12 @@ Debugger::HelpDetails = { //----- { "b", R"**( Command: b - Command: b
[] + Command: b [,]
[] Command: b #n The first form (with no arguments) shows all breakpoints. - The second form sets a new breakpoint on
. + The second form sets a new breakpoint at
on . + If is omitted, the current cpu is used. XXX skipcount If is -1, it will never match. It's useful to count the number of times you have passed this address. @@ -771,9 +1142,10 @@ Debugger::HelpDetails = { //----- { "bm", R"**( - Command: bm
[] + Command: bm [,]
[] - Sets a memory breakpoint on
. + Sets a memory breakpoint at
on . + If is omitted, the current cpu is used. XXX skipcount If is -1, it will never match. It's useful to count the number of times you have passed this address. @@ -781,16 +1153,17 @@ Debugger::HelpDetails = { //----- { "bi", R"**( - Command: bi [:] [] + Command: bi [,][:] [] - Sets an instruction breakpoint. must be 16 bits or 32 bits on - m68k and must be 32 bits on m88k. can specify the mask. Its - length must be the same as . If the is ommited, all bits - of is used to compare. + Sets an instruction breakpoint on . must be 16 bits or 32 + bits on m68k and must be 32 bits on m88k. can specify the mask. + Its length must be the same as . If the is omitted, all + bits of is used to compare. For example, "bi 4e75" on m68k will stop at 'rts' instruction. "bi 70004e4f:fff0ffff" on m68k will stop at any of 'IOCS #0'..'IOCS #15'. + If is omitted, the current cpu is used. XXX skipcount If is -1, it will never match. It's useful to count the number of times you have passed this address. @@ -798,13 +1171,15 @@ Debugger::HelpDetails = { //----- { "bv", R"**( - Command: bv [-] [] + Command: bv [,][-] [] - Sets an exception breakpoint. must be specified in hex. + Sets an exception breakpoint on . must be specified in hex. ranges from 0 to ff (255 in decimal) on m68k, and 0 to 1ff (511 in decimal) on m88k. If is specified, it matches the range from to (including themselves). For example, "bv 1-1ff" on m88k means all exceptions but reset. + + If is omitted, the current cpu is used. XXX skipcount If is -1, it will never match. It's useful to count the number of times you have passed this address. @@ -833,17 +1208,35 @@ Debugger::HelpDetails = { )**" }, //----- + { "ct", R"**( + Command: ct [] + + Continue until specified has elapsed. If the is + omitted, the last value is used again. The unit of is + seconds in default. You can use "msec", "usec", or "nsec" suffix. + )**" }, + + //----- { "d", R"**( Command: D
] [] - Command: d [[:]
] [] - Command: dt [[:]
] [] + Command: d [[:]
] [] + Command: dt [[:]
] [] Shows disassemble. The first form ("D") interprets
as a physical address. The second and third form ("d", "dt") interprets
as a logical - address if the address translation is enabled. Normally,
is - interpreted in the current privilege. You can force this by - specifier. "s" means the supervisor and "u" means the user privilege. + address if the address translation is enabled. Normally,
is + interpreted in the current privilege and current address space. You can + change it by modifier. + On m68k, can be specified either by function code number directly + ('1', '2', '5', and '6) or by one or more following modifiers: + 's' .. Supervisor mode 'u' .. User mode + 'd' .. Data space 'p' or 'i' .. Program space + On m88k, can be specified by using combination of privilege + modifier ('s' or 'u', same as above) and following CMMU identifiers: + 'd' .. Data CMMU 'i' or 'p' .. Instruction CMMU + Or CMMU can also be identified by CMMU number (like '6' or '7'). + "d" only looks up in ATC (on m68k) or in BATC/PATC (on m88k). "dt" will simulate to search the page table in addition to that. )**" }, @@ -888,7 +1281,7 @@ Debugger::HelpDetails = { //----- { "L", R"**( - Command: L + Command: L [=][,=[]]... Set loglevel. XXX To be written... )**" }, @@ -896,15 +1289,24 @@ Debugger::HelpDetails = { //----- { "m", R"**( Command: M
] [] - Command: m [[:]
] [] - Command: mt [[:]
] [] + Command: m [[:]
] [] + Command: mt [[:]
] [] Shows memory dump. The first form ("M") interprets
as a physical address. The second and third form ("m", "mt") interprets
as a logical address if the address translation is enabled. Normally,
is - interpreted in the current privilege. You can force this by - specifier. "s" means the supervisor and "u" means the user privilege. + interpreted in the current privilege and current address space. You can + change it by modifier. + On m68k, can be specified either by function code number directly + ('1', '2', '5', and '6) or by one or more following modifiers: + 's' .. Supervisor mode 'u' .. User mode + 'd' .. Data space 'p' or 'i' .. Program space + On m88k, can be specified by using combination of privilege + modifier ('s' or 'u', same as above) and following CMMU identifiers: + 'd' .. Data CMMU 'i' or 'p' .. Instruction CMMU + Or CMMU can also be identified by CMMU number (like '6' or '7'). + "m" only looks up in ATC (on m68k) or in BATC/PATC (on m88k). "mt" will simulate to search the page table in addition to that. )**" }, @@ -912,12 +1314,19 @@ Debugger::HelpDetails = { { "M", "=m" }, //----- + { "mpu", R"**( + Command: mpu + + Change the target CPU to "mpu"(M68030/M88100). + )**" }, + + //----- { "n", R"**( Command: n [] Command: nt [] Step one (or ) instructions. Unlike "s" command, "n" skips - subroutine. + subroutine (and repeat instruction like as LDIR in HD64*180). If "t" is suffixed, it shows a trace for each instruction (including while skipping). )**" }, @@ -969,6 +1378,20 @@ Debugger::HelpDetails = { //----- { "t", "=s" }, + + //----- + { "xp", R"**( + Command: xp + + Change the target CPU to "xp"(HD647180). + )**" }, + + //----- + { "z", R"**( + Command: z + + Continue until the next address. + )**" }, }; // cmdbuf を args... に分解する。 @@ -1012,10 +1435,10 @@ Debugger::ParseCmdbuf() // // ブレークポイント -// b ... 一覧表示 -// b #n ... #n を削除 -// b [] ... 設定 -void +// b ... 一覧表示 +// b #n ... #n を削除 +// b [,] [] ... 設定 +Debugger::CmdAct Debugger::cmd_b() { // 引数なしなら一覧表示 @@ -1026,54 +1449,54 @@ Debugger::cmd_b() // 引数取得 if (args[1][0] == '#') { // # 形式なら、指定番号のブレークポイントを削除 - int i = atoi(&args[1][1]); - if (i < 0 || i >= bpoint.size()) { - cons->Print("invaild breakpoint number: #%d\n", i); - return; - } - auto& bp = bpoint[i]; - if (bp.type == BreakpointType::Unused) { - cons->Print("invalid breakpoint number: #%d\n", i); - return; - } - cons->Print("breakpoint #%d (%08x) removed\n", - i, bp.addr); - bp.type = BreakpointType::Unused; - // 今登録されている命令ブレークの必要命令長を再計算 - RecalcInstMask(); - return; + cmd_b_delete(); + return CmdAct::Stay; } return cmd_b_set(BreakpointType::Address); } // メモリブレークポイントの設定 -// bm [] -void +// bm [,] [] +Debugger::CmdAct Debugger::cmd_bm() { - // XXX m68k では未サポート - if (dynamic_cast(md)) { - cons->Print("bm not supported yet on m68k\n"); - return; + // XXX m88k のみ未サポート + if (curmd->arch != CPUArch::M88xx0) { + fprintf(cons, "bm not supported yet on m68k\n"); + return CmdAct::Stay; } - cmd_b_set(BreakpointType::Memory); + return cmd_b_set(BreakpointType::Memory); } // type が違うだけの各種ブレークポイント設定の共通部分。 -void +Debugger::CmdAct Debugger::cmd_b_set(BreakpointType type) { breakpoint_t bp; + std::string cpustr; + std::string addrstr; if (args.size() < 2) { - cons->Print("usage: %s []\n", args[0].c_str()); - return; + fprintf(cons, "usage: %s [,] []\n", + args[0].c_str()); + return CmdAct::Stay; + } + + // まず CPU を分離 + auto pos = args[1].find(','); + if (pos == std::string::npos) { + // CPU 指定なし + addrstr = args[1]; + } else { + // CPU 指定あり + cpustr = args[1].substr(0, pos); + addrstr = args[1].substr(pos + 1); } // アドレス - if (!ParseAddr(args[1].c_str(), &bp.addr)) { - return; + if (!ParseAddr(addrstr.c_str(), &bp.addr)) { + return CmdAct::Stay; } // あればスキップカウント if (args.size() > 2) { @@ -1083,70 +1506,108 @@ Debugger::cmd_b_set(BreakpointType type) // 空いてるところにセット // (よく似たエントリがあっても干渉しない) bp.type = type; - int bi = AddBreakpoint(bp); + int bi = AddBreakpoint(bp, cpustr); if (bi == -1) { - cons->Print("no free breakpoints\n"); + fprintf(cons, "no free breakpoints\n"); } else { - cons->Print("breakpoint #%d added\n", bi); + fprintf(cons, "breakpoint #%d added\n", bi); } + return CmdAct::Stay; +} + +// ブレークポイント個別削除 +// (引数の先頭が '#' なことを判定したところで呼ばれる) +// b # +Debugger::CmdAct +Debugger::cmd_b_delete() +{ + int n = atoi(&args[1][1]); + if (n < 0 || n >= bpoint.size()) { + fprintf(cons, "invalid breakpoint number: #%d\n", n); + return CmdAct::Stay; + } + + auto& bp = bpoint[n]; + if (bp.type == BreakpointType::Unused) { + fprintf(cons, "invalid breakpoint number: #%d\n", n); + return CmdAct::Stay; + } + + fprintf(cons, "breakpoint #%d removed\n", n); + bp.type = BreakpointType::Unused; + // 今登録されている命令ブレークの必要命令長を再計算 + RecalcInstMask(); + return CmdAct::Stay; } // 命令ブレークポイントの設定 -// bi [:] [] -void +// bi [,][:] [] +Debugger::CmdAct Debugger::cmd_bi() { breakpoint_t bp; + std::string cpustr; + std::string argstr; std::string inststr; std::string maskstr; int instlen; int masklen; if (args.size() < 2) { - cons->Print("usage: bi [:] []\n"); - return; + fprintf(cons, "usage: bi [:] []\n"); + return CmdAct::Stay; } - // 引数をまず分離 - auto pos = args[1].find(':'); + // CPU をまず分離 + auto pos = args[1].find(','); + if (pos == std::string::npos) { + // CPU 指定なし + argstr = args[1]; + } else { + // CPU 指定あり + cpustr = args[1].substr(0, pos); + argstr = args[1].substr(pos + 1); + } + + pos = argstr.find(':'); if (pos == std::string::npos) { // マスク指定なし - inststr = args[1]; + inststr = argstr; instlen = inststr.size(); masklen = -1; } else { // マスク指定あり - inststr = args[1].substr(0, pos); + inststr = argstr.substr(0, pos); instlen = inststr.size(); - maskstr = args[1].substr(pos + 1); + maskstr = argstr.substr(pos + 1); masklen = maskstr.size(); } // 命令部チェック if (ParseVerbHex(inststr.c_str(), &bp.inst) == false) { - cons->Print("%s: invalid instruction value\n", args[1].c_str()); - return; + fprintf(cons, "%s: invalid instruction value\n", argstr.c_str()); + return CmdAct::Stay; } - if (instlen % (md->inst_bytes * 2) != 0) { - cons->Print("%s: invalid instruction length\n", args[1].c_str()); - return; + if (instlen % (curmd->inst_bytes * 2) != 0) { + fprintf(cons, "%s: invalid instruction length\n", argstr.c_str()); + return CmdAct::Stay; } // マスク部チェック bp.mask = 0xffffffff; if (masklen != -1) { if (ParseVerbHex(maskstr.c_str(), &bp.mask) == false) { - cons->Print("%s: invalid mask value\n", args[1].c_str()); - return; + fprintf(cons, "%s: invalid mask value\n", argstr.c_str()); + return CmdAct::Stay; } if (masklen != instlen) { - cons->Print("%s: inst:mask must be the same length\n", - args[1].c_str()); - return; + fprintf(cons, "%s: inst:mask must be the same length\n", + argstr.c_str()); + return CmdAct::Stay; } } // 8バイト未満なら左詰め。 - if (md->inst_bytes < 4 && instlen < 8) { + if (curmd->inst_bytes < 4 && instlen < 8) { bp.inst <<= 32 - instlen * 4; bp.mask <<= 32 - instlen * 4; } @@ -1159,60 +1620,78 @@ Debugger::cmd_bi() // 空いてるところにセット bp.type = BreakpointType::Instruction; - int bi = AddBreakpoint(bp); + int bi = AddBreakpoint(bp, cpustr); if (bi == -1) { - cons->Print("no free breakpoints\n"); + fprintf(cons, "no free breakpoints\n"); } else { - cons->Print("breakpoint #%d added\n", bi); + fprintf(cons, "breakpoint #%d added\n", bi); } // 今登録されている命令ブレークの必要命令長を再計算 RecalcInstMask(); + return CmdAct::Stay; } // 例外ブレークポイントの設定 -// bv [-] [] -void +// bv [,][-] [] +Debugger::CmdAct Debugger::cmd_bv() { breakpoint_t bp; + std::string argstr; + std::string cpustr; + DebuggerMD *md; if (args.size() < 2) { - cons->Print("usage: be [-] []\n"); - return; + fprintf(cons, "usage: bv [,][-] []\n"); + return CmdAct::Stay; + } + + // CPU をまず分離 + auto pos = args[1].find(','); + if (pos == std::string::npos) { + // CPU 指定なし + argstr = args[1]; + } else { + // CPU 指定あり + cpustr = args[1].substr(0, pos); + argstr = args[1].substr(pos + 1); } + // ベクタ名解釈のために必要 + md = ParseCPU(cpustr); - auto pos = args[1].find('-'); + pos = argstr.find('-'); if (pos == std::string::npos) { // ベクタ番号が1つなら vec1, vec2 を同値にしておく。 - if (ParseVerbHex(args[1].c_str(), (uint32 *)&bp.vec1) == false) { - cons->Print("%s: invalid vector number\n", args[1].c_str()); - return; + if (ParseVector(md, argstr.c_str(), (uint32 *)&bp.vec1) == false) { + fprintf(cons, "%s: invalid vector number\n", argstr.c_str()); + return CmdAct::Stay; } bp.vec2 = bp.vec1; } else { // ベクタ番号(範囲指定 [vec1, vec2]) - std::string str1 = args[1].substr(0, pos); - std::string str2 = args[1].substr(pos + 1); + std::string str1 = argstr.substr(0, pos); + std::string str2 = argstr.substr(pos + 1); - if (ParseVerbHex(str1.c_str(), (uint32 *)&bp.vec1) == false) { - cons->Print("%s: invalid first vector number\n", args[1].c_str()); - return; - } - if (ParseVerbHex(str2.c_str(), (uint32 *)&bp.vec2) == false) { - cons->Print("%s: invalid last vector number\n", args[1].c_str()); - return; + if (ParseVector(md, str1.c_str(), (uint32 *)&bp.vec1) == false) { + fprintf(cons, "%s: invalid first vector number\n", argstr.c_str()); + return CmdAct::Stay; + } + if (ParseVector(md, str2.c_str(), (uint32 *)&bp.vec2) == false) { + fprintf(cons, "%s: invalid last vector number\n", argstr.c_str()); + return CmdAct::Stay; } } // 範囲チェック - if (bp.vec1 < 0 || bp.vec1 >= md->vector_max) { - cons->Print("$%x: invalid vector number\n", bp.vec1); - return; - } - if (bp.vec2 < 0 || bp.vec2 >= md->vector_max) { - cons->Print("$%x: invalid last vector number\n", bp.vec2); - return; + auto vectortable = pVectorTable.get(); + if (bp.vec1 < 0 || bp.vec1 >= vectortable->Size()) { + fprintf(cons, "$%x: invalid vector number\n", bp.vec1); + return CmdAct::Stay; + } + if (bp.vec2 < 0 || bp.vec2 >= vectortable->Size()) { + fprintf(cons, "$%x: invalid last vector number\n", bp.vec2); + return CmdAct::Stay; } // 大小が逆なら入れ替える? @@ -1231,49 +1710,95 @@ Debugger::cmd_bv() // 空いてるところにセット bp.type = BreakpointType::Exception; - int bi = AddBreakpoint(bp); + int bi = AddBreakpoint(bp, cpustr); if (bi == -1) { - cons->Print("no free breakpoints\n"); + fprintf(cons, "no free breakpoints\n"); } else { - cons->Print("breakpoint #%d added\n", bi); + fprintf(cons, "breakpoint #%d added\n", bi); } - // すでに来ている例外をクリア。 + // この CPU 側に、すでに来ている例外をクリア。 // ブレークポイント設定の有無に関わらず例外が起きたら CPU 側から常に // 通知されている。これをクリアするのは CheckAllBreakpoints() で、これは // 命令間(命令前)に呼ばれるやつ、なのでこうなる。 - // 1. 例外が起きると bv_vector がセットされる + // 1. 例外が起きると md->bv_vector がセットされる // 2. 例外ブレークポイントを設定していないとこれがクリアされない // 3. bv コマンドで例外ブレークを新たに設定すると、次の命令境界で // 1.のベクタが反応してしまう。 // 命令ごととかにクリアしてもいいかも知れないが、ここでブレークポイントを // 設定したのだから、それ以前の事象には反応すべきでない、という意味では // ここでもいいか? - bv_vector = -1; + + // bp.md は AddBreakpoint() で bpoint 登録時にセットされるので + // インデックスから bp をもう一度読み込む。 + bp = bpoint[bi]; + bp.md->bv_vector = -1; + + return CmdAct::Stay; +} + +// ベクタ名かベクタ番号をパースする。 +bool +Debugger::ParseVector(DebuggerMD *md, const char *arg, uint32 *valp) +{ + // 数値変換出来るか。 + if (ParseVerbHex(arg, valp)) { + return true; + } + + // 出来なければ、機種ごとにベクタ名と比較する。 + if (md->arch == CPUArch::HD64180) { + static std::vector table = { + "trap", + "nmi", + "int0", + "int1", + "int2", + "inpcap", + "outcmp", + "timeov", + "timer0", + "timer1", + "dma0", + "dma1", + "csio", + "asci0", + "asci1", + }; + for (int i = 0, end = table.size(); i < end; i++) { + if (strcasecmp(arg, table[i]) == 0) { + *valp = i; + return true; + } + } + } + + return false; } // ブレークポイント一覧表示 -void +Debugger::CmdAct Debugger::cmd_b_list() { - ShowMonitor(*gBreakpointMonitor); + ShowMonitor(bpoint_monitor); + return CmdAct::Stay; } // ブレークポイント一覧 (モニタ) void -Debugger::MonitorBreakpoint(TextScreen& monitor) +Debugger::MonitorUpdateBpoint(Monitor *, TextScreen& monitor) { // 0 1 2 3 4 5 6 // 0123456789012345678901234567890123456789012345678901234567890 - // No Type Parameter Matched Skip - // #0 addr $01234567 123456789 123456789/123456789 - // #1 inst 00000000/00000000 - // #2 excp $00-$00 + // No CPU Type Parameter Matched Skip + // #0 xp addr $01234567 123456789 123456789/123456789 + // #1 main inst 00000000/00000000 + // #2 main excp $00-$00 monitor.Clear(); - monitor.Print(0, 0, "No Type Parameter"); - monitor.Print(26, 0, "Matched"); - monitor.Print(37, 0, "Skip"); + monitor.Print(0, 0, "No CPU Type Parameter"); + monitor.Print(31, 0, "Matched"); + monitor.Print(42, 0, "Skip"); for (int i = 0; i < bpoint.size(); i++) { const auto& bp = bpoint[i]; @@ -1287,82 +1812,106 @@ Debugger::MonitorBreakpoint(TextScreen& continue; case BreakpointType::Address: - monitor.Print(3, y, "addr $%08x", bp.addr); + monitor.Print(3, y, "%-4s addr $%08x", + bp.md->GetName().c_str(), bp.addr); break; case BreakpointType::Memory: - monitor.Print(3, y, "mem $%08x", bp.addr); + monitor.Print(3, y, "%-4s mem $%08x", + bp.md->GetName().c_str(), bp.addr); break; case BreakpointType::Exception: - monitor.Print(3, y, "excp $%02x", bp.vec1); + monitor.Print(3, y, "%-4s excp $%02x", + bp.md->GetName().c_str(), bp.vec1); if (bp.vec2 != bp.vec1) { - monitor.Print(11, y, "-$%02x", bp.vec2); + monitor.Print(16, y, "-$%02x", bp.vec2); } break; case BreakpointType::Instruction: - monitor.Print(3, y, "inst"); - if (md->inst_bytes == 4) { + monitor.Print(3, y, "%-4s inst", bp.md->GetName().c_str()); + if (bp.md->inst_bytes == 4) { if (bp.mask == 0xffffffff) { - monitor.Print(8, y, "%08x", bp.inst); + monitor.Print(13, y, "%08x", bp.inst); } else { - monitor.Print(8, y, "%08x:%08x", bp.inst, bp.mask); + monitor.Print(13, y, "%08x:%08x", bp.inst, bp.mask); } } else { if (bp.mask == 0xffffffff) { - monitor.Print(8, y, "%08x", bp.inst); + monitor.Print(13, y, "%08x", bp.inst); } else if (((bp.inst | bp.mask) & 0x0000ffff) != 0) { - monitor.Print(8, y, "%08x:%08x", bp.inst, bp.mask); + monitor.Print(13, y, "%08x:%08x", bp.inst, bp.mask); } else if (bp.mask == 0xffff0000) { - monitor.Print(8, y, "%04x", bp.inst >> 16); + monitor.Print(13, y, "%04x", bp.inst >> 16); } else { - monitor.Print(8, y, "%04x:%04x", + monitor.Print(13, y, "%04x:%04x", bp.inst >> 16, bp.mask >> 16); } } break; default: - monitor.Print(3, y, "type=%d", (int)bp.type); + monitor.Print(3, y, "%-4s type=%d", + (bp.md ? bp.md->GetName().c_str() : "?"), (int)bp.type); continue; } // マッチ回数 - monitor.Print(26, y, "%d", bp.matched); + monitor.Print(31, y, "%d", bp.matched); // スキップ if (bp.skip < 0) { - monitor.Print(37, y, "forever"); + monitor.Print(42, y, "forever"); } else if (bp.skip > 0) { - monitor.Print(37, y, "%d / %d", (bp.skip - bp.skipremain), bp.skip); + monitor.Print(42, y, "%d / %d", (bp.skip - bp.skipremain), bp.skip); } } } // ブレークポイント全削除 -void +Debugger::CmdAct Debugger::cmd_bx() { for (auto& bp : bpoint) { bp.type = BreakpointType::Unused; } - cons->Print(" All breakpoints disabled\n"); + fprintf(cons, " All breakpoints disabled\n"); // 今登録されている命令ブレークの必要命令長を再計算 RecalcInstMask(); + + return CmdAct::Stay; +} + +// CPU 文字列から対応する MD を返す。 +// 一致しなければ NULL を返す。 +DebuggerMD * +Debugger::ParseCPU(const std::string& cpustr) const +{ + if (cpustr.empty()) { + return curmd; + } else if (cpustr == "xp") { + return md_xp.get(); + } else if (cpustr == "main") { + return md_mpu.get(); + } + return NULL; } // ブレークポイントを設定。 -// new_bp のうち matched, skipremain はこちらで初期化する。 +// new_bp のうち cpu, matched, skipremain はこちらで初期化する。 // それ以外を埋めてから呼ぶこと。 // 設定できればその番号、できなければ -1 を返す。 int -Debugger::AddBreakpoint(const breakpoint_t& new_bp) +Debugger::AddBreakpoint(const breakpoint_t& new_bp, const std::string& cpustr) { + DebuggerMD *md = ParseCPU(cpustr); + for (int i = 0; i < bpoint.size(); i++) { auto& bp = bpoint[i]; if (bp.type == BreakpointType::Unused) { bp = new_bp; + bp.md = md; bp.matched = 0; if (bp.skip > 0) { bp.skipremain = bp.skip; @@ -1400,7 +1949,7 @@ Debugger::RecalcInstMask() // 上位側から数えたマスクに必要なビット数 int mlen = 32 - ntz; // 命令語単位に切り上げる - mlen = roundup(mlen, md->inst_bytes * 8); + mlen = roundup(mlen, curmd->inst_bytes * 8); // バイト数に変換 mlen /= 8; @@ -1416,20 +1965,22 @@ Debugger::RecalcInstMask() // コンソールではデフォルトで下を新しいの順とする。 を負数にすると // (行数は絶対値して) 並び順を逆にしてモニタウィンドウと同じ上を新しいの順に // する。 -void +Debugger::CmdAct Debugger::cmd_brhist() { - cmd_hist_common(md->GetBrHist(), 0); + auto brhist = gMainApp.GetObject(OBJ_MPU_BRHIST); + return cmd_hist_common(*brhist); } -void +Debugger::CmdAct Debugger::cmd_exhist() { - cmd_hist_common(md->GetExHist(), BranchHistory::ExHist); + auto exhist = gMainApp.GetObject(OBJ_MPU_EXHIST); + return cmd_hist_common(*exhist); } // ブランチ履歴、例外履歴表示の共通部分。 -void -Debugger::cmd_hist_common(BranchHistory& hist, uint64 flag) +Debugger::CmdAct +Debugger::cmd_hist_common(BranchHistory& hist) { // 表示最大行数(と向き) // 向きは bottom_to_top = true が新しいほうを下とする方向。 @@ -1456,175 +2007,347 @@ Debugger::cmd_hist_common(BranchHistory& int lines = std::min(used, maxlines) + 1; // MonitorUpdate() は TextScreen 高さに合わせて出力してくれる。 - auto size = hist.GetMonitorSize(); - TextScreen tscr; - tscr.Init(size.width, lines); + auto& histmon = hist.monitor; + auto size = histmon.GetSize(); + TextScreen screen; + screen.Init(size.width, lines); // コンソールでは下が新しいの順のほうがいい if (bottom_to_top) { - flag |= BranchHistory::BottomToTop; + screen.userdata |= BranchHistory::BottomToTop; } - tscr.userdata = flag; // 表示 - hist.MonitorUpdate(tscr); - ShowTextScreen(tscr); + MONITOR_UPDATE(histmon, screen); + ShowTextScreen(screen); + + return CmdAct::Stay; } // 実行再開(continue): c [] // 指定があれば まで実行。 -void +Debugger::CmdAct Debugger::cmd_c() { if (args.size() > 1) { // 引数があれば - if (!ParseAddr(args[1].c_str(), &bc_addr)) { - return; + uint32 addr; + if (!ParseAddr(args[1].c_str(), &addr)) { + return CmdAct::Stay; + } + step_type = StepType::Addr; + step_md = curmd; + // ネイティブ命令長に丸める + step_addr = addr & ~(curmd->inst_bytes - 1); + } + return CmdAct::Leave; +} + +// 指定仮想時間実行: ct [