Annotation of nono/debugger/debugger.cpp, revision 1.1.1.15

1.1       root        1: //
                      2: // nono
1.1.1.3   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
1.1.1.11  root        7: //
                      8: // デバッガ
                      9: //
                     10: 
                     11: //
                     12: // VM スレッド            デバッガスレッド            HostCOM
                     13: //                            condvar
                     14: //  |                            |                       |<--- 入力
                     15: //  |                            |<----------------------|
                     16: //  |                            |       RxCallback
                     17: //  |                            |
                     18: //  |                            |---------------------->|
                     19: //  |                            |  HostCOMDevice::Tx()  |---> 出力
                     20: 
                     21: //  |<---------------------------|
                     22: //  | is_prompt = true;          | デバッガスレッドからプロンプトを出したい
1.1.1.13  root       23: //  | Message(MPU_TRACE_ALL);    | 場合は MPU (VM) をトレースモードにする。
1.1.1.11  root       24: //  |                            | その際 is_prompt を立てておくことで止まる。
                     25: //  |                            |
                     26: //  |--------------------------->|
                     27: //  | condvar REQUEST_PROMPT     | VM スレッドからプロンプトを出したい場合
                     28: //  |                            | (上述の例も含む) は条件変数で通知。
                     29: //  |                            |
                     30: //  |<---------------------------|
                     31: //  | condvar prompt_released    | プロンプトを出している間 VM スレッドは
                     32: //  |                            | 条件変数で待機しているので、これを起こす
                     33: //  |                            | ことで実行再開。
                     34: 
                     35: #include "debugger.h"
1.1.1.13  root       36: #include "debugger_hd64180.h"
1.1.1.2   root       37: #include "debugger_m680x0.h"
                     38: #include "debugger_m88xx0.h"
1.1.1.11  root       39: #include "hostcom.h"
1.1.1.2   root       40: #include "mainapp.h"
1.1.1.13  root       41: #include "memdump.h"
1.1.1.9   root       42: #include "mystring.h"
1.1.1.11  root       43: #include "power.h"
1.1.1.7   root       44: #include "scheduler.h"
1.1.1.13  root       45: #include "syncer.h"
1.1.1.11  root       46: #include "uimessage.h"
                     47: #include "vectortable.h"
1.1.1.3   root       48: #include <algorithm>
1.1.1.12  root       49: #include <cmath>
1.1.1.13  root       50: #include <map>
1.1.1.11  root       51: #if defined(HAVE_BSD_STDIO_H)
                     52: #include <bsd/stdio.h>
                     53: #endif
1.1       root       54: 
1.1.1.11  root       55: static int readfunc(void *, char *, int);
                     56: static int writefunc(void *, const char *, int);
1.1       root       57: 
                     58: // コンストラクタ
                     59: Debugger::Debugger()
1.1.1.13  root       60:        : inherited(OBJ_DEBUGGER)
1.1       root       61: {
1.1.1.11  root       62:        // ベクタテーブル
                     63:        pVectorTable.reset(new VectorTable(gMainApp.GetVMType()));
1.1.1.9   root       64: 
1.1.1.13  root       65:        // ブレークポイントモニタ
1.1.1.11  root       66:        bpoint_monitor.func = ToMonitorCallback(&Debugger::MonitorUpdateBpoint);
1.1.1.13  root       67:        bpoint_monitor.SetSize(60, 9);
1.1.1.9   root       68:        bpoint_monitor.Regist(ID_MONITOR_BREAKPOINT);
                     69: 
1.1.1.13  root       70:        // メモリダンプモニタ
1.1.1.9   root       71:        for (int i = 0, end = memdump_monitor.size(); i < end; i++) {
1.1.1.13  root       72:                int objid = OBJ_MPU_MEMDUMP(i);
                     73:                int monid = ID_MONITOR_MEMDUMP(i);
                     74:                memdump_monitor[i].reset(new MemdumpMonitor(objid, monid));
                     75:        }
                     76: 
                     77:        if (gMainApp.Has(VMCap::LUNA)) {
                     78:                // XP 空間のメモリダンプモニタ
                     79:                for (int i = 0, end = xpmemdump_monitor.size(); i < end; i++) {
                     80:                        int objid = OBJ_XP_MEMDUMP(i);
                     81:                        int monid = ID_MONITOR_XPMEMDUMP(i);
                     82:                        xpmemdump_monitor[i].reset(new MemdumpMonitor(objid, monid));
                     83:                }
                     84:        }
1.1       root       85: }
                     86: 
1.1.1.11  root       87: // デストラクタ
                     88: Debugger::~Debugger()
                     89: {
                     90:        // fclose(fflush) にあたり hostcom へのアクセスが発生するので、
                     91:        // hostcom より先に片付けておかなければならない。
                     92:        Close();
                     93: 
                     94:        if ((bool)hostcom) {
1.1.1.14  root       95:                hostcom->SetRxCallback(NULL);
                     96:                hostcom->SetAcceptCallback(NULL);
1.1.1.11  root       97:        }
                     98: 
                     99:        TerminateThread();
                    100: }
                    101: 
                    102: bool
                    103: Debugger::Create()
                    104: {
                    105:        // ホストドライバを作成
1.1.1.14  root      106:        hostcom.reset(new HostCOMDevice(this, "Debugger"));
                    107:        if ((bool)hostcom == false) {
1.1.1.11  root      108:                return false;
                    109:        }
                    110: 
                    111:        hostcom->SetRxCallback(ToDeviceCallback(&Debugger::RxCallback));
                    112:        hostcom->SetAcceptCallback(ToDeviceCallback(&Debugger::AcceptCallback));
                    113: 
                    114:        return true;
                    115: }
                    116: 
1.1.1.13  root      117: // ログレベル設定
                    118: void
                    119: Debugger::SetLogLevel(int loglevel_)
                    120: {
                    121:        inherited::SetLogLevel(loglevel_);
                    122: 
                    123:        // ホストドライバを従属させる
                    124:        if ((bool)hostcom) {
                    125:                hostcom->SetLogLevel(loglevel_);
                    126:        }
                    127: }
                    128: 
1.1.1.11  root      129: // 初期化
                    130: bool
1.1       root      131: Debugger::Init()
                    132: {
1.1.1.13  root      133:        if (inherited::Init() == false) {
                    134:                return false;
                    135:        }
                    136: 
                    137:        syncer = GetSyncer();
                    138: 
                    139:        if (gMainApp.Has(VMCap::M88K)) {
                    140:                md_mpu.reset(new DebuggerMD_m88xx0(this));
1.1.1.11  root      141:        } else {
1.1.1.13  root      142:                md_mpu.reset(new DebuggerMD_m680x0(this));
                    143:        }
                    144:        if (gMainApp.Has(VMCap::LUNA)) {
                    145:                md_xp.reset(new DebuggerMD_hd64180(this));
                    146:        }
                    147:        // とりあえずメインプロセッサに固定
                    148:        curmd = md_mpu.get();
                    149: 
                    150:        // md_* が用意できたので MemdumpMonitor にセットする。
                    151:        // これらは Monitor なので Init() より前に用意してなければならないが、
                    152:        // 一方で md は諸々が落ち着いた後のここでないと用意できない。うーん。
                    153:        // ここはバス別のモニタなので curmd ではなく md_{mpu,xp} を参照する。
                    154:        for (int i = 0, end = memdump_monitor.size(); i < end; i++) {
                    155:                auto *mem = memdump_monitor[i].get();
                    156:                mem->InitMD(md_mpu.get());
                    157:        }
                    158:        if (gMainApp.Has(VMCap::LUNA)) {
                    159:                for (int i = 0, end = xpmemdump_monitor.size(); i < end; i++) {
                    160:                        auto *mem = xpmemdump_monitor[i].get();
                    161:                        mem->InitMD(md_xp.get());
                    162:                }
1.1.1.11  root      163:        }
                    164: 
1.1       root      165:        // -d なら CPU 起動時点で停止してプロンプトを待つ
1.1.1.2   root      166:        if (gMainApp.debug_on_start) {
1.1.1.11  root      167:                is_pause = true;
                    168:                // この時点ではまだ MPU にメッセージを送ることはできない
1.1       root      169:        }
                    170: 
1.1.1.13  root      171:        // -b [<cpu>,]<addr>[,<skip>] ならブレークポイント設定
1.1.1.4   root      172:        for (auto& str : gMainApp.debug_breakaddr) {
                    173:                breakpoint_t bp;
1.1.1.13  root      174:                std::string cpustr;
                    175:                std::string addrstr;
                    176:                std::string skipstr;
                    177: 
                    178:                // "," で分離
                    179:                auto arr = string_split(str, ',');
                    180:                if (arr.size() < 2) {
                    181:                        // 1個ならアドレス (0 ってことはないはずだが)
                    182:                        addrstr = arr[0];
                    183:                } else if (arr.size() == 2) {
                    184:                        // 2個なら cpu か skip のどちらかが省略。
                    185:                        // 1つ目が16進数っぽいかどうかで判定する。
                    186:                        char *end;
                    187:                        strtoul(arr[0].c_str(), &end, 16);
                    188:                        if (end == &arr[0][0]) {
                    189:                                cpustr = arr[0];
                    190:                                addrstr = arr[1];
                    191:                        } else {
                    192:                                addrstr = arr[0];
                    193:                                skipstr = arr[1];
                    194:                        }
                    195:                } else if (arr.size() == 3) {
                    196:                        cpustr  = arr[0];
                    197:                        addrstr = arr[1];
                    198:                        skipstr = arr[2];
                    199:                } else {
                    200:                        warnx("\"%s\": Invalid breakpoint", str.c_str());
                    201:                        return false;
1.1.1.4   root      202:                }
1.1.1.13  root      203: 
                    204:                // <addr> を取り出す
1.1.1.15! root      205:                if (!ParseVerbHex(addrstr.c_str(), &bp.addr)) {
1.1.1.4   root      206:                        warnx("\"%s\": Invalid breakpoint address", str.c_str());
1.1.1.11  root      207:                        return false;
1.1.1.4   root      208:                }
1.1.1.13  root      209:                // <skip> を取り出す
                    210:                if (skipstr.empty() == false) {
                    211:                        bp.skip = atoi(skipstr.c_str());
                    212:                }
1.1.1.4   root      213:                // 登録
                    214:                bp.type = BreakpointType::Address;
1.1.1.13  root      215:                AddBreakpoint(bp, cpustr);
1.1       root      216:        }
                    217: 
1.1.1.15! root      218:        // --bi-exg なら命令ブレークポイントを設定
        !           219:        // (CPU のチェックはしていない)
        !           220:        if (gMainApp.debug_breakinst_exg) {
        !           221:                breakpoint_t bp;
        !           222:                bp.type = BreakpointType::Instruction;
        !           223:                bp.inst = 0xcf4f0000;
        !           224:                bp.mask = 0xffff0000;
        !           225:                AddBreakpoint(bp, "");
        !           226:                // 今登録されている命令ブレークの必要命令長を再計算
        !           227:                RecalcInstMask();
        !           228:        }
        !           229: 
1.1.1.11  root      230:        // 入出力を FILE で扱う
                    231:        cons = funopen(this, readfunc, writefunc, NULL, NULL);
                    232:        if (cons == NULL) {
                    233:                warnx("funopen2 failed");
                    234:                return false;
                    235:        }
                    236: 
                    237:        return true;
1.1       root      238: }
                    239: 
                    240: // デバッガスレッド
                    241: void
                    242: Debugger::ThreadRun()
                    243: {
1.1.1.5   root      244:        // disp_regs の初期値設定。
                    245:        // 再接続でも継続してていいような気がするのでループ外で初期化。
                    246:        disp_regs.clear();
                    247:        disp_regs.push_back("r");
                    248: 
1.1.1.11  root      249:        // MPU のトレース状態の初期化は vm/mpu* 側のリセット例外で行っている。
                    250: 
1.1       root      251:        for (;;) {
1.1.1.11  root      252:                // 何か起きるまで待つ
                    253:                uint32 req;
                    254:                {
                    255:                        std::unique_lock<std::mutex> lock(mtx);
                    256:                        cv_request.wait(lock, [&] { return request != 0; });
                    257:                        req = request;
                    258:                        request = 0;
                    259:                }
                    260: 
                    261:                if ((req & REQUEST_EXIT)) {
1.1.1.3   root      262:                        break;
                    263:                }
                    264: 
1.1.1.11  root      265:                if ((req & REQUEST_RXCHAR)) {
                    266:                        // コンソールからの文字入力
                    267:                        int c;
                    268:                        while ((c = hostcom->Rx()) >= 0) {
                    269:                                Input(c);
                    270:                        }
                    271:                }
1.1       root      272: 
1.1.1.11  root      273:                if ((req & REQUEST_ACCEPT)) {
                    274:                        // TCP 待ち受けに着信があればすぐにプロンプトを出したい
                    275:                        Input('\n');
                    276:                }
1.1.1.3   root      277: 
1.1.1.11  root      278:                if ((req & REQUEST_PROMPT)) {
                    279:                        // VM 停止(したのでプロンプトモードへ)
                    280:                        EnterPrompt();
                    281:                }
                    282:        }
                    283: 
1.1.1.13  root      284:        LeavePrompt(false);
1.1.1.11  root      285:        Close();
                    286: }
                    287: 
                    288: // コンソールをクローズする
                    289: void
                    290: Debugger::Close()
                    291: {
                    292:        if (cons) {
                    293:                fclose(cons);
                    294:                cons = NULL;
                    295:        }
                    296: }
                    297: 
                    298: #if 0
1.1.1.3   root      299:                bool first = true;
                    300:                for (;;) {
                    301:                        // 接続後の1回目だけ実行するもの。
                    302:                        if (first) {
                    303:                                first = false;
                    304: 
                    305:                                // greeting はプロンプトが取れる前にもう表示したい。
                    306:                                // 何らかの事故でプロンプトが取れなくても、ここまでは接続
                    307:                                // できてることが分かるように。
1.1.1.11  root      308:                                fprintf(cons, "This is debugger console\n");
1.1.1.3   root      309: 
                    310:                                // 接続ごとに初期化する値
                    311:                                n_enable = false;
                    312:                                s_enable = false;
1.1.1.5   root      313:                                t_enable = true;
1.1.1.3   root      314:                        }
1.1.1.11  root      315:                }
                    316:        }
1.1.1.3   root      317: 
1.1.1.11  root      318: #endif
1.1.1.8   root      319: 
1.1.1.11  root      320: // スレッドに終了指示
                    321: void
                    322: Debugger::Terminate()
                    323: {
                    324:        std::unique_lock<std::mutex> lock(mtx);
                    325:        request |= REQUEST_EXIT;
                    326:        cv_request.notify_one();
                    327: }
1.1.1.3   root      328: 
1.1.1.11  root      329: // funopen の read コールバック
                    330: static int
                    331: readfunc(void *cookie, char *buf, int bufsize)
                    332: {
                    333:        return ((Debugger *)cookie)->ReadFunc(buf, bufsize);
                    334: }
1.1.1.3   root      335: 
1.1.1.11  root      336: // funopen の write コールバック
                    337: static int
                    338: writefunc(void *cookie, const char *buf, int len)
                    339: {
                    340:        return ((Debugger *)cookie)->WriteFunc(buf, len);
                    341: }
1.1.1.3   root      342: 
1.1.1.11  root      343: // funopen の read コールバックの本体
                    344: int
                    345: Debugger::ReadFunc(char *buf, int bufsize)
                    346: {
                    347:        char *d = buf;
                    348:        char *end = buf + bufsize;
1.1.1.8   root      349: 
1.1.1.11  root      350:        for (; d < end; ) {
                    351:                if ((bool)hostcom == false) {
                    352:                        errno = EIO;
                    353:                        return -1;
                    354:                }
1.1.1.3   root      355: 
1.1.1.11  root      356:                int c = hostcom->Rx();
                    357:                if (c < 0) {
                    358:                        break;
1.1.1.3   root      359:                }
1.1.1.11  root      360:                *d++ = c;
                    361:        }
                    362:        return (d - buf);
                    363: }
1.1.1.3   root      364: 
1.1.1.11  root      365: // funopen の write コールバックの本体
                    366: int
                    367: Debugger::WriteFunc(const char *buf, int len)
                    368: {
                    369:        const char *s = buf;
                    370:        const char *end = buf + len;
1.1       root      371: 
1.1.1.11  root      372:        for (; s < end; ) {
                    373:                if ((bool)hostcom == false) {
                    374:                        errno = EIO;
                    375:                        return -1;
                    376:                }
                    377: 
                    378:                // ホストスレッドのキューが満杯なら空くまで待つ。うーん…。
                    379:                // XXX 無期限で大丈夫だろうか
                    380:                int c = *s++;
                    381:                if (c == '\n') {
                    382:                        // ここで LF を CRLF にする?
                    383:                        // (TELNET に出力するのに必要)
                    384:                        while (hostcom->Tx('\r') == false) {
                    385:                                usleep(10);
                    386:                        }
                    387:                }
                    388:                while (hostcom->Tx(c) == false) {
                    389:                        usleep(10);
1.1.1.3   root      390:                }
1.1       root      391:        }
1.1.1.11  root      392:        return (s - buf);
                    393: }
1.1       root      394: 
1.1.1.11  root      395: // ホストからの1文字受信通知 (HostCOM スレッドから呼ばれる)
                    396: void
                    397: Debugger::RxCallback()
                    398: {
                    399:        // デバッガスレッドに通知
                    400:        std::unique_lock<std::mutex> lock(mtx);
                    401:        request |= REQUEST_RXCHAR;
                    402:        cv_request.notify_one();
1.1       root      403: }
                    404: 
1.1.1.11  root      405: // ホストからの着信通知 (HostCOM スレッドから呼ばれる)
                    406: void
                    407: Debugger::AcceptCallback()
1.1.1.3   root      408: {
1.1.1.11  root      409:        // デバッガスレッドに通知
                    410:        std::unique_lock<std::mutex> lock(mtx);
                    411:        request |= REQUEST_ACCEPT;
                    412:        cv_request.notify_one();
                    413: }
                    414: 
                    415: // ホストからの1文字入力
                    416: void
                    417: Debugger::Input(int c)
                    418: {
                    419:        // '^@' は捨てる
                    420:        // (意図的にも入力できるが nc が TELNET オプション扱えなくて送ってくる)
                    421:        if (c == '\0') {
                    422:                return;
                    423:        }
                    424: 
                    425:        // HostCOM からは改行で CR が来るようなので LF にしておく
                    426:        if (c == '\r') {
                    427:                c = '\n';
                    428:        }
                    429: 
                    430:        if (is_prompt == false) {
                    431:                // プロンプトでない時は、Enter か ^C でプロンプトを出す
                    432:                if (c == '\n' || c == '\x03') {
                    433:                        // MPU に一時停止を要求
                    434:                        is_pause = true;
1.1.1.13  root      435:                        scheduler->SendMessage(MessageID::MPU_TRACE_ALL, true);
1.1.1.11  root      436:                }
1.1.1.3   root      437:        } else {
1.1.1.11  root      438:                // プロンプト中なら行入力
1.1.1.13  root      439:                if (c == '\b') {
                    440:                        if (cmdbuf.empty() == false) {
                    441:                                // 簡易バックスペースを出力。
                    442:                                // XXX 実際どうするんだこれ
                    443:                                fputc('\b', cons);
                    444:                                fputc(' ', cons);
                    445:                                fputc('\b', cons);
                    446:                                fflush(cons);
1.1.1.3   root      447: 
1.1.1.13  root      448:                                cmdbuf.pop_back();
                    449:                        }
                    450:                } else if (c == '\n') {
1.1.1.11  root      451:                        // Enter ならここでコマンド実行
1.1.1.13  root      452:                        fputc(c, cons);
                    453:                        fflush(cons);
1.1.1.11  root      454: 
                    455:                        auto act = Command();
                    456: 
                    457:                        // 次回との差分のため、今のレジスタセットをバックアップ
1.1.1.13  root      458:                        curmd->BackupRegs();
1.1.1.11  root      459: 
                    460:                        switch (act) {
1.1.1.12  root      461:                         case CmdAct::Stay:
1.1.1.11  root      462:                                // プロンプトに留まるならここで、次行のプロンプト?
                    463:                                PrintPrompt();
                    464:                                break;
1.1.1.12  root      465:                         case CmdAct::Leave:
1.1.1.11  root      466:                                // プロンプトを抜ける
1.1.1.13  root      467:                                LeavePrompt(IsTrace());
1.1.1.11  root      468:                                break;
1.1.1.12  root      469:                         case CmdAct::Quit:
1.1.1.11  root      470:                                // アプリケーション自体を終了
1.1.1.13  root      471:                                LeavePrompt(false);
1.1.1.11  root      472:                                UIMessage::Post(UIMessage::APPEXIT);
                    473:                        }
1.1.1.14  root      474:                } else if (c < ' ' || c == 0x07f) {
1.1.1.13  root      475:                        // 他のコントロールコードはとりあえず無視
                    476:                } else {
                    477:                        // 通常文字なら、エコーバックして追加
                    478:                        fputc(c, cons);
                    479:                        fflush(cons);
                    480: 
                    481:                        cmdbuf.push_back(c);
1.1.1.3   root      482:                }
                    483:        }
1.1.1.11  root      484: }
1.1.1.3   root      485: 
1.1.1.11  root      486: // コマンドモード(プロンプト)に入る
                    487: void
                    488: Debugger::EnterPrompt()
                    489: {
                    490:        is_prompt = true;
                    491: 
                    492:        // ブレークポイント到達メッセージがあればここで表示
                    493:        if (bpointmsg.empty() == false) {
                    494:                fprintf(cons, "%s", bpointmsg.c_str());
                    495:                bpointmsg.clear();
                    496:        }
                    497: 
1.1.1.13  root      498:        // d/m をいきなり引数なしで実行した時のため、現在地にしておく。
                    499:        ResetStickyAddr();
                    500: 
1.1.1.11  root      501:        // プロンプトのたびに表示するやつ
                    502:        cmd_minus();
                    503: 
                    504:        PrintPrompt();
1.1.1.3   root      505: }
                    506: 
1.1.1.13  root      507: // コマンドモード(プロンプト)から出る。
                    508: // trace はプロンプトから抜ける際のトレース状態の指示。
                    509: // スレッド終了時は false を指定すること。
1.1.1.11  root      510: void
1.1.1.13  root      511: Debugger::LeavePrompt(bool trace)
1.1       root      512: {
1.1.1.11  root      513:        // MPU のトレース状態を変更
1.1.1.13  root      514:        scheduler->SendMessage(MessageID::MPU_TRACE_ALL, trace);
1.1.1.3   root      515: 
1.1.1.11  root      516:        is_prompt = false;
1.1.1.2   root      517: 
1.1.1.11  root      518:        // 最後に VM スレッドで待機している Exec() を起こす
                    519:        {
                    520:                std::unique_lock<std::mutex> lock(mtx);
                    521:                prompt_released = true;
                    522:                cv_prompt.notify_one();
1.1.1.2   root      523:        }
1.1.1.3   root      524: }
1.1       root      525: 
1.1.1.11  root      526: // プロンプトを表示
                    527: void
                    528: Debugger::PrintPrompt()
                    529: {
1.1.1.13  root      530:        fprintf(cons, "%s> ", curmd->GetName().c_str());
1.1.1.11  root      531:        fflush(cons);
                    532: }
                    533: 
                    534: // コマンド実行
1.1.1.12  root      535: Debugger::CmdAct
1.1.1.11  root      536: Debugger::Command()
1.1.1.3   root      537: {
1.1.1.11  root      538:        string_rtrim(cmdbuf);
1.1.1.7   root      539: 
1.1.1.11  root      540:        if (cmdbuf.empty()) {
                    541:                // 空行なら、直前のコマンドをもう一度。
                    542:                // 前行がなければ何もせずもう一度プロンプトを表示するかね。
                    543:                if (last_cmdbuf.empty()) {
1.1.1.12  root      544:                        return CmdAct::Stay;
1.1.1.3   root      545:                }
1.1.1.11  root      546:                cmdbuf = last_cmdbuf;
                    547:        } else {
                    548:                // 入力された行を次回のために保存
                    549:                last_cmdbuf = cmdbuf;
                    550:        }
1.1       root      551: 
1.1.1.11  root      552:        // 行を args に分解
                    553:        ParseCmdbuf();
                    554:        cmdbuf.clear();
1.1       root      555: 
1.1.1.11  root      556:        // 前回の値が有効なのはコマンドが連続した時だけ
                    557: 
                    558:        // コマンドをテーブルから探す
                    559:        auto it = std::find_if(cmdtable.begin(), cmdtable.end(),
                    560:                [=](auto x) { return strcmp(args[0].c_str(), x.name) == 0; });
                    561:        if (it != cmdtable.end()) {
                    562:                auto cmd = *it;
                    563: 
                    564:                // 見付かれば実行
1.1.1.12  root      565:                return (this->*(cmd.func))();
1.1.1.11  root      566:        }
                    567: 
                    568:        // (コマンドテーブルになくて) "r" から始まっていればレジスタ表示系
                    569:        if (args[0][0] == 'r') {
                    570:                // ShowRegister() は処理したら true を返す。
                    571:                // "r" 系コマンドはすべて Stay。
1.1.1.13  root      572:                if (curmd->ShowRegister(cons, args)) {
1.1.1.12  root      573:                        return CmdAct::Stay;
1.1.1.3   root      574:                }
1.1.1.11  root      575:        }
1.1.1.3   root      576: 
1.1.1.11  root      577:        // 知らないコマンドも Stay 相当。
                    578:        fprintf(cons, "%s: unknown command\n", args[0].c_str());
                    579:        // この行は次回空エンターで再発行しないでいい。
                    580:        last_cmdbuf.clear();
1.1.1.12  root      581:        return CmdAct::Stay;
1.1.1.11  root      582: }
1.1.1.3   root      583: 
1.1.1.11  root      584: // ブレークポイントとかを調べる。1命令ごとに呼び出される。
                    585: // (VM スレッドから呼ばれる)
                    586: void
1.1.1.13  root      587: Debugger::Exec(DebuggerMD *md)
1.1.1.11  root      588: {
1.1.1.13  root      589:        if (Check(md)) {
                    590:                // この CPU でブレークしたので、ターゲット CPU をこっちに変更。
                    591:                ChangeMD(md);
                    592: 
1.1.1.11  root      593:                // 実時間を停止
1.1.1.13  root      594:                syncer->StopRealTime();
1.1.1.3   root      595: 
1.1.1.11  root      596:                // 待機
                    597:                {
                    598:                        std::unique_lock<std::mutex> lock(mtx);
1.1.1.3   root      599: 
1.1.1.11  root      600:                        // notify 前にクリア
                    601:                        prompt_released = false;
1.1.1.3   root      602: 
1.1.1.11  root      603:                        // MPU が一時停止したことをデバッガスレッドへ通知する
                    604:                        request |= REQUEST_PROMPT;
                    605:                        cv_request.notify_one();
1.1       root      606: 
1.1.1.11  root      607:                        // デバッガスレッドがプロンプト解放するまで待機
                    608:                        cv_prompt.wait(lock, [&] { return prompt_released; });
1.1.1.3   root      609:                }
1.1.1.2   root      610: 
1.1.1.11  root      611:                // 実時間を再開
1.1.1.13  root      612:                syncer->StartRealTime();
1.1.1.11  root      613:        } else {
                    614:                // t (トレース表示) が有効な場合は終了条件にマッチしなかったここで
                    615:                // レジスタを表示。終了条件にマッチした時は EnterPrompt() で表示する。
1.1.1.13  root      616:                if (t_enable == md) {
                    617:                        assert(md == curmd);
                    618:                        pc = curmd->GetPC();
1.1.1.11  root      619:                        cmd_minus();
                    620:                        // 次回との差分のため今のレジスタセットをバックアップ
1.1.1.13  root      621:                        curmd->BackupRegs();
1.1.1.11  root      622:                }
1.1.1.3   root      623:        }
1.1       root      624: }
                    625: 
1.1.1.11  root      626: // MPU をトレース状態にすべきなら true を返す。
1.1       root      627: bool
1.1.1.11  root      628: Debugger::IsTrace() const
1.1       root      629: {
1.1.1.11  root      630:        // 有効なブレークポイントがあれば MPU をトレースにする
                    631:        for (const auto& bp : bpoint) {
                    632:                if (bp.type != BreakpointType::Unused) {
                    633:                        return true;
                    634:                }
                    635:        }
                    636: 
                    637:        // この辺のどれかがあれば MPU をトレースにする
1.1.1.13  root      638:        return is_pause || (step_type != StepType::None);
1.1       root      639: }
                    640: 
1.1.1.11  root      641: // デバッガ実行中なら命令開始前に VM スレッドから呼ばれる。
1.1       root      642: // プロンプトに降りるなら true を返す。
                    643: bool
1.1.1.13  root      644: Debugger::Check(DebuggerMD *md)
1.1       root      645: {
1.1.1.5   root      646:        bool is_break = false;
                    647: 
                    648:        // ブレークポイントは他のチェックとは併用になるので先に調べる。
1.1.1.13  root      649:        if (CheckAllBreakpoints(md)) {
1.1.1.5   root      650:                is_break = true;
1.1       root      651: 
1.1.1.13  root      652:                // この次に調べる各種終了条件が来ないうちに先にブレークポイントに
                    653:                // 到達した場合でも、ブレークポイントによりプロンプトに降りる
                    654:                // わけなので、実行中のステップ実行をキャンセルする。
                    655:                // この場合もブレークポイント側が true なので true を返せばよい。
                    656:                step_type = StepType::None;
                    657:                step_md = NULL;
                    658:        }
                    659: 
                    660:        // ステップ実行系は、ターゲット CPU 側でだけ判定を行い、
                    661:        // いずれの場合も終了条件にマッチしたらブレークポイントの成否に関わらず
                    662:        // true を返せばいい。
                    663:        // なお、トレース表示に関してはここではなく Exec() 側でやってある。
                    664: 
                    665:        if (step_md == md) {
                    666:                bool hit = false;
                    667:                switch (step_type) {
                    668:                 case StepType::None:
                    669:                        break;
1.1.1.5   root      670: 
1.1.1.13  root      671:                 case StepType::Count:          // 命令数指定
                    672:                        step_count--;
                    673:                        putlog(1, "Check s: --step_count=%d", step_count);
                    674:                        if (step_count == 0) {
                    675:                                hit = true;
                    676:                        }
                    677:                        break;
1.1.1.5   root      678: 
1.1.1.13  root      679:                 case StepType::CountSkipSub:   // 命令数指定 (サブルーチンをスキップ)
                    680:                        if ((int64)step_addr >= 0) {
                    681:                                // アドレスが指定されていれば、ステップインをスキップ中
                    682:                                if (step_addr == step_md->GetPC()) {
                    683:                                        step_count--;
                    684:                                        step_addr = (uint64)-1;
                    685:                                }
                    686:                        } else {
                    687:                                step_count--;
                    688:                        }
                    689:                        if (loglevel >= 1) {
                    690:                                if ((int64)step_addr < 0) {
                    691:                                        putlogn("Check n: --step_count=%d", step_count);
                    692:                                } else {
                    693:                                        putlogn("Check n: step_addr=$%08x", (uint32)step_addr);
                    694:                                }
                    695:                        }
                    696:                        if (step_count == 0 || is_break/*?*/) {
                    697:                                hit = true;
                    698:                        } else {
                    699:                                // スキップ中でなければ、ステップインが起きるか都度調べる。
                    700:                                // すでにスキップ中なら到達するまでは何もしない。
                    701:                                if ((int64)step_addr < 0) {
                    702:                                        SetNBreakpoint();
                    703:                                }
                    704:                        }
                    705:                        break;
1.1.1.5   root      706: 
1.1.1.13  root      707:                 case StepType::StepOut:        // ステップアウト
                    708:                        putlog(1, "Check so");
                    709:                        if (step_md->IsStepOut()) {
                    710:                                hit = true;
                    711:                        }
                    712:                        break;
1.1       root      713: 
1.1.1.13  root      714:                 case StepType::Addr:           // アドレス指定
                    715:                        putlog(1, "Check c: step_addr=$%08x", (uint32)step_addr);
                    716:                        if (step_addr == step_md->GetPC()) {
                    717:                                hit = true;
                    718:                        }
                    719:                        break;
1.1       root      720: 
1.1.1.13  root      721:                 case StepType::Time:           // 時間指定
                    722:                        putlog(1, "Check ct");
                    723:                        if (scheduler->GetVirtTime() >= step_time) {
                    724:                                hit = true;
                    725: 
                    726:                                // 時間到達は他のと比べて分かりづらいので、
                    727:                                // ブレークポイントメッセージに便乗して表示(?)
                    728:                                bpointmsg += string_format("%s has passed.\n",
                    729:                                        TimeToStr(ct_timespan).c_str());
1.1.1.5   root      730:                        }
1.1.1.13  root      731:                        break;
                    732: 
                    733:                 default:
                    734:                        PANIC("StepType %d not supported\n", (int)step_type);
1.1.1.5   root      735:                }
1.1.1.13  root      736: 
                    737:                if (hit) {
1.1.1.11  root      738:                        is_break = true;
1.1.1.13  root      739:                        step_type = StepType::None;
                    740:                        step_md = NULL;
                    741:                        t_enable = NULL;
1.1.1.5   root      742:                }
1.1.1.11  root      743:        }
1.1.1.5   root      744: 
1.1.1.11  root      745:        // デバッガから MPU の一時停止が要求されているか
                    746:        if (is_pause) {
                    747:                is_pause = false;
                    748:                is_break = true;
1.1       root      749:        }
                    750: 
1.1.1.5   root      751:        return is_break;
1.1       root      752: }
                    753: 
1.1.1.13  root      754: // md で指定された CPU のブレークポイントがどれかでも成立するかを調べる。
                    755: // 1つ以上成立してブレークするなら、true を返す。
1.1.1.4   root      756: // 1つも成立しておらずブレークしないなら false を返す。
1.1.1.11  root      757: // このルーチンから cons への出力は使わないこと。(プロンプトにいない時でも
                    758: // 呼ばれるので)
1.1.1.4   root      759: bool
1.1.1.13  root      760: Debugger::CheckAllBreakpoints(DebuggerMD *md)
1.1.1.4   root      761: {
                    762:        bool is_break = false;
1.1.1.13  root      763:        int vector;
1.1.1.4   root      764: 
                    765:        // 命令ごとにクリアする
                    766:        bi_inst = 0;
                    767:        bi_inst_bytes = 0;
                    768: 
1.1.1.13  root      769:        // 例外はここでローカルにコピーしてから、クリアする。
                    770:        // 厳密には atomic exchange すべきのような。
                    771:        vector = md->bv_vector;
                    772:        md->bv_vector = -1;
                    773: 
1.1.1.4   root      774:        // 1つの条件でマッチしても(そこでブレークすること自体は確定するのだが)
                    775:        // 残りの他の条件も成立すればカウントを進める必要があるため、
                    776:        // 全部処理した上でどれか一つでもブレークしたかで判断する必要がある。
1.1.1.5   root      777:        for (int i = 0, end = bpoint.size(); i < end; i++) {
                    778:                auto& bp = bpoint[i];
                    779: 
1.1.1.13  root      780:                if (bp.md != md) {
                    781:                        continue;
                    782:                }
                    783: 
1.1.1.4   root      784:                switch (bp.type) {
                    785:                 case BreakpointType::Address:
1.1.1.13  root      786:                        // 通常動作中でアドレスが一致すればマッチ
                    787:                        if (bp.addr == bp.md->GetPC() &&
                    788:                                bp.md->GetCPUState() == CPUState::Normal)
                    789:                        {
                    790:                                break;
1.1.1.4   root      791:                        }
1.1.1.13  root      792:                        continue;
1.1.1.4   root      793: 
                    794:                 case BreakpointType::Memory:
1.1.1.13  root      795:                        if (bp.md->CheckLEA(bp.addr)) {
                    796:                                break;
1.1.1.4   root      797:                        }
1.1.1.13  root      798:                        continue;
1.1.1.4   root      799: 
                    800:                 case BreakpointType::Exception:
1.1.1.13  root      801:                        if (vector >= 0) {
                    802:                                if (bp.vec1 <= vector && vector <= bp.vec2) {
1.1.1.4   root      803:                                        break;
                    804:                                }
                    805:                        }
                    806:                        continue;
                    807: 
                    808:                 case BreakpointType::Instruction:
1.1.1.13  root      809:                        if (CheckBreakpointInst(bp)) {
                    810:                                break;
1.1.1.4   root      811:                        }
1.1.1.13  root      812:                        continue;
1.1.1.4   root      813: 
                    814:                 default:
                    815:                        continue;
                    816:                }
                    817: 
                    818:                // 条件は成立したのでカウントとスキップ
                    819:                bp.matched++;
                    820: 
                    821:                // skip == -1 は常にブレークしない (カウントするだけ)
                    822:                if (bp.skip < 0) {
                    823:                        continue;
                    824:                }
                    825: 
                    826:                // skip が 1 以上なら、remain を減算
                    827:                if (bp.skip > 0 && --bp.skipremain > 0) {
                    828:                        continue;
                    829:                }
                    830: 
                    831:                // ブレーク成立
                    832:                is_break = true;
1.1.1.5   root      833: 
                    834:                std::string desc;
1.1.1.13  root      835:                desc = string_format("cpu=%s ", bp.md->GetName().c_str());
1.1.1.5   root      836:                switch (bp.type) {
                    837:                 case BreakpointType::Address:
1.1.1.13  root      838:                        desc += string_format("addr=$%08x", bp.addr);
1.1.1.5   root      839:                        break;
                    840:                 case BreakpointType::Memory:
1.1.1.13  root      841:                        desc += string_format("mem=$%08x", bp.addr);
1.1.1.5   root      842:                        break;
                    843:                 case BreakpointType::Exception:
                    844:                 {
1.1.1.13  root      845:                        desc += string_format("excp=$%02x", vector);
                    846:                        const char *name;
                    847:                        if (bp.md->arch == CPUArch::HD64180) {
                    848:                                name = MPU64180Device::InterruptName[vector];
                    849:                        } else {
                    850:                                auto vectortable = pVectorTable.get();
                    851:                                name = vectortable->GetExceptionName(vector);
                    852:                        }
1.1.1.11  root      853:                        if (name) {
1.1.1.5   root      854:                                desc += string_format(" \"%s\"", name);
                    855:                        }
                    856:                        break;
                    857:                 }
                    858:                 case BreakpointType::Instruction:
1.1.1.13  root      859:                        desc += string_format("inst=%0*x",
1.1.1.5   root      860:                                bi_inst_bytes * 2,
                    861:                                bi_inst >> ((4 - bi_inst_bytes) * 8));
                    862:                        break;
                    863:                 default:
                    864:                        assert(false);
                    865:                        break;
                    866:                }
1.1.1.7   root      867: 
                    868:                // 到達メッセージを作成。
                    869:                // この時点ではまだコンソールを取得していない可能性があるので
                    870:                // (-D なしで起動した場合とか)、表示せず用意するだけ。
                    871:                // コンソールが取得できたところで表示する。
                    872:                bpointmsg += string_format("breakpoint #%d (%s) reached\n",
                    873:                        i, desc.c_str());
1.1.1.4   root      874:        }
                    875: 
                    876:        // ブレークポイントが1つでも成立したかどうかを返す
                    877:        return is_break;
                    878: }
                    879: 
                    880: // 命令ブレークポイントが成立するか調べる。
                    881: bool
                    882: Debugger::CheckBreakpointInst(breakpoint_t& bp)
                    883: {
                    884:        // uint32 bi_inst が現在の PC 位置の命令データ (左詰め)
                    885:        // bi_inst_bytes が読み込んだバイト数(bi_inst の左からの有効バイト数)。
                    886:        // bi_need_bytes が現在のブレークポイントで読み込む必要のあるバイト数。
                    887: 
1.1.1.15! root      888:        busaddr laddr(bp.md->GetPC(), busaddr::Fetch, bp.md->IsSuper());
        !           889:        DebuggerMemoryStream mem(bp.md, laddr);
1.1.1.5   root      890: 
1.1.1.4   root      891:        // 必要なバイト数に達するまで読み足す
1.1.1.5   root      892:        bi_inst = 0;
1.1.1.4   root      893:        while (bi_inst_bytes < bi_need_bytes) {
1.1.1.13  root      894:                uint64 data = mem.Read(bp.md->inst_bytes);
1.1.1.5   root      895:                if ((int64)data < 0) {
                    896:                        return false;
1.1.1.4   root      897:                }
                    898: 
1.1.1.13  root      899:                bi_inst <<= bp.md->inst_bytes * 8;
1.1.1.5   root      900:                bi_inst |= data;
1.1.1.13  root      901:                bi_inst_bytes += bp.md->inst_bytes;
1.1.1.4   root      902:        }
1.1.1.5   root      903: 
1.1.1.4   root      904:        // 左詰めにする
                    905:        bi_inst <<= (4 - bi_inst_bytes) * 8;
                    906: 
                    907:        // 必要なバイト数取得できたので比較
                    908:        if ((bi_inst & bp.mask) == bp.inst) {
                    909:                return true;
                    910:        }
                    911:        return false;
                    912: }
                    913: 
1.1.1.13  root      914: // 例外通知 (メイン CPU)
1.1.1.4   root      915: void
1.1.1.13  root      916: Debugger::NotifyExceptionMain(int vector)
1.1.1.4   root      917: {
1.1.1.13  root      918:        auto md = md_mpu.get();
                    919:        md->bv_vector = vector;
1.1.1.4   root      920: }
                    921: 
1.1.1.13  root      922: // 例外通知 (XP)。
                    923: // ベクタとして割り込み優先順位 (Intmap*) を使う。
1.1.1.4   root      924: void
1.1.1.13  root      925: Debugger::NotifyExceptionXP(int vector)
1.1.1.4   root      926: {
1.1.1.13  root      927:        auto md = md_xp.get();
                    928:        md->bv_vector = vector;
1.1.1.4   root      929: }
                    930: 
1.1       root      931: // コマンド一覧。
1.1.1.3   root      932: // "r" から始まるレジスタ表示系コマンドはコード中で別処理してある。
                    933: /*static*/ std::vector<Debugger::cmddef_t> Debugger::cmdtable = {
1.1.1.12  root      934:        { "bi",         &Debugger::cmd_bi,              },
                    935:        { "bm",         &Debugger::cmd_bm,              },
                    936:        { "bv",         &Debugger::cmd_bv,              },
                    937:        { "bx",         &Debugger::cmd_bx,              },
                    938:        { "b",          &Debugger::cmd_b,               },
                    939:        { "brhist",     &Debugger::cmd_brhist,  },
                    940:        { "c",          &Debugger::cmd_c,               },
                    941:        { "ct",         &Debugger::cmd_ct,              },
                    942:        { "d",          &Debugger::cmd_d,               },
                    943:        { "D",          &Debugger::cmd_D,               },
                    944:        { "disp",       &Debugger::cmd_disp,    },
                    945:        { "exhist",     &Debugger::cmd_exhist,  },
                    946:        { "hb",         &Debugger::cmd_hb,              },
                    947:        { "hr",         &Debugger::cmd_hr,              },
                    948:        { "h",          &Debugger::cmd_h,               },
                    949:        { "help",       &Debugger::cmd_h,               },
                    950:        { "L",          &Debugger::cmd_L,               },
                    951:        { "m",          &Debugger::cmd_m,               },
                    952:        { "M",          &Debugger::cmd_M,               },
1.1.1.13  root      953:        { "mpu",        &Debugger::cmd_mpu,             },
1.1.1.12  root      954:        { "n",          &Debugger::cmd_n,               },
                    955:        { "nt",         &Debugger::cmd_nt,              },
                    956:        { "q",          &Debugger::cmd_q,               },
                    957:        { "quit",       &Debugger::cmd_q,               },
                    958:        { "reset",      &Debugger::cmd_reset,   },
                    959:        { "s",          &Debugger::cmd_s,               },
                    960:        { "st",         &Debugger::cmd_st,              },
                    961:        { "so",         &Debugger::cmd_so,              },
                    962:        { "sot",        &Debugger::cmd_sot,             },
                    963:        { "show",       &Debugger::cmd_show,    },
                    964:        { "t",          &Debugger::cmd_t,               },
1.1.1.13  root      965:        { "xp",         &Debugger::cmd_xp,              },
                    966:        { "z",          &Debugger::cmd_z,               },
1.1.1.12  root      967:        { "-",          &Debugger::cmd_minus,   },
1.1       root      968: };
                    969: 
                    970: // ヘルプ
1.1.1.5   root      971: // h       : 一覧を表示
                    972: // h <cmd> : 単独コマンドの詳細を表示
1.1.1.12  root      973: Debugger::CmdAct
1.1       root      974: Debugger::cmd_h()
                    975: {
1.1.1.5   root      976:        if (args.size() < 2) {
                    977:                // 引数なしなら一覧表示。
1.1.1.6   root      978:                ShowHelpList(HelpListMain);
1.1.1.12  root      979:                return CmdAct::Stay;
1.1.1.3   root      980:        }
1.1.1.5   root      981: 
                    982:        // 引数があれば個別の詳細
1.1.1.13  root      983:        const std::string cmd1 = args[1];
                    984: 
                    985:        // curmd の MD 分も併せて検索するため、ローカルに map を作成する。
                    986:        // value のほうは実体コピーは不要なのでポインタ。
                    987:        std::map<const std::string, const std::string *> helpmap;
                    988:        for (const auto& pair : HelpDetails) {
                    989:                helpmap.insert(std::make_pair(pair.first, &pair.second));
                    990:        }
                    991:        for (const auto& pair : curmd->GetHelpReg()) {
                    992:                helpmap.insert(std::make_pair(pair.first, &pair.second));
                    993:        }
                    994: 
                    995:        // 検索
                    996:        const auto it1 = helpmap.find(cmd1);
                    997:        if (it1 == helpmap.end()) {
                    998:                fprintf(cons, "invalid command name: %s\n", cmd1.c_str());
                    999:                return CmdAct::Stay;
                   1000:        }
                   1001:        const auto& msg1 = *(it1->second);
                   1002: 
                   1003:        const std::string *msg;
                   1004:        if (msg1[0] != '=') {
                   1005:                // メッセージ本文が "=" から始まってなければこれを採用。
                   1006:                msg = &msg1;
                   1007:        } else {
                   1008:                // メッセージ本文が "=<cmd>" の形式なら、<cmd> を探し直す。
                   1009:                // 別名で同じヘルプを指すシンボリックリンクみたいなもの。
                   1010:                std::string cmd2 = msg1.substr(1);
                   1011: 
                   1012:                // 再検索 (こっちは見付からないはずはない)
                   1013:                const auto it2 = helpmap.find(cmd2);
                   1014:                if (it2 == helpmap.end()) {
                   1015:                        fprintf(cons, "Warning: '%s' in '%s' not found\n",
                   1016:                                msg1.c_str(), cmd1.c_str());
                   1017:                        return CmdAct::Stay;
1.1.1.5   root     1018:                }
1.1.1.13  root     1019:                const auto& msg2 = *(it2->second);
                   1020:                msg = &msg2;
                   1021:        }
                   1022: 
                   1023:        std::string disp = HelpConvert(*msg);
                   1024:        fprintf(cons, "%s", disp.c_str());
1.1.1.12  root     1025:        return CmdAct::Stay;
1.1.1.3   root     1026: }
                   1027: 
1.1.1.5   root     1028: // hb : ブレークポイント系コマンドの一覧を表示
                   1029: // こいつだけ結構占めるので別階層。
1.1.1.12  root     1030: Debugger::CmdAct
1.1.1.5   root     1031: Debugger::cmd_hb()
                   1032: {
1.1.1.12  root     1033:        return ShowHelpList(HelpListBreakpoints);
1.1.1.5   root     1034: }
                   1035: 
                   1036: // hr : レジスタ表示系コマンドの一覧を表示
                   1037: // CPU ごとに違うので。
1.1.1.12  root     1038: Debugger::CmdAct
1.1.1.5   root     1039: Debugger::cmd_hr()
                   1040: {
1.1.1.13  root     1041:        return ShowHelpList(curmd->GetHelpListReg());
1.1.1.5   root     1042: }
1.1.1.3   root     1043: 
1.1.1.6   root     1044: // ヘルプ一覧を表示。
1.1.1.12  root     1045: Debugger::CmdAct
1.1.1.6   root     1046: Debugger::ShowHelpList(const HelpMessages& msgs)
1.1.1.3   root     1047: {
1.1.1.11  root     1048:        fprintf(cons, "Type \"help <cmd>\" for indivisual details.\n");
1.1.1.3   root     1049:        for (const auto& pair : msgs) {
1.1.1.11  root     1050:                fprintf(cons, " %-16s %s\n", pair.first.c_str(), pair.second.c_str());
1.1       root     1051:        }
1.1.1.12  root     1052:        return CmdAct::Stay;
1.1       root     1053: }
                   1054: 
1.1.1.5   root     1055: // 個別ヘルプメッセージを出力用に置換。
                   1056: // 1. 行頭の連続する改行 (通常は1つのはず) を取り除く。
                   1057: // 2. 末尾の連続するタブ (通常は1つのはず) を取り除く。
                   1058: // 3. (各行頭の) タブを空白4つに置換する。
                   1059: std::string
                   1060: Debugger::HelpConvert(const std::string& src)
                   1061: {
                   1062:        int start = 0;
                   1063:        int len = src.size();
                   1064: 
                   1065:        // 末尾の連続するタブをカウント
                   1066:        while (src[len - 1] == '\t') {
                   1067:                len--;
                   1068:        }
                   1069:        // 先頭の連続する改行をカウント
                   1070:        while (src[start] == '\n') {
                   1071:                start++;
                   1072:                len--;
                   1073:        }
                   1074:        std::string stripped = src.substr(start, len);
                   1075: 
                   1076:        // 面倒なので行頭に関わらず全タブを置換
                   1077:        const char *c_stripped = stripped.c_str();
                   1078:        std::string dst;
                   1079:        for (const char *s = c_stripped; *s; s++) {
                   1080:                if (*s == '\t') {
                   1081:                        dst.append("    ");
                   1082:                } else {
                   1083:                        dst.append(1, *s);
                   1084:                }
                   1085:        }
                   1086:        return dst;
                   1087: }
                   1088: 
                   1089: /*static*/ const HelpMessages
1.1.1.6   root     1090: Debugger::HelpListMain = {
1.1.1.5   root     1091:        { "b*",         "Set/show Breakpoints (Type \"hb\" for details)" },
                   1092:        { "brhist",     "Show branch history" },
1.1.1.12  root     1093:        { "c/ct",       "Continue" },
1.1.1.5   root     1094:        { "d/dt/D",     "Disassemble" },
                   1095:        { "disp",       "Set register group to show" },
                   1096:        { "exhist",     "Show exception history" },
                   1097:        { "help",       "Show this message" },
                   1098:        { "L",          "Set log level" },
                   1099:        { "m/mt/M",     "Memory dump" },
                   1100:        { "n/nt",       "Step until next instruction (Skip subroutines)" },
                   1101:        { "quit",       "Quit" },
                   1102:        { "r*",         "Show registers (Type \"hr\" for details)" },
                   1103:        { "reset",      "Reset the VM" },
                   1104:        { "s/st",       "Step an instruction (Step in)" },
                   1105:        { "so",         "Step out" },
                   1106:        { "show",       "Show monitor" },
                   1107: };
                   1108: 
                   1109: /*static*/ const HelpMessages
1.1.1.6   root     1110: Debugger::HelpListBreakpoints = {
1.1.1.5   root     1111:        { "b",          "Show all breakpoints" },
                   1112:        { "b arg..","Set/Delete breakpoint" },
                   1113:        { "bm",         "Set memory breakpoint" },
                   1114:        { "bi",         "Set instruction breakpoint" },
                   1115:        { "bv",         "Set exception breakpoint" },
                   1116:        { "bx",         "Delete all breakpoints" },
                   1117: };
                   1118: 
                   1119: // 各コマンドの詳細。"コマンド名" => "説明文" の形式。
                   1120: // 説明文は C+11 の生文字リテラル機能を使って R"**( )**" で囲む。
                   1121: // 1つのエントリは
                   1122: //      { "cmdname", R"**(
                   1123: //      <tab>cmdname [argument...]
                   1124: //
                   1125: //      <tab>Description...
                   1126: //      <tab>)**" },
                   1127: // のようになる。説明文本文はインデント1つ(TAB 幅 4) で字下げした状態で
                   1128: // 80桁以内に収めること。出力の際にタブを空白4つに置き換えることでソース上
                   1129: // での見た目と実際の出力とを揃えてある。
                   1130: // またソースコード上の見栄えの問題として、文字列の開始記号、終了記号を本文
                   1131: // とは別の行に書いているため、オブジェクトとしての文字列には先頭に改行、
                   1132: // 末尾にタブが余計に入っているが、これは表示の際にコードで取り除く。
                   1133: //
                   1134: // 説明文の書き方は、まずコマンド書式を1行ずつ書く。
                   1135: // コマンド書式と本文との間は1行あける。
                   1136: /*static*/ const HelpMessages
                   1137: Debugger::HelpDetails = {
                   1138:        //-----
                   1139:        { "b", R"**(
                   1140:        Command: b
1.1.1.13  root     1141:        Command: b [<cpu>,]<address> [<skipcount>]
1.1.1.5   root     1142:        Command: b #n
                   1143: 
                   1144:        The first form (with no arguments) shows all breakpoints.
1.1.1.13  root     1145:        The second form sets a new breakpoint at <address> on <cpu>.
                   1146:        If <cpu> is omitted, the current cpu is used.
1.1.1.5   root     1147:        XXX skipcount
                   1148:        If <skipcount> is -1, it will never match.  It's useful to count the
                   1149:        number of times you have passed this address.
                   1150:        The third form deletes a breakpoint specified by number.
                   1151:        )**" },
                   1152: 
                   1153:        //-----
                   1154:        { "bm", R"**(
1.1.1.13  root     1155:        Command: bm [<cpu>,]<address> [<skipcount>]
1.1.1.5   root     1156: 
1.1.1.13  root     1157:        Sets a memory breakpoint at <address> on <cpu>.
                   1158:        If <cpu> is omitted, the current cpu is used.
1.1.1.5   root     1159:        XXX skipcount
                   1160:        If <skipcount> is -1, it will never match.  It's useful to count the
                   1161:        number of times you have passed this address.
                   1162:        )**" },
                   1163: 
                   1164:        //-----
                   1165:        { "bi", R"**(
1.1.1.13  root     1166:        Command: bi [<cpu>,]<inst>[:<mask>] [<skipcount>]
1.1.1.5   root     1167: 
1.1.1.13  root     1168:        Sets an instruction breakpoint on <cpu>.  <inst> must be 16 bits or 32
                   1169:        bits on m68k and must be 32 bits on m88k.  <mask> can specify the mask.
                   1170:        Its length must be the same as <inst>.  If the <mask> is omitted, all
                   1171:        bits of <inst> is used to compare.
1.1.1.5   root     1172:        For example,
                   1173:        "bi 4e75" on m68k will stop at 'rts' instruction.
                   1174:        "bi 70004e4f:fff0ffff" on m68k will stop at any of 'IOCS #0'..'IOCS #15'.
                   1175: 
1.1.1.13  root     1176:        If <cpu> is omitted, the current cpu is used.
1.1.1.5   root     1177:        XXX skipcount
                   1178:        If <skipcount> is -1, it will never match.  It's useful to count the
                   1179:        number of times you have passed this address.
                   1180:        )**" },
                   1181: 
                   1182:        //-----
                   1183:        { "bv", R"**(
1.1.1.13  root     1184:        Command: bv [<cpu>,]<vector>[-<vector2>] [<skipcount>]
1.1.1.5   root     1185: 
1.1.1.13  root     1186:        Sets an exception breakpoint on <cpu>.  <vector> must be specified in hex.
1.1.1.5   root     1187:        <vector> ranges from 0 to ff (255 in decimal) on m68k, and 0 to 1ff (511
                   1188:        in decimal) on m88k.  If <vector2> is specified, it matches the range
                   1189:        from <vector> to <vector2> (including themselves).
                   1190:        For example, "bv 1-1ff" on m88k means all exceptions but reset.
1.1.1.13  root     1191: 
                   1192:        If <cpu> is omitted, the current cpu is used.
1.1.1.5   root     1193:        XXX skipcount
                   1194:        If <skipcount> is -1, it will never match.  It's useful to count the
                   1195:        number of times you have passed this address.
                   1196:        )**" },
                   1197: 
                   1198:        //-----
                   1199:        { "bx", R"**(
                   1200:        Command: bx
                   1201: 
                   1202:        Deletes all breakpoints.
                   1203:        )**" },
                   1204: 
                   1205:        //-----
                   1206:        { "brhist", R"**(
                   1207:        Command: brhist [<maxlines>]
                   1208: 
                   1209:        Show branch history.  <maxlines> specifies the maximum number of lines
                   1210:        to display.  If <maxlines> is negative value, sort in reverse order.
                   1211:        )**" },
                   1212: 
                   1213:        //-----
                   1214:        { "c", R"**(
                   1215:        Command: c [<address>]
                   1216: 
                   1217:        Continue (until <address> if specified).
                   1218:        )**" },
                   1219: 
                   1220:        //-----
1.1.1.12  root     1221:        { "ct", R"**(
                   1222:        Command: ct [<timespan>]
                   1223: 
                   1224:        Continue until specified <timespan> has elapsed.  If the <timespan> is
1.1.1.13  root     1225:        omitted, the last value is used again.  The unit of <timespan> is
1.1.1.12  root     1226:        seconds in default.  You can use "msec", "usec", or "nsec" suffix.
                   1227:        )**" },
                   1228: 
                   1229:        //-----
1.1.1.5   root     1230:        { "d", R"**(
                   1231:        Command: D  <address>] [<lines>]
1.1.1.6   root     1232:        Command: d  [[<space>:]<address>] [<lines>]
1.1.1.5   root     1233: 
                   1234:        Shows disassemble.
                   1235:        The first form ("D") interprets <address> as a physical address.
1.1.1.15! root     1236:        The second form ("d") interprets <address> as a logical address
        !          1237:        if the address translation is enabled.  Normally, <address> is
1.1.1.6   root     1238:        interpreted in the current privilege and current address space.  You can
                   1239:        change it by <space> modifier.
                   1240:        On m68k, <space> can be specified either by function code number directly
                   1241:        ('1', '2', '5', and '6) or by one or more following modifiers:
                   1242:          's' .. Supervisor mode        'u'        .. User mode
                   1243:          'd' .. Data space             'p' or 'i' .. Program space
                   1244:        On m88k, <space> can be specified by using combination of privilege
                   1245:        modifier ('s' or 'u', same as above) and following CMMU identifiers:
                   1246:          'd' .. Data CMMU              'i' or 'p' .. Instruction CMMU
                   1247:        Or CMMU can also be identified by CMMU number (like '6' or '7').
1.1.1.5   root     1248:        )**" },
                   1249:        { "D",  "=d" },
                   1250: 
                   1251:        //-----
                   1252:        { "disp", R"**(
                   1253:        Command: disp <register...>
                   1254: 
                   1255:        Sets the registers list to be shown in the trace.
                   1256:        <registers...> is comma-separated list and each of which is a command
                   1257:        name that shows registers. See "hr".
                   1258:        The default is "r" and thus it only shows general registers in the trace.
                   1259:        For example, if you set "disp r,rf", it will show general registers and
                   1260:        FPU registers in every trace.
                   1261:        )**" },
                   1262: 
                   1263:        //-----
                   1264:        { "exhist", R"**(
                   1265:        Command: exhist [<maxlines>]
                   1266: 
                   1267:        Show exception history.  <maxlines> specifies the maximum number of lines
                   1268:        to display.  If <maxlines> is negative value, sort in reverse order.
                   1269:        )**" },
                   1270: 
                   1271:        //-----
                   1272:        { "help", R"**(
                   1273:        Command: help [<command>]
                   1274:        Command: hb
                   1275:        Command: hr
                   1276: 
                   1277:        The "help" command without arguments shows the list of commands.
                   1278:        The "help" command with an argument shows <command>'s detailed help.
                   1279:        "h" is a synonym of "help".
                   1280:        The "hb" command shows the list of breakpoint-related commands.
                   1281:        The "hr" command shows the list of register-related commands.
                   1282:        )**" },
                   1283:        { "h",  "=help" },
                   1284:        { "hb", "=help" },
                   1285:        { "hr", "=help" },
                   1286: 
                   1287:        //-----
                   1288:        { "L", R"**(
1.1.1.9   root     1289:        Command: L <name1>[=<level1>][,<name2>=[<level2>]]...
1.1.1.5   root     1290: 
                   1291:        Set loglevel. XXX To be written...
                   1292:        )**" },
                   1293: 
                   1294:        //-----
                   1295:        { "m", R"**(
                   1296:        Command: M  <address>] [<lines>]
1.1.1.6   root     1297:        Command: m  [[<space>:]<address>] [<lines>]
1.1.1.5   root     1298: 
                   1299:        Shows memory dump.
                   1300:        The first form ("M") interprets <address> as a physical address.
1.1.1.15! root     1301:        The second form ("m") interprets <address> as a logical address
        !          1302:        if the address translation is enabled.  Normally, <address> is
1.1.1.6   root     1303:        interpreted in the current privilege and current address space.  You can
                   1304:        change it by <space> modifier.
                   1305:        On m68k, <space> can be specified either by function code number directly
                   1306:        ('1', '2', '5', and '6) or by one or more following modifiers:
                   1307:          's' .. Supervisor mode        'u'        .. User mode
                   1308:          'd' .. Data space             'p' or 'i' .. Program space
                   1309:        On m88k, <space> can be specified by using combination of privilege
                   1310:        modifier ('s' or 'u', same as above) and following CMMU identifiers:
                   1311:          'd' .. Data CMMU              'i' or 'p' .. Instruction CMMU
                   1312:        Or CMMU can also be identified by CMMU number (like '6' or '7').
1.1.1.5   root     1313:        )**" },
                   1314:        { "M",  "=m" },
                   1315: 
                   1316:        //-----
1.1.1.13  root     1317:        { "mpu", R"**(
                   1318:        Command: mpu
                   1319: 
                   1320:        Change the target CPU to "mpu"(M68030/M88100).
                   1321:        )**" },
                   1322: 
                   1323:        //-----
1.1.1.5   root     1324:        { "n", R"**(
                   1325:        Command: n  [<count>]
                   1326:        Command: nt [<count>]
                   1327: 
                   1328:        Step one (or <count>) instructions.  Unlike "s" command, "n" skips
1.1.1.13  root     1329:        subroutine (and repeat instruction like as LDIR in HD64*180).
1.1.1.5   root     1330:        If "t" is suffixed, it shows a trace for each instruction (including
                   1331:        while skipping).
                   1332:        )**" },
                   1333:        { "nt", "=n" },
                   1334: 
                   1335:        //-----
                   1336:        { "quit", R"**(
                   1337:        Command: quit (or q)
                   1338: 
                   1339:        Quit the debugger console.  This continues the execution, as same as "c".
                   1340:        )**" },
                   1341:        { "q", "=quit" },
                   1342: 
                   1343:        //-----
                   1344:        { "reset", R"**(
                   1345:        Command: reset
                   1346: 
                   1347:        Reset the VM.  Currently this does the hardware reset.
                   1348:        )**" },
                   1349: 
                   1350:        //-----
                   1351:        { "s", R"**(
                   1352:        Command: s  [<count>]
                   1353:        Command: st [<count>]
                   1354: 
                   1355:        Step one (or <count>) instructions.  Unlike "n" command, "s" steps in
                   1356:        subroutine.
                   1357:        If "t" is suffixed, it shows a trace for each instruction.
                   1358: 
                   1359:        "t" command is a synonym of "st" for backward compatibility.
                   1360:        )**" },
                   1361:        { "st", "=s" },
                   1362: 
                   1363:        //-----
                   1364:        { "so", R"**(
                   1365:        Command: so
                   1366:        Command: sot
                   1367: 
                   1368:        Step out this sub routine.
                   1369:        If "t" is suffixed, it shows a trace for each instruction.
                   1370:        )**" },
                   1371: 
                   1372:        //-----
                   1373:        { "show", R"**(
                   1374:        Command: show <monitor>
                   1375: 
                   1376:        Shows the specified monitor.
                   1377:        )**" },
                   1378: 
                   1379:        //-----
                   1380:        { "t", "=s" },
1.1.1.13  root     1381: 
                   1382:        //-----
                   1383:        { "xp", R"**(
                   1384:        Command: xp
                   1385: 
                   1386:        Change the target CPU to "xp"(HD647180).
                   1387:        )**" },
                   1388: 
                   1389:        //-----
                   1390:        { "z", R"**(
                   1391:        Command: z
                   1392: 
                   1393:        Continue until the next address.
                   1394:        )**" },
1.1.1.5   root     1395: };
                   1396: 
1.1.1.3   root     1397: // cmdbuf を args... に分解する。
1.1       root     1398: void
1.1.1.3   root     1399: Debugger::ParseCmdbuf()
1.1       root     1400: {
1.1.1.3   root     1401:        int pos;
                   1402:        int start;
                   1403: 
                   1404:        pos = 0;
                   1405:        args.clear();
1.1       root     1406: 
                   1407:        // 引数を空白で分解
1.1.1.3   root     1408:        for (;;) {
                   1409:                // 先頭の空白はスキップ
                   1410:                for (; cmdbuf[pos] != '\0'; pos++) {
                   1411:                        if (!isspace((unsigned int)cmdbuf[pos]))
                   1412:                                break;
                   1413:                }
                   1414:                if (cmdbuf[pos] == '\0')
1.1       root     1415:                        break;
                   1416: 
1.1.1.3   root     1417:                // 終わりを探す
                   1418:                start = pos;
                   1419:                for (; cmdbuf[pos] != '\0'; pos++) {
                   1420:                        if (isspace((unsigned int)cmdbuf[pos]))
1.1       root     1421:                                break;
                   1422:                }
1.1.1.3   root     1423:                args.push_back(cmdbuf.substr(start, pos - start));
1.1.1.2   root     1424:        }
                   1425:        // デバッグ表示
                   1426:        if (0) {
1.1.1.3   root     1427:                for (int i = 0 ; i < args.size(); i++) {
                   1428:                        printf("args[%d]=|%s|\n", i, args[i].c_str());
1.1.1.2   root     1429:                }
                   1430:        }
1.1       root     1431: }
                   1432: 
                   1433: //
                   1434: // デバッガコマンド
                   1435: //
                   1436: 
                   1437: // ブレークポイント
1.1.1.13  root     1438: // b                         ... 一覧表示
                   1439: // b #n                      ... #n を削除
                   1440: // b [<cpu>,]<addr> [<skip>] ... 設定
1.1.1.12  root     1441: Debugger::CmdAct
1.1       root     1442: Debugger::cmd_b()
                   1443: {
                   1444:        // 引数なしなら一覧表示
1.1.1.3   root     1445:        if (args.size() < 2) {
1.1       root     1446:                return cmd_b_list();
                   1447:        }
                   1448: 
                   1449:        // 引数取得
1.1.1.3   root     1450:        if (args[1][0] == '#') {
1.1       root     1451:                // #<n> 形式なら、指定番号のブレークポイントを削除
1.1.1.10  root     1452:                cmd_b_delete();
1.1.1.12  root     1453:                return CmdAct::Stay;
1.1       root     1454:        }
                   1455: 
1.1.1.4   root     1456:        return cmd_b_set(BreakpointType::Address);
                   1457: }
                   1458: 
                   1459: // メモリブレークポイントの設定
1.1.1.13  root     1460: // bm [<cpu>,]<addr> [<skip>]
1.1.1.12  root     1461: Debugger::CmdAct
1.1.1.4   root     1462: Debugger::cmd_bm()
                   1463: {
1.1.1.13  root     1464:        // XXX m88k のみ未サポート
                   1465:        if (curmd->arch != CPUArch::M88xx0) {
1.1.1.11  root     1466:                fprintf(cons, "bm not supported yet on m68k\n");
1.1.1.12  root     1467:                return CmdAct::Stay;
1.1.1.4   root     1468:        }
1.1.1.12  root     1469:        return cmd_b_set(BreakpointType::Memory);
1.1.1.4   root     1470: }
                   1471: 
                   1472: // type が違うだけの各種ブレークポイント設定の共通部分。
1.1.1.12  root     1473: Debugger::CmdAct
1.1.1.4   root     1474: Debugger::cmd_b_set(BreakpointType type)
                   1475: {
                   1476:        breakpoint_t bp;
1.1.1.13  root     1477:        std::string cpustr;
                   1478:        std::string addrstr;
1.1.1.4   root     1479: 
                   1480:        if (args.size() < 2) {
1.1.1.13  root     1481:                fprintf(cons, "usage: %s [<cpu>,]<addr> [<skipcount>]\n",
                   1482:                        args[0].c_str());
1.1.1.12  root     1483:                return CmdAct::Stay;
1.1.1.4   root     1484:        }
                   1485: 
1.1.1.13  root     1486:        // まず CPU を分離
                   1487:        auto pos = args[1].find(',');
                   1488:        if (pos == std::string::npos) {
                   1489:                // CPU 指定なし
                   1490:                addrstr = args[1];
                   1491:        } else {
                   1492:                // CPU 指定あり
                   1493:                cpustr = args[1].substr(0, pos);
                   1494:                addrstr = args[1].substr(pos + 1);
                   1495:        }
                   1496: 
1.1.1.4   root     1497:        // アドレス
1.1.1.13  root     1498:        if (!ParseAddr(addrstr.c_str(), &bp.addr)) {
1.1.1.12  root     1499:                return CmdAct::Stay;
1.1.1.4   root     1500:        }
                   1501:        // あればスキップカウント
                   1502:        if (args.size() > 2) {
                   1503:                bp.skip = atoi(args[2].c_str());
                   1504:        }
                   1505: 
                   1506:        // 空いてるところにセット
                   1507:        // (よく似たエントリがあっても干渉しない)
                   1508:        bp.type = type;
1.1.1.13  root     1509:        int bi = AddBreakpoint(bp, cpustr);
1.1.1.4   root     1510:        if (bi == -1) {
1.1.1.11  root     1511:                fprintf(cons, "no free breakpoints\n");
1.1       root     1512:        } else {
1.1.1.11  root     1513:                fprintf(cons, "breakpoint #%d added\n", bi);
1.1.1.3   root     1514:        }
1.1.1.12  root     1515:        return CmdAct::Stay;
1.1.1.4   root     1516: }
                   1517: 
1.1.1.10  root     1518: // ブレークポイント個別削除
                   1519: // (引数の先頭が '#' なことを判定したところで呼ばれる)
                   1520: // b #<n>
1.1.1.12  root     1521: Debugger::CmdAct
1.1.1.10  root     1522: Debugger::cmd_b_delete()
                   1523: {
                   1524:        int n = atoi(&args[1][1]);
                   1525:        if (n < 0 || n >= bpoint.size()) {
1.1.1.11  root     1526:                fprintf(cons, "invalid breakpoint number: #%d\n", n);
1.1.1.12  root     1527:                return CmdAct::Stay;
1.1.1.10  root     1528:        }
                   1529: 
                   1530:        auto& bp = bpoint[n];
                   1531:        if (bp.type == BreakpointType::Unused) {
1.1.1.11  root     1532:                fprintf(cons, "invalid breakpoint number: #%d\n", n);
1.1.1.12  root     1533:                return CmdAct::Stay;
1.1.1.10  root     1534:        }
                   1535: 
1.1.1.11  root     1536:        fprintf(cons, "breakpoint #%d removed\n", n);
1.1.1.10  root     1537:        bp.type = BreakpointType::Unused;
                   1538:        // 今登録されている命令ブレークの必要命令長を再計算
                   1539:        RecalcInstMask();
1.1.1.12  root     1540:        return CmdAct::Stay;
1.1.1.10  root     1541: }
                   1542: 
1.1.1.4   root     1543: // 命令ブレークポイントの設定
1.1.1.13  root     1544: // bi [<cpu>,]<insn>[:<mask>] [<skip>]
1.1.1.12  root     1545: Debugger::CmdAct
1.1.1.4   root     1546: Debugger::cmd_bi()
                   1547: {
                   1548:        breakpoint_t bp;
1.1.1.13  root     1549:        std::string cpustr;
                   1550:        std::string argstr;
1.1.1.4   root     1551:        std::string inststr;
                   1552:        std::string maskstr;
                   1553:        int instlen;
                   1554:        int masklen;
                   1555: 
                   1556:        if (args.size() < 2) {
1.1.1.11  root     1557:                fprintf(cons, "usage: bi <inst>[:<mask>] [<skipcount>]\n");
1.1.1.12  root     1558:                return CmdAct::Stay;
1.1       root     1559:        }
                   1560: 
1.1.1.13  root     1561:        // CPU をまず分離
                   1562:        auto pos = args[1].find(',');
                   1563:        if (pos == std::string::npos) {
                   1564:                // CPU 指定なし
                   1565:                argstr = args[1];
                   1566:        } else {
                   1567:                // CPU 指定あり
                   1568:                cpustr = args[1].substr(0, pos);
                   1569:                argstr = args[1].substr(pos + 1);
                   1570:        }
                   1571: 
                   1572:        pos = argstr.find(':');
1.1.1.4   root     1573:        if (pos == std::string::npos) {
                   1574:                // マスク指定なし
1.1.1.13  root     1575:                inststr = argstr;
1.1.1.4   root     1576:                instlen = inststr.size();
                   1577:                masklen = -1;
                   1578:        } else {
                   1579:                // マスク指定あり
1.1.1.13  root     1580:                inststr = argstr.substr(0, pos);
1.1.1.4   root     1581:                instlen = inststr.size();
1.1.1.13  root     1582:                maskstr = argstr.substr(pos + 1);
1.1.1.4   root     1583:                masklen = maskstr.size();
                   1584:        }
                   1585: 
                   1586:        // 命令部チェック
                   1587:        if (ParseVerbHex(inststr.c_str(), &bp.inst) == false) {
1.1.1.13  root     1588:                fprintf(cons, "%s: invalid instruction value\n", argstr.c_str());
1.1.1.12  root     1589:                return CmdAct::Stay;
1.1.1.4   root     1590:        }
1.1.1.13  root     1591:        if (instlen % (curmd->inst_bytes * 2) != 0) {
                   1592:                fprintf(cons, "%s: invalid instruction length\n", argstr.c_str());
1.1.1.12  root     1593:                return CmdAct::Stay;
1.1.1.4   root     1594:        }
                   1595: 
                   1596:        // マスク部チェック
                   1597:        bp.mask = 0xffffffff;
                   1598:        if (masklen != -1) {
                   1599:                if (ParseVerbHex(maskstr.c_str(), &bp.mask) == false) {
1.1.1.13  root     1600:                        fprintf(cons, "%s: invalid mask value\n", argstr.c_str());
1.1.1.12  root     1601:                        return CmdAct::Stay;
1.1.1.4   root     1602:                }
                   1603:                if (masklen != instlen) {
1.1.1.11  root     1604:                        fprintf(cons, "%s: inst:mask must be the same length\n",
1.1.1.13  root     1605:                                argstr.c_str());
1.1.1.12  root     1606:                        return CmdAct::Stay;
1.1       root     1607:                }
                   1608:        }
1.1.1.4   root     1609:        // 8バイト未満なら左詰め。
1.1.1.13  root     1610:        if (curmd->inst_bytes < 4 && instlen < 8) {
1.1.1.4   root     1611:                bp.inst <<= 32 - instlen * 4;
                   1612:                bp.mask <<= 32 - instlen * 4;
                   1613:        }
                   1614: 
                   1615:        // あればスキップカウント
                   1616:        bp.skip = 0;
                   1617:        if (args.size() > 2) {
                   1618:                bp.skip = atoi(args[2].c_str());
                   1619:        }
                   1620: 
                   1621:        // 空いてるところにセット
                   1622:        bp.type = BreakpointType::Instruction;
1.1.1.13  root     1623:        int bi = AddBreakpoint(bp, cpustr);
1.1.1.4   root     1624:        if (bi == -1) {
1.1.1.11  root     1625:                fprintf(cons, "no free breakpoints\n");
1.1.1.4   root     1626:        } else {
1.1.1.11  root     1627:                fprintf(cons, "breakpoint #%d added\n", bi);
1.1.1.4   root     1628:        }
                   1629: 
                   1630:        // 今登録されている命令ブレークの必要命令長を再計算
                   1631:        RecalcInstMask();
1.1.1.12  root     1632:        return CmdAct::Stay;
1.1.1.4   root     1633: }
                   1634: 
                   1635: // 例外ブレークポイントの設定
1.1.1.13  root     1636: // bv [<cpu>,]<vector_number>[-<end_number>] [<skip>]
1.1.1.12  root     1637: Debugger::CmdAct
1.1.1.4   root     1638: Debugger::cmd_bv()
                   1639: {
                   1640:        breakpoint_t bp;
1.1.1.13  root     1641:        std::string argstr;
                   1642:        std::string cpustr;
                   1643:        DebuggerMD *md;
1.1.1.4   root     1644: 
                   1645:        if (args.size() < 2) {
1.1.1.13  root     1646:                fprintf(cons, "usage: bv [<cpu>,]<vec#>[-<end#>] [<skipcount>]\n");
1.1.1.12  root     1647:                return CmdAct::Stay;
1.1.1.4   root     1648:        }
                   1649: 
1.1.1.13  root     1650:        // CPU をまず分離
                   1651:        auto pos = args[1].find(',');
                   1652:        if (pos == std::string::npos) {
                   1653:                // CPU 指定なし
                   1654:                argstr = args[1];
                   1655:        } else {
                   1656:                // CPU 指定あり
                   1657:                cpustr = args[1].substr(0, pos);
                   1658:                argstr = args[1].substr(pos + 1);
                   1659:        }
                   1660:        // ベクタ名解釈のために必要
                   1661:        md = ParseCPU(cpustr);
                   1662: 
                   1663:        pos = argstr.find('-');
1.1.1.4   root     1664:        if (pos == std::string::npos) {
                   1665:                // ベクタ番号が1つなら vec1, vec2 を同値にしておく。
1.1.1.13  root     1666:                if (ParseVector(md, argstr.c_str(), (uint32 *)&bp.vec1) == false) {
                   1667:                        fprintf(cons, "%s: invalid vector number\n", argstr.c_str());
1.1.1.12  root     1668:                        return CmdAct::Stay;
1.1.1.4   root     1669:                }
                   1670:                bp.vec2 = bp.vec1;
                   1671:        } else {
                   1672:                // ベクタ番号(範囲指定 [vec1, vec2])
1.1.1.13  root     1673:                std::string str1 = argstr.substr(0, pos);
                   1674:                std::string str2 = argstr.substr(pos + 1);
1.1.1.4   root     1675: 
1.1.1.13  root     1676:                if (ParseVector(md, str1.c_str(), (uint32 *)&bp.vec1) == false) {
                   1677:                        fprintf(cons, "%s: invalid first vector number\n", argstr.c_str());
1.1.1.12  root     1678:                        return CmdAct::Stay;
1.1.1.4   root     1679:                }
1.1.1.13  root     1680:                if (ParseVector(md, str2.c_str(), (uint32 *)&bp.vec2) == false) {
                   1681:                        fprintf(cons, "%s: invalid last vector number\n", argstr.c_str());
1.1.1.12  root     1682:                        return CmdAct::Stay;
1.1.1.4   root     1683:                }
                   1684:        }
                   1685: 
                   1686:        // 範囲チェック
1.1.1.13  root     1687:        auto vectortable = pVectorTable.get();
                   1688:        if (bp.vec1 < 0 || bp.vec1 >= vectortable->Size()) {
1.1.1.11  root     1689:                fprintf(cons, "$%x: invalid vector number\n", bp.vec1);
1.1.1.12  root     1690:                return CmdAct::Stay;
1.1.1.4   root     1691:        }
1.1.1.13  root     1692:        if (bp.vec2 < 0 || bp.vec2 >= vectortable->Size()) {
1.1.1.11  root     1693:                fprintf(cons, "$%x: invalid last vector number\n", bp.vec2);
1.1.1.12  root     1694:                return CmdAct::Stay;
1.1.1.4   root     1695:        }
                   1696: 
                   1697:        // 大小が逆なら入れ替える?
                   1698:        if (bp.vec1 > bp.vec2) {
                   1699:                int tmp;
                   1700:                tmp = bp.vec1;
                   1701:                bp.vec1 = bp.vec2;
                   1702:                bp.vec2 = tmp;
                   1703:        }
                   1704: 
                   1705:        // あればスキップカウント
                   1706:        bp.skip = 0;
                   1707:        if (args.size() > 2) {
                   1708:                bp.skip = atoi(args[2].c_str());
                   1709:        }
1.1       root     1710: 
                   1711:        // 空いてるところにセット
1.1.1.4   root     1712:        bp.type = BreakpointType::Exception;
1.1.1.13  root     1713:        int bi = AddBreakpoint(bp, cpustr);
1.1       root     1714:        if (bi == -1) {
1.1.1.11  root     1715:                fprintf(cons, "no free breakpoints\n");
1.1       root     1716:        } else {
1.1.1.11  root     1717:                fprintf(cons, "breakpoint #%d added\n", bi);
1.1       root     1718:        }
1.1.1.5   root     1719: 
1.1.1.13  root     1720:        // この CPU 側に、すでに来ている例外をクリア。
1.1.1.5   root     1721:        // ブレークポイント設定の有無に関わらず例外が起きたら CPU 側から常に
                   1722:        // 通知されている。これをクリアするのは CheckAllBreakpoints() で、これは
                   1723:        // 命令間(命令前)に呼ばれるやつ、なのでこうなる。
1.1.1.13  root     1724:        // 1. 例外が起きると md->bv_vector がセットされる
1.1.1.5   root     1725:        // 2. 例外ブレークポイントを設定していないとこれがクリアされない
                   1726:        // 3. bv コマンドで例外ブレークを新たに設定すると、次の命令境界で
                   1727:        //    1.のベクタが反応してしまう。
                   1728:        // 命令ごととかにクリアしてもいいかも知れないが、ここでブレークポイントを
                   1729:        // 設定したのだから、それ以前の事象には反応すべきでない、という意味では
                   1730:        // ここでもいいか?
1.1.1.13  root     1731: 
                   1732:        // bp.md は AddBreakpoint() で bpoint 登録時にセットされるので
                   1733:        // インデックスから bp をもう一度読み込む。
                   1734:        bp = bpoint[bi];
                   1735:        bp.md->bv_vector = -1;
1.1.1.12  root     1736: 
                   1737:        return CmdAct::Stay;
1.1       root     1738: }
                   1739: 
1.1.1.13  root     1740: // ベクタ名かベクタ番号をパースする。
                   1741: bool
                   1742: Debugger::ParseVector(DebuggerMD *md, const char *arg, uint32 *valp)
                   1743: {
                   1744:        // 数値変換出来るか。
                   1745:        if (ParseVerbHex(arg, valp)) {
                   1746:                return true;
                   1747:        }
                   1748: 
                   1749:        // 出来なければ、機種ごとにベクタ名と比較する。
                   1750:        if (md->arch == CPUArch::HD64180) {
                   1751:                static std::vector<const char *> table = {
                   1752:                        "trap",
                   1753:                        "nmi",
                   1754:                        "int0",
                   1755:                        "int1",
                   1756:                        "int2",
                   1757:                        "inpcap",
                   1758:                        "outcmp",
                   1759:                        "timeov",
                   1760:                        "timer0",
                   1761:                        "timer1",
                   1762:                        "dma0",
                   1763:                        "dma1",
                   1764:                        "csio",
                   1765:                        "asci0",
                   1766:                        "asci1",
                   1767:                };
                   1768:                for (int i = 0, end = table.size(); i < end; i++) {
                   1769:                        if (strcasecmp(arg, table[i]) == 0) {
                   1770:                                *valp = i;
                   1771:                                return true;
                   1772:                        }
                   1773:                }
                   1774:        }
                   1775: 
                   1776:        return false;
                   1777: }
                   1778: 
1.1       root     1779: // ブレークポイント一覧表示
1.1.1.12  root     1780: Debugger::CmdAct
1.1       root     1781: Debugger::cmd_b_list()
                   1782: {
1.1.1.9   root     1783:        ShowMonitor(bpoint_monitor);
1.1.1.12  root     1784:        return CmdAct::Stay;
1.1.1.4   root     1785: }
                   1786: 
                   1787: // ブレークポイント一覧 (モニタ)
                   1788: void
1.1.1.9   root     1789: Debugger::MonitorUpdateBpoint(Monitor *, TextScreen& monitor)
1.1.1.4   root     1790: {
                   1791:        // 0         1         2         3         4         5         6
                   1792:        // 0123456789012345678901234567890123456789012345678901234567890
1.1.1.13  root     1793:        // No CPU  Type Parameter         Matched    Skip
                   1794:        // #0 xp   addr $01234567         123456789  123456789/123456789
                   1795:        // #1 main inst 00000000/00000000
                   1796:        // #2 main excp $00-$00
1.1.1.4   root     1797: 
                   1798:        monitor.Clear();
1.1.1.13  root     1799:        monitor.Print(0, 0, "No CPU  Type Parameter");
                   1800:        monitor.Print(31, 0, "Matched");
                   1801:        monitor.Print(42, 0, "Skip");
1.1       root     1802: 
1.1.1.3   root     1803:        for (int i = 0; i < bpoint.size(); i++) {
1.1.1.4   root     1804:                const auto& bp = bpoint[i];
                   1805:                int y = i + 1;
                   1806: 
                   1807:                monitor.Print(0, y, "#%d", i);
                   1808: 
                   1809:                // 種別ごとの表示
                   1810:                switch (bp.type) {
                   1811:                 case BreakpointType::Unused:
                   1812:                        continue;
                   1813: 
                   1814:                 case BreakpointType::Address:
1.1.1.13  root     1815:                        monitor.Print(3, y, "%-4s addr $%08x",
                   1816:                                bp.md->GetName().c_str(), bp.addr);
1.1.1.4   root     1817:                        break;
                   1818:                 case BreakpointType::Memory:
1.1.1.13  root     1819:                        monitor.Print(3, y, "%-4s mem  $%08x",
                   1820:                                bp.md->GetName().c_str(), bp.addr);
1.1.1.4   root     1821:                        break;
                   1822: 
                   1823:                 case BreakpointType::Exception:
1.1.1.13  root     1824:                        monitor.Print(3, y, "%-4s excp $%02x",
                   1825:                                bp.md->GetName().c_str(), bp.vec1);
1.1.1.4   root     1826:                        if (bp.vec2 != bp.vec1) {
1.1.1.13  root     1827:                                monitor.Print(16, y, "-$%02x", bp.vec2);
1.1.1.4   root     1828:                        }
                   1829:                        break;
                   1830: 
                   1831:                 case BreakpointType::Instruction:
1.1.1.13  root     1832:                        monitor.Print(3, y, "%-4s inst", bp.md->GetName().c_str());
                   1833:                        if (bp.md->inst_bytes == 4) {
1.1.1.4   root     1834:                                if (bp.mask == 0xffffffff) {
1.1.1.13  root     1835:                                        monitor.Print(13, y, "%08x", bp.inst);
1.1.1.4   root     1836:                                } else {
1.1.1.13  root     1837:                                        monitor.Print(13, y, "%08x:%08x", bp.inst, bp.mask);
1.1.1.4   root     1838:                                }
                   1839:                        } else {
                   1840:                                if (bp.mask == 0xffffffff) {
1.1.1.13  root     1841:                                        monitor.Print(13, y, "%08x", bp.inst);
1.1.1.4   root     1842:                                } else if (((bp.inst | bp.mask) & 0x0000ffff) != 0) {
1.1.1.13  root     1843:                                        monitor.Print(13, y, "%08x:%08x", bp.inst, bp.mask);
1.1.1.4   root     1844:                                } else if (bp.mask == 0xffff0000) {
1.1.1.13  root     1845:                                        monitor.Print(13, y, "%04x", bp.inst >> 16);
1.1.1.4   root     1846:                                } else {
1.1.1.13  root     1847:                                        monitor.Print(13, y, "%04x:%04x",
1.1.1.4   root     1848:                                                bp.inst >> 16, bp.mask >> 16);
                   1849:                                }
                   1850:                        }
                   1851:                        break;
                   1852: 
                   1853:                 default:
1.1.1.13  root     1854:                        monitor.Print(3, y, "%-4s type=%d",
                   1855:                                (bp.md ? bp.md->GetName().c_str() : "?"), (int)bp.type);
1.1.1.4   root     1856:                        continue;
                   1857:                }
                   1858: 
                   1859:                // マッチ回数
1.1.1.13  root     1860:                monitor.Print(31, y, "%d", bp.matched);
1.1.1.4   root     1861: 
                   1862:                // スキップ
                   1863:                if (bp.skip < 0) {
1.1.1.13  root     1864:                        monitor.Print(42, y, "forever");
1.1.1.4   root     1865:                } else if (bp.skip > 0) {
1.1.1.13  root     1866:                        monitor.Print(42, y, "%d / %d", (bp.skip - bp.skipremain), bp.skip);
1.1       root     1867:                }
                   1868:        }
                   1869: }
                   1870: 
                   1871: // ブレークポイント全削除
1.1.1.12  root     1872: Debugger::CmdAct
1.1       root     1873: Debugger::cmd_bx()
                   1874: {
1.1.1.3   root     1875:        for (auto& bp : bpoint) {
1.1.1.4   root     1876:                bp.type = BreakpointType::Unused;
1.1       root     1877:        }
1.1.1.11  root     1878:        fprintf(cons, " All breakpoints disabled\n");
1.1.1.4   root     1879: 
                   1880:        // 今登録されている命令ブレークの必要命令長を再計算
                   1881:        RecalcInstMask();
1.1.1.12  root     1882: 
                   1883:        return CmdAct::Stay;
1.1       root     1884: }
                   1885: 
1.1.1.13  root     1886: // CPU 文字列から対応する MD を返す。
                   1887: // 一致しなければ NULL を返す。
                   1888: DebuggerMD *
                   1889: Debugger::ParseCPU(const std::string& cpustr) const
                   1890: {
                   1891:        if (cpustr.empty()) {
                   1892:                return curmd;
                   1893:        } else if (cpustr == "xp") {
                   1894:                return md_xp.get();
                   1895:        } else if (cpustr == "main") {
                   1896:                return md_mpu.get();
                   1897:        }
                   1898:        return NULL;
                   1899: }
                   1900: 
1.1       root     1901: // ブレークポイントを設定。
1.1.1.13  root     1902: // new_bp のうち cpu, matched, skipremain はこちらで初期化する。
1.1.1.4   root     1903: // それ以外を埋めてから呼ぶこと。
                   1904: // 設定できればその番号、できなければ -1 を返す。
1.1       root     1905: int
1.1.1.13  root     1906: Debugger::AddBreakpoint(const breakpoint_t& new_bp, const std::string& cpustr)
1.1       root     1907: {
1.1.1.13  root     1908:        DebuggerMD *md = ParseCPU(cpustr);
                   1909: 
1.1.1.3   root     1910:        for (int i = 0; i < bpoint.size(); i++) {
                   1911:                auto& bp = bpoint[i];
1.1.1.4   root     1912:                if (bp.type == BreakpointType::Unused) {
                   1913:                        bp = new_bp;
1.1.1.13  root     1914:                        bp.md = md;
1.1.1.4   root     1915:                        bp.matched = 0;
                   1916:                        if (bp.skip > 0) {
                   1917:                                bp.skipremain = bp.skip;
                   1918:                        } else {
                   1919:                                bp.skipremain = 0;
                   1920:                        }
                   1921: 
1.1       root     1922:                        return i;
                   1923:                }
                   1924:        }
                   1925:        return -1;
                   1926: }
                   1927: 
1.1.1.4   root     1928: // すべての命令ブレークのマスクのうち最長のものを再計算する。
                   1929: // 命令ブレークポイントの追加/削除のたびに呼び出すこと。
                   1930: void
                   1931: Debugger::RecalcInstMask()
                   1932: {
                   1933:        uint32 mask;
                   1934:        bi_need_bytes = 0;
                   1935: 
                   1936:        // 登録されている命令ブレークの最長マスクを求める
                   1937:        mask = 0;
                   1938:        for (const auto& bp : bpoint) {
                   1939:                if (bp.type == BreakpointType::Instruction) {
                   1940:                        mask |= bp.mask;
                   1941:                }
                   1942:        }
                   1943: 
                   1944:        // mask の Number of Trailing Zero を求める。
                   1945:        // (x & -x) で x の最も下の立ってるビットだけを立てる、
                   1946:        // (x & -x) -1 でそれより下の全ビットを立てる、
                   1947:        // それを popcount で数えるので、$fffffff0 なら ntz = 4 になる。
                   1948:        int ntz = __builtin_popcount((mask & -(int32)mask) - 1);
                   1949:        // 上位側から数えたマスクに必要なビット数
                   1950:        int mlen = 32 - ntz;
                   1951:        // 命令語単位に切り上げる
1.1.1.13  root     1952:        mlen = roundup(mlen, curmd->inst_bytes * 8);
1.1.1.4   root     1953:        // バイト数に変換
                   1954:        mlen /= 8;
                   1955: 
                   1956:        if (mlen > bi_need_bytes) {
                   1957:                bi_need_bytes = mlen;
                   1958:        }
                   1959: }
                   1960: 
                   1961: // ブランチ履歴、例外履歴表示
                   1962: // brhist [<maxlines>]
                   1963: // exhist [<maxlines>]
                   1964: // <maxlines> は表示する最大エントリ数。
                   1965: // コンソールではデフォルトで下を新しいの順とする。<maxlines> を負数にすると
                   1966: // (行数は絶対値して) 並び順を逆にしてモニタウィンドウと同じ上を新しいの順に
                   1967: // する。
1.1.1.12  root     1968: Debugger::CmdAct
1.1       root     1969: Debugger::cmd_brhist()
                   1970: {
1.1.1.13  root     1971:        auto brhist = gMainApp.GetObject<BranchHistory>(OBJ_MPU_BRHIST);
                   1972:        return cmd_hist_common(*brhist);
1.1.1.4   root     1973: }
1.1.1.12  root     1974: Debugger::CmdAct
1.1.1.4   root     1975: Debugger::cmd_exhist()
                   1976: {
1.1.1.13  root     1977:        auto exhist = gMainApp.GetObject<BranchHistory>(OBJ_MPU_EXHIST);
                   1978:        return cmd_hist_common(*exhist);
1.1.1.4   root     1979: }
1.1.1.3   root     1980: 
1.1.1.4   root     1981: // ブランチ履歴、例外履歴表示の共通部分。
1.1.1.12  root     1982: Debugger::CmdAct
1.1.1.9   root     1983: Debugger::cmd_hist_common(BranchHistory& hist)
1.1.1.4   root     1984: {
                   1985:        // 表示最大行数(と向き)
                   1986:        // 向きは bottom_to_top = true が新しいほうを下とする方向。
                   1987:        int maxlines = 20;
                   1988:        bool bottom_to_top = true;
1.1.1.3   root     1989:        if (args.size() > 1) {
1.1.1.4   root     1990:                maxlines = atoi(args[1].c_str());
                   1991:                if (maxlines < 0) {
                   1992:                        bottom_to_top = false;
                   1993:                        maxlines = -maxlines;
1.1.1.3   root     1994:                }
1.1.1.4   root     1995: 
                   1996:                if (maxlines < 1)
                   1997:                        maxlines = 1;
                   1998:                if (maxlines > 256)
                   1999:                        maxlines = 256;
                   2000:        }
                   2001: 
                   2002:        // コンソールでは空エントリの行は表示したくないので、
                   2003:        // 先にエントリ数を調べる。
                   2004:        int used = hist.GetUsed();
                   2005: 
                   2006:        // 表示行数 + 1行はヘッダ分
                   2007:        int lines = std::min(used, maxlines) + 1;
                   2008: 
                   2009:        // MonitorUpdate() は TextScreen 高さに合わせて出力してくれる。
1.1.1.9   root     2010:        auto& histmon = hist.monitor;
                   2011:        auto size = histmon.GetSize();
                   2012:        TextScreen screen;
                   2013:        screen.Init(size.width, lines);
1.1.1.4   root     2014: 
                   2015:        // コンソールでは下が新しいの順のほうがいい
                   2016:        if (bottom_to_top) {
1.1.1.9   root     2017:                screen.userdata |= BranchHistory::BottomToTop;
1.1.1.3   root     2018:        }
1.1.1.4   root     2019: 
                   2020:        // 表示
1.1.1.9   root     2021:        MONITOR_UPDATE(histmon, screen);
                   2022:        ShowTextScreen(screen);
1.1.1.12  root     2023: 
                   2024:        return CmdAct::Stay;
1.1       root     2025: }
                   2026: 
                   2027: // 実行再開(continue): c [<addr>]
                   2028: // <addr> 指定があれば <addr> まで実行。
1.1.1.12  root     2029: Debugger::CmdAct
1.1       root     2030: Debugger::cmd_c()
                   2031: {
1.1.1.3   root     2032:        if (args.size() > 1) {
1.1       root     2033:                // 引数があれば
1.1.1.13  root     2034:                uint32 addr;
                   2035:                if (!ParseAddr(args[1].c_str(), &addr)) {
1.1.1.12  root     2036:                        return CmdAct::Stay;
1.1       root     2037:                }
1.1.1.13  root     2038:                step_type = StepType::Addr;
                   2039:                step_md = curmd;
                   2040:                // ネイティブ命令長に丸める
                   2041:                step_addr = addr & ~(curmd->inst_bytes - 1);
1.1       root     2042:        }
1.1.1.12  root     2043:        return CmdAct::Leave;
                   2044: }
                   2045: 
                   2046: // 指定仮想時間実行: ct [<time>]
                   2047: // <time> が省略されたら前回指定した時間間隔で再実行。
                   2048: Debugger::CmdAct
                   2049: Debugger::cmd_ct()
                   2050: {
                   2051:        if (args.size() > 1) {
                   2052:                // 引数があれば
                   2053:                if (!ParseTime(args[1].c_str(), &ct_timespan)) {
                   2054:                        return CmdAct::Stay;
                   2055:                }
                   2056:        }
                   2057:        if (ct_timespan == 0) {
                   2058:                fprintf(cons, "time must be specified\n");
                   2059:                return CmdAct::Stay;
                   2060:        }
1.1.1.13  root     2061:        step_type = StepType::Time;
                   2062:        step_md = curmd;
                   2063:        step_time = scheduler->GetVirtTime() + ct_timespan;
1.1.1.12  root     2064: 
                   2065:        return CmdAct::Leave;
1.1       root     2066: }
                   2067: 
1.1.1.5   root     2068: // 引数などからアドレスを取得するごった煮共通ルーチン。
                   2069: //
1.1.1.6   root     2070: // 引数がなければ sa->addr には書き込まずに true で帰る (ので、呼び出し側が
                   2071: // 事前に適切なデフォルト値をセットしておくと、それがそのまま使われる)。
1.1.1.3   root     2072: // 引数が1つ(以上)あれば args[1] をアドレスとしてパースする。
1.1.1.6   root     2073: // アドレスには空間修飾子を前置可能、省略の場合はいずれも現在値を使う。
                   2074: // エラーならメッセージを表示して false を返す。
1.1.1.5   root     2075: // sa は in/out パラメータ。
1.1       root     2076: bool
1.1.1.15! root     2077: Debugger::GetAddr(busaddr *sa, bool default_data)
1.1       root     2078: {
1.1.1.5   root     2079:        if (args.size() < 2) {
1.1.1.6   root     2080:                // 引数なしなら、呼び出し側が sa にセットした前回値をそのまま使う。
1.1.1.5   root     2081:                return true;
                   2082:        }
                   2083: 
1.1.1.6   root     2084:        // 引数ありならパースする。アドレスが明示的に指定されたので、
                   2085:        // ここから先の省略箇所は前回値ではなくデフォルト値で補完する。
                   2086: 
1.1.1.5   root     2087:        std::string& str = args[1];
                   2088:        uint32 addr;
                   2089: 
1.1.1.6   root     2090:        // ':' があればその前はアドレス空間修飾子
                   2091:        int mod_super = -1;             // 'u'/'s'
                   2092:        int mod_prog  = -1;             // 'd'/'p' or 'd'/'i'
                   2093:        int mod_num   = -1;             // '0'..'7'
                   2094:        auto addrpos = str.find(':');
                   2095:        if (addrpos == std::string::npos) {
                   2096:                // なければ先頭からアドレス
                   2097:                addrpos = 0;
                   2098:        } else {
                   2099:                // ':' が途中にあれば、その前が修飾子。
                   2100:                // ':' が先頭なら修飾子が空文字列という扱いでいいか。
                   2101:                std::string mod = str.substr(0, addrpos);
                   2102:                addrpos++;
                   2103:                for (auto ch : mod) {
                   2104:                        if (ch == 'u') {
                   2105:                                if (mod_super == 1)
                   2106:                                        goto error;
                   2107:                                mod_super = 0;
                   2108:                        } else if (ch == 's') {
                   2109:                                if (mod_super == 0)
                   2110:                                        goto error;
                   2111:                                mod_super = 1;
                   2112:                        } else if (ch == 'd') {
                   2113:                                if (mod_prog == 1)
                   2114:                                        goto error;
                   2115:                                mod_prog = 0;
                   2116:                        } else if (ch == 'p' || ch == 'i') {
                   2117:                                // m68k では 'P'rogram space、m88k では 'I'nstruction CMMU…
                   2118:                                if (mod_prog == 0)
                   2119:                                        goto error;
                   2120:                                mod_prog = 1;
                   2121:                        } else if ('0' <= ch && ch <= '7') {
                   2122:                                int num = ch - '0';
1.1.1.13  root     2123:                                if (curmd->arch == CPUArch::M680x0) {
1.1.1.6   root     2124:                                        // m68k では FC 指定は Super/User 指定を含んでおり、
                   2125:                                        // 値指定は他の修飾子とは同時に指定できない。
                   2126:                                        if (mod_super >= 0 || mod_prog >= 0 ||
                   2127:                                            (mod_num >= 0 && mod_num != num)) {
                   2128:                                                goto error;
                   2129:                                        }
                   2130:                                        // 有効な値は 1,2,5,6 のみ
                   2131:                                        if (num == 0 || num == 3 || num == 4 || num == 7) {
1.1.1.11  root     2132:                                                fprintf(cons, "%c: invalid address space\n", ch);
1.1.1.6   root     2133:                                        }
                   2134:                                        mod_num   = num;
                   2135:                                        mod_super = mod_num >> 2;               // FC2
                   2136:                                        mod_prog  = (mod_num & 3) - 1;  // FC1,FC0
1.1.1.13  root     2137:                                } else if (curmd->arch == CPUArch::M88xx0) {
1.1.1.6   root     2138:                                        // m88k では CMMU 指定と、Super/User 指定は独立。
                   2139:                                        if (mod_prog >= 0 ||
                   2140:                                            (mod_num >= 0 && mod_num != num)) {
                   2141:                                                goto error;
                   2142:                                        }
                   2143:                                        mod_num  = num;
                   2144:                                        mod_prog = (mod_num & 1);               // CMMU7 が Inst
1.1.1.13  root     2145:                                } else {
                   2146:                                        fprintf(cons, "not yet\n");
1.1.1.6   root     2147:                                }
                   2148:                        } else {
1.1.1.11  root     2149:                                fprintf(cons, "%c: unknown address space modifier\n", ch);
1.1.1.6   root     2150:                                return false;
                   2151:                        }
                   2152:                        continue;
                   2153: 
                   2154:                 error:
1.1.1.11  root     2155:                        fprintf(cons, "%s: address space modifier '%c' conflicts\n",
1.1.1.6   root     2156:                                mod.c_str(), ch);
                   2157:                        return false;
                   2158:                }
                   2159: 
                   2160:                // 数値指定を機種ごとに読み替える
                   2161:                if (mod_num >= 0) {
1.1.1.13  root     2162:                        if (curmd->arch == CPUArch::M680x0) {
1.1.1.6   root     2163:                                // m68k なら値指定で全部確定する
                   2164:                                //  "1:" -> User/Data
                   2165:                                //  "2:" -> User/Program
                   2166:                                //  "5:" -> Super/Data
                   2167:                                //  "6:" -> Super/Program
                   2168:                                mod_super = (mod_num & 4) ? true : false;
                   2169:                                mod_prog  = (mod_num & 3) - 1;
1.1.1.13  root     2170:                        } else if (curmd->arch == CPUArch::M88xx0) {
1.1.1.6   root     2171:                                // m88k では mod_num を CMMU ID とする(?)。
                   2172:                                // XXX まだ CPU は1つしかないので雑に処理。
                   2173:                                //  "6"  -> Data    (CPU#0)
                   2174:                                //  "7"  -> Program (CPU#0)
                   2175:                                if (mod_num < 6) {
1.1.1.11  root     2176:                                        fprintf(cons, "%d: CMMU#%d not installed\n",
1.1.1.6   root     2177:                                                mod_num, mod_num);
                   2178:                                        return false;
                   2179:                                }
                   2180:                                mod_prog = mod_num - 6;
                   2181:                        }
                   2182:                }
1.1       root     2183:        }
1.1.1.5   root     2184: 
1.1.1.6   root     2185:        // アドレス空間修飾子のうち省略箇所はデフォルト状態で補完
                   2186:        // - 's'/'u' いずれもなければ現在値。
                   2187:        // - 'd'/'p'/'i' いずれもなければ、d/m コマンドごとに自然なほう。
                   2188:        if (mod_super < 0) {
1.1.1.13  root     2189:                mod_super = curmd->IsSuper();
1.1.1.6   root     2190:        }
                   2191:        if (mod_prog < 0) {
                   2192:                mod_prog = !default_data;
                   2193:        }
                   2194:        sa->SetSuper(mod_super);
                   2195:        sa->SetData(!mod_prog);
                   2196: 
1.1.1.5   root     2197:        // アドレス
                   2198:        if (ParseAddr(str.c_str() + addrpos, &addr) == false) {
                   2199:                return false;
                   2200:        }
1.1.1.7   root     2201:        // 命令境界に丸める
1.1.1.13  root     2202:        sa->SetAddr(addr & ~(curmd->inst_bytes - 1));
1.1.1.5   root     2203: 
1.1       root     2204:        return true;
                   2205: }
                   2206: 
1.1.1.13  root     2207: // ターゲット CPU を "mpu" (M680x0/M88xx0) に変更。
                   2208: Debugger::CmdAct
                   2209: Debugger::cmd_mpu()
                   2210: {
                   2211:        if (curmd != md_mpu.get()) {
                   2212:                ChangeMD(md_mpu.get());
                   2213:                ResetStickyAddr();
                   2214:                cmd_minus();
                   2215:        }
                   2216:        return CmdAct::Stay;
                   2217: }
                   2218: 
                   2219: // ターゲット CPU を "xp" (HD647180) に変更。
                   2220: Debugger::CmdAct
                   2221: Debugger::cmd_xp()
                   2222: {
                   2223:        if (curmd != md_xp.get()) {
                   2224:                ChangeMD(md_xp.get());
                   2225:                ResetStickyAddr();
                   2226:                cmd_minus();
                   2227:        }
                   2228:        return CmdAct::Stay;
                   2229: }
                   2230: 
                   2231: // この命令の1つ次のアドレスまでスキップする。
                   2232: // dbra とかのループを飛ばす用。
                   2233: Debugger::CmdAct
                   2234: Debugger::cmd_z()
                   2235: {
                   2236:        // 次のアドレスを求める。
                   2237:        if (curmd->inst_bytes_fixed) {
                   2238:                // 固定長命令
                   2239:                step_addr = curmd->GetPC() + curmd->inst_bytes_fixed;
                   2240:        } else {
                   2241:                // 可変長なら逆アセンブルしてみるしか。
                   2242: 
1.1.1.15! root     2243:                busaddr laddr(curmd->GetPC(), busaddr::Fetch, curmd->IsSuper());
        !          2244:                DebuggerMemoryStream mem(curmd, laddr);
1.1.1.13  root     2245:                // XXX 失敗したらどうすべ
                   2246:                curmd->Disassemble(mem);
1.1.1.15! root     2247:                step_addr = mem.laddr.Addr();
1.1.1.13  root     2248:        }
                   2249:        step_type = StepType::Addr;
                   2250:        step_md = curmd;
                   2251: 
                   2252:        return CmdAct::Leave;
                   2253: }
                   2254: 
                   2255: void
                   2256: Debugger::ChangeMD(DebuggerMD *newmd)
                   2257: {
                   2258:        if (newmd != curmd) {
                   2259:                curmd = newmd;
                   2260:                fprintf(cons, "Target cpu switched to '%s'\n",
                   2261:                        curmd->GetName().c_str());
                   2262:        }
                   2263: }
                   2264: 
                   2265: // pc と d/m の初期値をリセット。
                   2266: void
                   2267: Debugger::ResetStickyAddr()
                   2268: {
                   2269:        pc = curmd->GetPC();
1.1.1.15! root     2270:        m_last_addr.Set(pc, busaddr::Read,  curmd->IsSuper());
        !          2271:        d_last_addr.Set(pc, busaddr::Fetch, curmd->IsSuper());
1.1.1.13  root     2272: }
                   2273: 
1.1.1.15! root     2274: // 逆アセンブル: d [[SU:]<addr> [<cnt>]] (論理)
1.1.1.12  root     2275: Debugger::CmdAct
1.1       root     2276: Debugger::cmd_d()
                   2277: {
1.1.1.15! root     2278:        return cmd_d_common(busaddr::Logical);
1.1       root     2279: }
                   2280: 
                   2281: // 逆アセンブル: D [<addr> [<cnt>]] (物理)
1.1.1.12  root     2282: Debugger::CmdAct
1.1       root     2283: Debugger::cmd_D()
                   2284: {
1.1.1.15! root     2285:        return cmd_d_common(busaddr::Physical);
1.1       root     2286: }
                   2287: 
                   2288: // 逆アセンブル共通
1.1.1.12  root     2289: Debugger::CmdAct
1.1.1.15! root     2290: Debugger::cmd_d_common(busaddr access_mode)
1.1       root     2291: {
1.1.1.15! root     2292:        busaddr saddr;
1.1.1.13  root     2293:        int row;
1.1       root     2294: 
1.1.1.13  root     2295:        row = 10;
1.1       root     2296: 
1.1.1.15! root     2297:        // 開始アドレス。S/U は GetAddr() がセットする。
1.1.1.6   root     2298:        saddr = d_last_addr;
                   2299:        if (GetAddr(&saddr, false) == false) {
1.1.1.12  root     2300:                return CmdAct::Stay;
1.1       root     2301:        }
1.1.1.15! root     2302: 
        !          2303:        // 論理アドレス/物理アドレスはコマンドによって決まる。
        !          2304:        saddr |= access_mode;
        !          2305: 
1.1       root     2306:        // 2つ目の引数があれば行数
1.1.1.3   root     2307:        if (args.size() > 2) {
1.1.1.13  root     2308:                row = strtol(args[2].c_str(), NULL, 10);
1.1       root     2309:        }
                   2310: 
1.1.1.13  root     2311:        // アドレス幅を制限 (XP用) (ここ?)
                   2312:        uint64 mask;
1.1.1.15! root     2313:        if (saddr.IsPhysical()) {
1.1.1.13  root     2314:                mask = (1ULL << curmd->GetPASize()) - 1;
1.1.1.15! root     2315:        } else {
        !          2316:                mask = (1ULL << curmd->GetLASize()) - 1;
1.1       root     2317:        }
1.1.1.15! root     2318:        saddr &= (uint32)mask;
1.1.1.13  root     2319: 
                   2320:        Memdump disasm(curmd, curmd->arch);
                   2321:        disasm.SetAddr(saddr);
                   2322:        disasm.Print(cons, row);
1.1.1.2   root     2323: 
1.1.1.15! root     2324:        // アドレスを次回継続用に保存
1.1.1.13  root     2325:        d_last_addr = disasm.GetAddr();
1.1.1.12  root     2326: 
                   2327:        return CmdAct::Stay;
1.1       root     2328: }
                   2329: 
1.1.1.5   root     2330: // 表示レジスタ選択
                   2331: // disp           現在の内容を表示
                   2332: // disp <regs>... 設定
1.1.1.12  root     2333: Debugger::CmdAct
1.1.1.5   root     2334: Debugger::cmd_disp()
                   2335: {
                   2336:        if (args.size() < 2) {
                   2337:                // 表示
                   2338:                bool first = true;
                   2339:                for (const auto& r : disp_regs) {
1.1.1.11  root     2340:                        fprintf(cons, "%s%s", (first ? "" : ","), r.c_str());
1.1.1.5   root     2341:                        first = false;
                   2342:                }
1.1.1.11  root     2343:                fprintf(cons, "\n");
1.1.1.12  root     2344:                return CmdAct::Stay;
1.1.1.5   root     2345:        }
                   2346: 
                   2347:        // 設定 (引数を分解する)
                   2348:        char buf[256];
                   2349:        char *p;
                   2350:        char *last;
                   2351:        strlcpy(buf, args[1].c_str(), sizeof(buf));
                   2352:        disp_regs.clear();
                   2353:        for (p = strtok_r(buf, ",", &last); p; p = strtok_r(NULL, ",", &last)) {
                   2354:                disp_regs.push_back(std::string(p));
                   2355:        }
                   2356: 
                   2357:        // XXX チェック
1.1.1.12  root     2358: 
                   2359:        return CmdAct::Stay;
1.1.1.5   root     2360: }
                   2361: 
1.1.1.9   root     2362: // ログレベル設定: L <name>[=<level>][...]
1.1.1.12  root     2363: Debugger::CmdAct
1.1       root     2364: Debugger::cmd_L()
                   2365: {
1.1.1.9   root     2366:        if (args.size() < 2) {
1.1.1.11  root     2367:                fprintf(cons, "L <name1>[=<level1>][,<name2>[=<level2>]]...\n");
1.1.1.12  root     2368:                return CmdAct::Stay;
1.1       root     2369:        }
                   2370: 
1.1.1.9   root     2371:        // 一旦全部つなげる。
                   2372:        // help が混ざる場合の "L foo=1,help" と "L foo=1 help" を同じ動作に
                   2373:        // するため。
                   2374:        std::string str;
                   2375:        for (int i = 1, ac = args.size(); i < ac; i++) {
                   2376:                if (str.empty() == false) {
                   2377:                        str += ',';
                   2378:                }
                   2379:                str += args[i];
                   2380:        }
                   2381: 
                   2382:        // で、再分解
                   2383:        std::vector<std::string> items = string_split(str, ',');
                   2384: 
                   2385:        // "help" があれば一覧を表示
                   2386:        for (const auto& item : items) {
                   2387:                if (item == "help") {
                   2388:                        std::vector<std::string> list = MainApp::GetLogNames();
                   2389:                        for (const auto& name : list) {
1.1.1.11  root     2390:                                fprintf(cons, " %s\n", name.c_str());
1.1.1.9   root     2391:                        }
1.1.1.12  root     2392:                        return CmdAct::Stay;
1.1.1.9   root     2393:                }
                   2394:        }
1.1       root     2395: 
1.1.1.9   root     2396:        // ログレベルを設定
                   2397:        std::string errmsg;
                   2398:        if (MainApp::SetLogopt(items, &errmsg) == false) {
1.1.1.11  root     2399:                fprintf(cons, "%s\n", errmsg.c_str());
1.1.1.12  root     2400:                return CmdAct::Stay;
1.1       root     2401:        }
1.1.1.12  root     2402: 
                   2403:        return CmdAct::Stay;
1.1       root     2404: }
                   2405: 
1.1.1.15! root     2406: // メモリダンプ: m [<addr> [<cnt>]] (論理)
1.1.1.12  root     2407: Debugger::CmdAct
1.1       root     2408: Debugger::cmd_m()
                   2409: {
1.1.1.15! root     2410:        return cmd_m_common(busaddr::Logical);
1.1       root     2411: }
                   2412: 
                   2413: // メモリダンプ: M [<addr> [<cnt>]] (物理)
1.1.1.12  root     2414: Debugger::CmdAct
1.1       root     2415: Debugger::cmd_M()
                   2416: {
1.1.1.15! root     2417:        return cmd_m_common(busaddr::Physical);
1.1       root     2418: }
                   2419: 
                   2420: // メモリダンプ共通
1.1.1.12  root     2421: Debugger::CmdAct
1.1.1.15! root     2422: Debugger::cmd_m_common(busaddr access_mode)
1.1       root     2423: {
1.1.1.15! root     2424:        busaddr saddr;
1.1.1.9   root     2425:        int row;
1.1       root     2426: 
1.1.1.9   root     2427:        row = 8;
1.1       root     2428: 
1.1.1.15! root     2429:        // 開始アドレス。S/U は GetAddr() がセットする。
1.1.1.6   root     2430:        saddr = m_last_addr;
                   2431:        if (GetAddr(&saddr, true) == false) {
1.1.1.12  root     2432:                return CmdAct::Stay;
1.1       root     2433:        }
1.1.1.15! root     2434: 
        !          2435:        // 論理アドレス/物理アドレスはコマンドによって決まる。
        !          2436:        saddr |= access_mode;
        !          2437: 
1.1       root     2438:        // 2つ目の引数があれば行数
1.1.1.3   root     2439:        if (args.size() > 2) {
1.1.1.9   root     2440:                row = strtol(args[2].c_str(), NULL, 10);
                   2441:        }
                   2442: 
1.1.1.13  root     2443:        // アドレス幅を制限 (XP用) (ここ?)
                   2444:        uint64 mask;
1.1.1.15! root     2445:        if (saddr.IsPhysical()) {
1.1.1.13  root     2446:                mask = (1ULL << curmd->GetPASize()) - 1;
1.1.1.15! root     2447:        } else {
        !          2448:                mask = (1ULL << curmd->GetLASize()) - 1;
1.1.1.9   root     2449:        }
1.1.1.15! root     2450:        saddr &= (uint32)mask;
1.1.1.9   root     2451: 
1.1.1.13  root     2452:        Memdump memdump(curmd);
                   2453:        memdump.SetAddr(saddr);
                   2454:        memdump.SetFormat(Memdump::Word);
                   2455:        memdump.Print(cons, row);
                   2456: 
                   2457:        // アドレスを次回継続用に保存
                   2458:        m_last_addr = memdump.GetAddr();
1.1.1.12  root     2459: 
                   2460:        return CmdAct::Stay;
1.1.1.9   root     2461: }
                   2462: 
1.1       root     2463: // プロンプトに来た最初に表示するいつものやつを再表示する
1.1.1.12  root     2464: Debugger::CmdAct
1.1       root     2465: Debugger::cmd_minus()
                   2466: {
1.1.1.5   root     2467:        // 指定のレジスタ群を表示
                   2468:        // disp_regs はレジスタ群のリスト { "r", "rf" } のような感じ。
                   2469:        // 一方 ShowRegisters() の第2引数は1つのコマンドラインを空白で区切った
                   2470:        // 文字列リスト { arg[0], arg[1], .. }。型が同じで意味が違うので注意。
                   2471:        for (const auto& reg : disp_regs) {
                   2472:                std::vector<std::string> tmpargs;
                   2473:                tmpargs.push_back(reg);
1.1.1.13  root     2474:                curmd->ShowRegister(cons, tmpargs);
1.1.1.5   root     2475:        }
1.1       root     2476: 
1.1.1.13  root     2477:        // 現在の PC の位置を逆アセンブル。
1.1.1.2   root     2478:        std::string addrbuf;
1.1.1.13  root     2479:        std::string textbuf;
1.1.1.6   root     2480: 
1.1.1.15! root     2481:        busaddr laddr(pc, busaddr::Fetch, curmd->IsSuper());
        !          2482:        DebuggerMemoryStream mem(curmd, laddr);
1.1.1.6   root     2483:        // アドレス
                   2484:        if (mem.FormatAddr(addrbuf) == false) {
1.1.1.11  root     2485:                fprintf(cons, "%s\n", addrbuf.c_str());
1.1.1.12  root     2486:                return CmdAct::Stay;
1.1.1.2   root     2487:        }
1.1.1.13  root     2488:        // オンライン用に逆アセンブル
                   2489:        textbuf = curmd->Disassemble(mem);
1.1.1.6   root     2490: 
1.1.1.13  root     2491:        fprintf(cons, "%-18s %s\n", addrbuf.c_str(), textbuf.c_str());
1.1.1.12  root     2492:        return CmdAct::Stay;
1.1       root     2493: }
                   2494: 
1.1.1.5   root     2495: // サブルーチンとループを飛ばしながら count 命令ステップ実行
                   2496: // n [<count>?:1] (途中レジスタを表示しない)
1.1.1.12  root     2497: Debugger::CmdAct
1.1       root     2498: Debugger::cmd_n()
                   2499: {
1.1.1.12  root     2500:        return cmd_n_common(false);
1.1.1.5   root     2501: }
                   2502: 
                   2503: // サブルーチンとループを飛ばしながら count 命令ステップ実行
                   2504: // nt [<count>?:1] (1命令ずつレジスタを表示する)
1.1.1.12  root     2505: Debugger::CmdAct
1.1.1.5   root     2506: Debugger::cmd_nt()
                   2507: {
1.1.1.12  root     2508:        return cmd_n_common(true);
1.1.1.5   root     2509: }
                   2510: 
                   2511: // n と nt の共通部分
1.1.1.12  root     2512: Debugger::CmdAct
1.1.1.5   root     2513: Debugger::cmd_n_common(bool trace)
                   2514: {
1.1.1.13  root     2515:        t_enable = trace ? curmd : NULL;
1.1.1.5   root     2516: 
1.1.1.3   root     2517:        if (args.size() > 1) {
1.1       root     2518:                int count;
1.1.1.3   root     2519:                count = strtol(args[1].c_str(), NULL, 10);
1.1       root     2520:                if (count < 1) {
1.1.1.11  root     2521:                        fprintf(cons, " invalid step count: %d\n", count);
1.1.1.12  root     2522:                        return CmdAct::Stay;
1.1       root     2523:                }
                   2524: 
1.1.1.13  root     2525:                step_count = count;
1.1       root     2526:        } else {
1.1.1.13  root     2527:                step_count = 1;
1.1       root     2528:        }
1.1.1.13  root     2529:        step_type = StepType::CountSkipSub;
                   2530:        step_md = curmd;
1.1       root     2531:        SetNBreakpoint();
1.1.1.12  root     2532: 
                   2533:        return CmdAct::Leave;
1.1       root     2534: }
                   2535: 
1.1.1.5   root     2536: // n コマンド用のブレークポイントを設定する。
                   2537: // この命令語によって仕掛けるブレークポイントが変わるので、都度都度
                   2538: // 呼び出すこと。
                   2539: void
1.1       root     2540: Debugger::SetNBreakpoint()
                   2541: {
1.1.1.15! root     2542:        busaddr laddr(curmd->GetPC(), busaddr::Fetch, curmd->IsSuper());
        !          2543:        DebuggerMemoryStream mem(curmd, laddr);
1.1.1.5   root     2544: 
1.1.1.6   root     2545:        // 命令がサブルーチンやトラップなどステップインできる命令か。
1.1.1.13  root     2546:        if (curmd->IsOpStepIn(mem)) {
1.1.1.5   root     2547:                // この次の命令にブレークをかける
1.1.1.13  root     2548:                if (curmd->inst_bytes_fixed != 0) {
1.1.1.5   root     2549:                        // 固定長命令
1.1.1.15! root     2550:                        step_addr = mem.laddr.Addr();
1.1.1.5   root     2551:                } else {
1.1.1.13  root     2552:                        // 可変長なら逆アセンブルしてみるしか。
                   2553:                        // 表示用文字列は作る必要ないけど、処理分けるのも手間だし
                   2554:                        // ここは人間がコマンド打った時しかこないので気にしない。
                   2555: 
                   2556:                        // IsOpStepIn() が mem を進めるので戻す。
                   2557:                        mem.ResetAddr(curmd->GetPC());
1.1.1.5   root     2558:                        // XXX 失敗したらどうすべ
1.1.1.13  root     2559:                        curmd->Disassemble(mem);
1.1.1.15! root     2560:                        step_addr = mem.laddr.Addr();
1.1.1.5   root     2561:                }
1.1       root     2562:        } else {
1.1.1.5   root     2563:                // そうでなければ1命令進める。
1.1.1.13  root     2564:                step_addr = (int64)-1;
1.1       root     2565:        }
                   2566: }
                   2567: 
1.1.1.2   root     2568: // デバッガ終了コマンド
1.1.1.12  root     2569: Debugger::CmdAct
1.1.1.2   root     2570: Debugger::cmd_q()
                   2571: {
1.1.1.11  root     2572:        fprintf(cons, "quit debugger console.\n");
1.1.1.12  root     2573:        return CmdAct::Quit;
1.1       root     2574: }
                   2575: 
1.1.1.5   root     2576: // VM リセット
1.1.1.12  root     2577: Debugger::CmdAct
1.1.1.5   root     2578: Debugger::cmd_reset()
                   2579: {
1.1.1.13  root     2580:        GetPowerDevice()->MakeResetHard();
1.1.1.12  root     2581:        return CmdAct::Leave;
1.1.1.5   root     2582: }
                   2583: 
1.1.1.13  root     2584: // モニタ表示
1.1.1.12  root     2585: Debugger::CmdAct
1.1       root     2586: Debugger::cmd_show()
                   2587: {
1.1.1.3   root     2588:        if (args.size() != 2) {
1.1.1.9   root     2589:                std::vector<int> list;
                   2590: 
1.1.1.13  root     2591:                // 登録されているモニタだけを列挙
1.1.1.9   root     2592:                // (登録されていてもサブウィンドウの ID を持っているものは除外)
1.1.1.11  root     2593:                for (const auto mon : gMonitorManager->GetList()) {
1.1.1.9   root     2594:                        int id = mon->GetId();
                   2595:                        if (id <= ID_MONITOR_END) {
                   2596:                                list.push_back(id);
1.1       root     2597:                        }
                   2598:                }
1.1.1.9   root     2599: 
                   2600:                // 一覧を表示
                   2601:                std::string msg = MonitorManager::MakeListString(list);
1.1.1.11  root     2602:                fprintf(cons, "usage: show <monitor>\n");
                   2603:                fprintf(cons, "%s", msg.c_str());
1.1.1.12  root     2604:                return CmdAct::Stay;
1.1       root     2605:        }
                   2606: 
1.1.1.9   root     2607:        std::vector<Monitor *> candidates;
                   2608:        std::vector<std::string> cand_names;
1.1.1.3   root     2609:        std::string name = args[1];
1.1.1.9   root     2610: 
1.1.1.11  root     2611:        for (auto mon : gMonitorManager->GetList()) {
1.1.1.9   root     2612:                int id = mon->GetId();
1.1.1.11  root     2613:                const auto& aliases = gMonitorManager->GetAliases(id);
1.1.1.9   root     2614: 
                   2615:                bool matched = false;
                   2616:                for (const auto& alias : aliases) {
                   2617:                        // 部分一致したら名前はすべて覚えておく
                   2618:                        if (starts_with_ignorecase(alias, name)) {
                   2619:                                cand_names.push_back(alias);
                   2620:                                matched = true;
                   2621:                        }
                   2622:                }
                   2623: 
                   2624:                // 同じモニタで別名が複数回部分一致しても、
                   2625:                // モニタのほうは1回として数える
                   2626:                if (matched) {
                   2627:                        candidates.push_back(mon);
                   2628:                }
                   2629:        }
                   2630: 
                   2631:        if (candidates.empty()) {
                   2632:                // 一致しない
1.1.1.11  root     2633:                fprintf(cons, "unknown monitor name: \"%s\"\n", name.c_str());
1.1.1.9   root     2634:        } else if (candidates.size() == 1) {
                   2635:                // 確定した
                   2636:                Monitor& mon = *(candidates[0]);
                   2637:                ShowMonitor(mon);
                   2638:        } else {
                   2639:                // 候補が複数あった
                   2640:                std::string candstr;
                   2641:                for (const auto& cname : cand_names) {
                   2642:                        candstr += ' ';
                   2643:                        candstr += cname;
1.1       root     2644:                }
1.1.1.11  root     2645:                fprintf(cons, "ambiguous monitor name \"%s\": candidates are%s\n",
1.1.1.9   root     2646:                        name.c_str(), candstr.c_str());
1.1       root     2647:        }
1.1.1.12  root     2648: 
                   2649:        return CmdAct::Stay;
1.1       root     2650: }
                   2651: 
1.1.1.13  root     2652: // モニタを更新して表示。
1.1.1.3   root     2653: void
1.1.1.9   root     2654: Debugger::ShowMonitor(Monitor& monitor)
1.1.1.3   root     2655: {
                   2656:        // モニタを更新
1.1.1.4   root     2657:        TextScreen ts;
                   2658: 
1.1.1.9   root     2659:        auto size = monitor.GetSize();
1.1.1.4   root     2660:        ts.Init(size.width, size.height);
                   2661: 
1.1.1.9   root     2662:        MONITOR_UPDATE(monitor, ts);
1.1.1.4   root     2663:        ShowTextScreen(ts);
1.1.1.3   root     2664: }
                   2665: 
                   2666: // テキストスクリーンを表示。
1.1       root     2667: void
1.1.1.3   root     2668: Debugger::ShowTextScreen(TextScreen& monitor)
1.1       root     2669: {
                   2670:        char sbuf[1024];        // 適当
                   2671: 
                   2672:        // monitor 内部バッファから ShiftJIS バッファを作成。
                   2673:        // その際属性もエスケープシーケンスで再現する。
                   2674:        int col = monitor.GetCol();
                   2675:        int row = monitor.GetRow();
1.1.1.8   root     2676:        const std::vector<uint16>& src = monitor.GetBuf();
1.1       root     2677: 
                   2678:        for (int y = 0; y < row; y++) {
                   2679:                int sy = y * col;
                   2680:                int sx;
                   2681:                uint attr;
                   2682:                char *d;
                   2683: 
                   2684:                memset(sbuf, 0, sizeof(sbuf));
                   2685:                d = sbuf;
                   2686:                attr = TA::Normal;
                   2687:                for (sx = 0; sx < col; sx++) {
                   2688:                        uint a  = src[sy + sx] & 0xff00;
                   2689:                        uint ch = src[sy + sx] & 0x00ff;
                   2690: 
                   2691:                        // 属性の変わり目
                   2692:                        if (attr != a) {
                   2693:                                switch (a) {
                   2694:                                 case TA::Normal:
                   2695:                                 case TA::Off:
                   2696:                                        *d++ = 0x1b;
                   2697:                                        *d++ = '[';
                   2698:                                        *d++ = 'm';
                   2699:                                        break;
1.1.1.14  root     2700:                                 case TA::Reverse:
                   2701:                                 case TA::ReverseRed:
                   2702:                                 case TA::ReverseGreen:
                   2703:                                 case TA::ReverseOrange:
1.1       root     2704:                                        *d++ = 0x1b;
                   2705:                                        *d++ = '[';
                   2706:                                        *d++ = '7';
                   2707:                                        *d++ = 'm';
                   2708:                                        break;
                   2709:                                 case TA::Disable:
                   2710:                                        *d++ = 0x1b;
                   2711:                                        *d++ = '[';
                   2712:                                        *d++ = '2';
                   2713:                                        *d++ = 'm';
                   2714:                                        break;
                   2715:                                 case TA::Em:
                   2716:                                        *d++ = 0x1b;
                   2717:                                        *d++ = '[';
                   2718:                                        *d++ = '1';
                   2719:                                        *d++ = 'm';
                   2720:                                        break;
                   2721:                                }
                   2722:                                attr = a;
                   2723:                        }
                   2724:                        *d++ = ch;
                   2725:                }
1.1.1.3   root     2726:                // 行の終わりで一旦属性を戻しておく
                   2727:                if (attr != TA::Normal) {
                   2728:                        *d++ = 0x1b;
                   2729:                        *d++ = '[';
                   2730:                        *d++ = 'm';
                   2731:                }
1.1       root     2732:                *d = '\0';
                   2733: 
                   2734:                // XXX 日本語が使われてれば UTF-8 にしないといけないはず
                   2735: 
1.1.1.11  root     2736:                fprintf(cons, "%s\n", sbuf);
1.1       root     2737:        }
                   2738: }
                   2739: 
1.1.1.5   root     2740: // ステップ実行
                   2741: // s [<count>?:1] (途中レジスタを表示しない)
1.1.1.12  root     2742: Debugger::CmdAct
1.1       root     2743: Debugger::cmd_s()
                   2744: {
1.1.1.12  root     2745:        return cmd_s_common(false);
1.1.1.5   root     2746: }
                   2747: 
                   2748: // ステップ実行
                   2749: // st [<count>?:1] (命令ごとにレジスタを表示する)
1.1.1.12  root     2750: Debugger::CmdAct
1.1.1.5   root     2751: Debugger::cmd_st()
                   2752: {
1.1.1.12  root     2753:        return cmd_s_common(true);
1.1.1.5   root     2754: }
                   2755: 
                   2756: // ステップ実行共通部分
1.1.1.12  root     2757: Debugger::CmdAct
1.1.1.5   root     2758: Debugger::cmd_s_common(bool trace)
                   2759: {
1.1.1.13  root     2760:        t_enable = trace ? curmd : NULL;
1.1.1.5   root     2761: 
1.1.1.3   root     2762:        if (args.size() > 1) {
1.1       root     2763:                int count;
1.1.1.3   root     2764:                count = strtol(args[1].c_str(), NULL, 10);
1.1       root     2765:                if (count < 1) {
1.1.1.11  root     2766:                        fprintf(cons, " invalid step count: %d\n", count);
1.1.1.12  root     2767:                        return CmdAct::Stay;
1.1       root     2768:                }
                   2769: 
1.1.1.13  root     2770:                step_count = count;
1.1       root     2771:        } else {
1.1.1.13  root     2772:                step_count = 1;
1.1       root     2773:        }
1.1.1.13  root     2774:        step_type = StepType::Count;
                   2775:        step_md = curmd;
1.1.1.12  root     2776: 
                   2777:        return CmdAct::Leave;
1.1       root     2778: }
                   2779: 
1.1.1.5   root     2780: // ステップアウト (途中レジスタを表示しない)
1.1.1.12  root     2781: Debugger::CmdAct
1.1       root     2782: Debugger::cmd_so()
                   2783: {
1.1.1.12  root     2784:        return cmd_so_common(false);
1.1.1.5   root     2785: }
                   2786: 
                   2787: // ステップアウト (命令ごとにレジスタを表示する)
1.1.1.12  root     2788: Debugger::CmdAct
1.1.1.5   root     2789: Debugger::cmd_sot()
                   2790: {
1.1.1.12  root     2791:        return cmd_so_common(true);
1.1.1.5   root     2792: }
                   2793: 
                   2794: // ステップアウトの共通部分
1.1.1.12  root     2795: Debugger::CmdAct
1.1.1.5   root     2796: Debugger::cmd_so_common(bool trace)
                   2797: {
1.1.1.13  root     2798:        t_enable = trace ? curmd : NULL;
1.1.1.5   root     2799: 
1.1.1.13  root     2800:        step_type = StepType::StepOut;
                   2801:        step_md = curmd;
                   2802:        step_md->SetStepOut();
1.1.1.12  root     2803: 
                   2804:        return CmdAct::Leave;
1.1       root     2805: }
                   2806: 
1.1.1.5   root     2807: // t は st の省略形。互換のため。
1.1.1.12  root     2808: Debugger::CmdAct
1.1       root     2809: Debugger::cmd_t()
                   2810: {
1.1.1.12  root     2811:        return cmd_st();
1.1       root     2812: }
                   2813: 
1.1.1.4   root     2814: // 引数で示されるプレフィックスなしの16進数値を返す。
                   2815: // 正しく変換できれば値を valp に格納して true を返す。
                   2816: // そうでなければ false を返す。
                   2817: bool
                   2818: Debugger::ParseVerbHex(const char *arg, uint32 *valp)
                   2819: {
                   2820:        uint32 val;
                   2821:        char *end;
                   2822: 
                   2823:        errno = 0;
                   2824:        val = strtoul(arg, &end, 16);
                   2825:        if (arg[0] == '\0' || end[0] != '\0') {
                   2826:                return false;
                   2827:        }
                   2828:        if (errno == ERANGE) {
                   2829:                return false;
                   2830:        }
                   2831: 
                   2832:        *valp = val;
                   2833:        return true;
                   2834: }
                   2835: 
1.1       root     2836: // 引数で示されるアドレスを返す。
                   2837: // '%' で始まればレジスタ。
                   2838: // そうでなければ16進数値。
                   2839: // アドレスが正しく取得できればアドレスを addr に格納し真を返す。
                   2840: // そうでなければエラーメッセージを表示して偽を返す。
                   2841: bool
1.1.1.4   root     2842: Debugger::ParseAddr(const char *arg, uint32 *addrp)
1.1       root     2843: {
1.1.1.4   root     2844:        uint32 addr;
1.1       root     2845:        char *end;
                   2846: 
                   2847:        if (arg[0] == '%') {
1.1.1.2   root     2848:                // '%' から始まればレジスタ
                   2849:                uint64 r;
1.1.1.13  root     2850:                r = curmd->GetRegAddr(arg + 1);
1.1.1.2   root     2851:                if ((int64)r < 0) {
                   2852: #if 0 // not yet
1.1.1.11  root     2853:                        fprintf(cons, "valid register name are: %s%s\n",
1.1       root     2854:                                "%d0-%d7,%a0-%a7,%sp,%usp,%isp,%pc",
                   2855:                                ",%msp,%vbr,%srp,%crp");
1.1.1.2   root     2856: #endif
1.1.1.11  root     2857:                        fprintf(cons, "invalid register name\n");
1.1       root     2858:                        return false;
                   2859:                }
1.1.1.2   root     2860:                addr = (uint32)r;
1.1       root     2861:        } else {
                   2862:                /* そうでなければ番地指定 */
                   2863:                errno = 0;
                   2864:                addr = strtoul(arg, &end, 16);
                   2865:                if (arg[0] == '\0' || end[0] != '\0') {
1.1.1.11  root     2866:                        fprintf(cons, "invalid address: '%s'\n", arg);
1.1       root     2867:                        return false;
                   2868:                }
                   2869:                if (errno == ERANGE) {
1.1.1.11  root     2870:                        fprintf(cons, "out of range: %s\n", arg);
1.1       root     2871:                        return false;
                   2872:                }
                   2873:        }
                   2874: 
                   2875:        *addrp = addr;
                   2876:        return true;
                   2877: }
                   2878: 
1.1.1.12  root     2879: // 引数で示される時間間隔を返す。
                   2880: bool
                   2881: Debugger::ParseTime(const char *arg, uint64 *timep)
                   2882: {
                   2883:        double f;
                   2884:        uint64 t;
                   2885:        char *end;
                   2886: 
                   2887:        errno = 0;
                   2888:        f = strtod(arg, &end);
                   2889:        if (end == arg) {
                   2890:                fprintf(cons, "invalid time: %s\n", arg);
                   2891:                return false;
                   2892:        }
                   2893:        if (errno == ERANGE || f < 0 || std::isinf(f) || std::isnan(f)) {
                   2894:                fprintf(cons, "out of range: %s\n", arg);
                   2895:                return false;
                   2896:        }
                   2897: 
                   2898:        // 単位省略は nsec とする。
                   2899:        // 接尾語は "nsec" とかの他 C++ 書式で使ってる "_nsec" も受け付けておく。
                   2900:        if (*end == '\0' || strcmp(end, "nsec") == 0 || strcmp(end, "_nsec") == 0) {
                   2901:                t = f * 1_nsec;
                   2902:        } else if (strcmp(end, "usec") == 0 || strcmp(end, "_usec") == 0) {
                   2903:                t = f * 1_usec;
                   2904:        } else if (strcmp(end, "msec") == 0 || strcmp(end, "_msec") == 0) {
                   2905:                t = f * 1_msec;
                   2906:        } else if (strcmp(end, "sec") == 0 || strcmp(end, "_sec") == 0) {
                   2907:                t = f * 1_sec;
                   2908:        } else {
                   2909:                fprintf(cons, "unknown time suffix: %s\n", arg);
                   2910:                return false;
                   2911:        }
                   2912: 
                   2913:        *timep = t;
                   2914:        return true;
                   2915: }
                   2916: 
1.1.1.4   root     2917: 
                   2918: //
1.1.1.5   root     2919: // MD
                   2920: //
                   2921: 
1.1.1.13  root     2922: // コンストラクタ
                   2923: DebuggerMD::DebuggerMD(Debugger *parent_, CPUArch arch_)
                   2924: {
                   2925:        parent = parent_;
                   2926:        arch = arch_;
                   2927: 
                   2928:        bv_vector = -1;
                   2929: }
                   2930: 
1.1.1.6   root     2931: // デストラクタ
                   2932: DebuggerMD::~DebuggerMD()
                   2933: {
1.1.1.5   root     2934: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.