822 lines · 23.9 KB · C++
RawDownload
        const auto pointer_size = static_cast<int>(sizeof(pointer_t));        auto byte_offset1 = int{ 0 };        auto byte_offset2 = int{ -1 };        try        {            byte_offset1 = std::stoi(a[2]);             // The 4th argument is optional            if (a.size() >= 4)                byte_offset2 = std::stoi(a[3]);        }        catch (...)        {            debug_print_error("Invalid memory offset format");            return;        }         if (0 > byte_offset1 || byte_offset1 >= base_type->size_in_bytes)        {            debug_print_error("First offset out of range");            return;        }         auto msg = std::stringstream{};        if (byte_offset2 != -1)            msg << "  " << byte_offset2 << "(" << byte_offset1 << "(" << a[1] << ")):\n";        else            msg << "  " << byte_offset1 << "(" << a[1] << "):\n";         auto value_is_pointer = bool{ false };        const auto pointer_offset1 = byte_offset1 / pointer_size;        auto value_to_examine = reinterpret_cast<pointer_t>(base_pointer[pointer_offset1]);        if (runtime::is_offset_pointer(*base_type, pointer_offset1))        {            value_is_pointer = true;            auto alloc_to_examine = vm_alloc_t::from_allocation(value_to_examine);            if (byte_offset2 != -1)            {                if (alloc_to_examine == nullptr)                {                    debug_print_error("First offset is nil");                    return;                }                 if (0 > byte_offset2 || byte_offset2 >= alloc_to_examine->alloc_type->size_in_bytes)                {                    debug_print_error("Second offset out of range");                    return;                }                 // Update data pointer for 2nd indirection                base_pointer = reinterpret_cast<word_t *>(alloc_to_examine->get_allocation());                base_type = alloc_to_examine->alloc_type;                 const auto pointer_offset2 = byte_offset2 / pointer_size;                value_to_examine = reinterpret_cast<pointer_t>(base_pointer[pointer_offset2]);                value_is_pointer = runtime::is_offset_pointer(*base_type, pointer_offset2);            }        }         if (value_is_pointer)            msg << "  " << vm_alloc_t::from_allocation(value_to_examine);        else            msg << "  " << std::setw(8) << std::showbase << std::hex << value_to_examine << std::dec;         debug_print_info(msg.str());    }     // Forward declaration    void print_help();     void cmd_help(const std::vector<std::string> &, dbg_cmd_cxt_t &)    {        print_help();    }     // ee    void cmd_tlk_tilde_star(const std::vector<std::string> &, dbg_cmd_cxt_t &)    {        std::cout            << console_modifiers::black_font            << console_modifiers::white_bkg            << "H"            << console_modifiers::reset_all            << console_modifiers::red_font            << console_modifiers::bold            << "e"            << console_modifiers::green_font            << console_modifiers::bold            << "l"            << console_modifiers::yellow_font            << console_modifiers::bold            << "l"            << console_modifiers::blue_font            << console_modifiers::bold            << "o"            << console_modifiers::magenta_font            << console_modifiers::bold            << ", "            << console_modifiers::black_font            << console_modifiers::white_bkg            << "W"            << console_modifiers::reset_all            << console_modifiers::red_font            << console_modifiers::bold            << "o"            << console_modifiers::green_font            << console_modifiers::bold            << "r"            << console_modifiers::yellow_font            << console_modifiers::bold            << "l"            << console_modifiers::blue_font            << console_modifiers::bold            << "d"            << console_modifiers::magenta_font            << console_modifiers::bold            << "!\n"            << console_modifiers::reset_all;    }     const dbg_cmd_t cmds[] =    {        { "c", "Continue", nullptr, nullptr, cmd_continue },        { "r", "Print registers", nullptr, nullptr, cmd_print_registers },        { "t", "Print all threads", nullptr, nullptr, cmd_print_threads },        { "m", "Print all loaded modules", nullptr, nullptr, cmd_print_modules },        { "sw", "Stack walk/back trace", "bt ([0-9]+|\\*)?", "bt, bt 34, bt *", cmd_stacktrace },        { "st", "Switch to thread", "st [0-9]+", "st 42", cmd_switchthread },        { "d", "Disassemble the next/previous N instructions", "d (-?[0-9]+)?", "d, d -4, d 5", cmd_disassemble },        { "x", "Examine memory", "x [mp|fp] [0-9]+ ([0-9]+)?", "x mp 3, x fp 20 6", cmd_examine },        { "?", "Print help", nullptr, nullptr, cmd_help },         // Undocumented commands        { "tlk~*", nullptr, nullptr, nullptr, cmd_tlk_tilde_star }    };     dbg_cmd_exec_t find_cmd(const std::string &cmd_maybe)    {        for (auto i = std::size_t{}; i < sizeof(cmds) / sizeof(cmds[0]); ++i)        {            if (cmd_maybe.compare(cmds[i].cmd) == 0)                return cmds[i].exec;        }         // [TODO] Didn't find the cmd, try ignoring case?        return nullptr;    }     void print_help()    {        auto ss = std::stringstream{};        ss << "Commands:\n";        for (auto i = std::size_t{}; i < sizeof(cmds) / sizeof(cmds[0]); ++i)        {            auto &c = cmds[i];             // Ignore if undocumented            if (c.description == nullptr)                continue;             ss << "  " << c.cmd << "\t" << c.description << "\n";             if (c.syntax != nullptr)                ss << "\t  Syntax: " << c.syntax << "\n";             if (c.example != nullptr)                ss << "\t  Example(s): " << c.example << "\n";        }         debug_print_info(ss.str());    }     // split the string using the supplied delimiter    std::vector<std::string> split(const std::string &str, const char delim = ' ')    {        auto split_string = std::vector<std::string>{};         auto curr = str.begin();        auto end = str.cend();        while (curr != end)        {            auto curr_begin = curr;            while (curr != end && *curr != delim)                ++curr;             if (curr_begin != curr)                split_string.push_back(std::move(std::string{ curr_begin, curr }));             if (curr == end)                break;             ++curr;        }         return split_string;    }     void prompt(const vm_t &vm, const vm_registers_t &r)    {        std::cout            << console_modifiers::green_font            << console_modifiers::bold            << "'?' for help\n"            << console_modifiers::reset_all;         std::locale loc;        std::string cmd;         dbg_cmd_cxt_t cxt{ vm, r };        for (;;)        {            std::cout                << console_modifiers::green_font                << console_modifiers::bold                << cxt.current_register->thread.get_thread_id()                << " >>> "                << console_modifiers::reset_all;             std::getline(std::cin, cmd);            if (cmd.empty())                continue;             const auto cmd_tokens = split(cmd);            if (cmd_tokens.empty())                continue;             auto cmd_exec = find_cmd(cmd_tokens[0]);            if (cmd_exec == nullptr)            {                debug_print_error("Unknown command. '?' for help.");                continue;            }             cmd_exec(cmd_tokens, cxt);            if (cxt.exit_break)                break;        }    }} void debugger::on_load(vm_t &vm, vm_tool_controller_t &controller, std::size_t tool_id){    _tool_id = tool_id;    _vm = &vm;     _event_cookies.push_back(controller.subscribe_event(vm_event_t::thread_begin, [](vm_event_t, vm_event_context_t &cxt)    {        auto t = cxt.value1.thread;        auto ss = std::stringstream{};        ss << "Thread "            << t->get_thread_id()            << " started in "            << t->get_registers().module_ref->module->module_name->str()            << " @ "            << t->get_registers().pc;         debug_print_info(ss.str());    }));     _event_cookies.push_back(controller.subscribe_event(vm_event_t::thread_end, [](vm_event_t, vm_event_context_t &cxt)    {        auto ss = std::stringstream{};        ss << "Thread "            << cxt.value1.thread->get_thread_id()            << " ended.";         debug_print_info(ss.str());    }));     _event_cookies.push_back(controller.subscribe_event(vm_event_t::exception_raised, [](vm_event_t, vm_event_context_t &cxt)    {        auto ss = std::stringstream{};        ss << "Exception '"            << cxt.value2.str->str()            << "' thrown.";         debug_print_info(ss.str());    }));     _event_cookies.push_back(controller.subscribe_event(vm_event_t::exception_unhandled, [&](vm_event_t, vm_event_context_t &cxt)    {        auto ss = std::stringstream{};        ss << "Unhandled exception '"            << cxt.value2.str->str()            << "'";         debug_print_error(ss.str());        prompt(*_vm, *cxt.value1.registers);    }));     _event_cookies.push_back(controller.subscribe_event(vm_event_t::module_load, [](vm_event_t, vm_event_context_t &cxt)    {        auto m = (*cxt.value1.module);        auto ss = std::stringstream{};        ss << "Module "            << m->module_name->str()            << " loaded (";         if (!util::has_flag(m->header.runtime_flag, runtime_flags_t::builtin))            ss << cxt.value2.str->str();        else            ss << "built-in";         ss << ")";         debug_print_info(ss.str());    }));     _controller = &controller;} void debugger::on_unload(){    assert(_controller != nullptr);     for (auto ec : _event_cookies)        _controller->unsubscribe_event(ec);     _controller = nullptr;    _vm = nullptr;}

Lines 501–822 of 822. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t