History

Update debugger to use exceptions for internal command processing.

arr committed 10 years ago

Commit 8549d341 parent 682a72b1 file · +47 −71Browse files

Showing the changes to src/exec/debugger.cpp (1 of 1 file). Show all

Unified · Split

src/exec/debugger.cpp+47−71
@@ -51,7 +51,14 @@ namespace · 50 unmodified lines
5151 #undef CSI
5252 }
5353
54− void debug_print_error(const std::string &str)
54+ struct debug_cmd_error_t : std::runtime_error
55+ {
56+ debug_cmd_error_t(const char *message)
57+ : std::runtime_error{ message }
58+ { }
59+ };
60+
61+ void debug_print_error(const char *str)
5562 {
5663 std::cout
5764 << console_modifiers::red_font
@@ -62,6 +69,11 @@ namespace · 4 unmodified lines
6269 << "\n";
6370 }
6471
72+ void debug_print_error(const std::string &str)
73+ {
74+ debug_print_error(str.c_str());
75+ }
76+
6577 void debug_print_info(const std::string &str)
6678 {
6779 std::cout
@@ -290,7 +302,7 @@ namespace · 222 unmodified lines
290302 cxt.exit_break = true;
291303 }
292304
293− void cmd_term(const std::vector<std::string> &, dbg_cmd_cxt_t &cxt)
305+ void cmd_term(const std::vector<std::string> &, dbg_cmd_cxt_t &)
294306 {
295307 throw vm_user_exception{ "Debugger signaled VM termination" };
296308 }
@@ -332,10 +344,7 @@ namespace · 35 unmodified lines
332344 void cmd_breakpoint_set(const std::vector<std::string> &a, dbg_cmd_cxt_t &cxt)
333345 {
334346 if (a.size() < 3)
335− {
336− debug_print_error("Must supply an IP and module ID");
337− return;
338− }
347+ throw debug_cmd_error_t{ "Must supply an IP and module ID" };
339348
340349 vm_pc_t pc;
341350 try
@@ -344,8 +353,7 @@ namespace · 2 unmodified lines
344353 }
345354 catch (...)
346355 {
347− debug_print_error("Invalid IP format");
348− return;
356+ throw debug_cmd_error_t{ "Invalid IP format" };
349357 }
350358
351359 uint32_t module_id;
@@ -355,8 +363,7 @@ namespace · 3 unmodified lines
355363 }
356364 catch (...)
357365 {
358− debug_print_error("Invalid module ID format");
359− return;
366+ throw debug_cmd_error_t{ "Invalid module ID format" };
360367 }
361368
362369 auto module = std::shared_ptr<vm_module_t>{};
@@ -372,20 +379,15 @@ namespace · 9 unmodified lines
372379
373380 if (module == nullptr)
374381 {
375− debug_print_error("Invalid module ID");
376− return;
382+ throw debug_cmd_error_t{ "Invalid module ID" };
377383 }
378384 else if (util::has_flag(module->header.runtime_flag, runtime_flags_t::builtin))
379385 {
380− debug_print_error("Unable to set breakpoint in built-in module");
381− return;
386+ throw debug_cmd_error_t{ "Unable to set breakpoint in built-in module" };
382387 }
383388
384389 if (pc < 0 || module->code_section.size() <= static_cast<std::size_t>(pc))
385− {
386− debug_print_error("Invalid IP for module");
387− return;
388− }
390+ throw debug_cmd_error_t{ "Invalid IP for module" };
389391
390392 cxt.dbg.breakpoint_cookies.push_back(cxt.dbg.controller->set_breakpoint(module, pc));
391393 }
@@ -393,10 +395,7 @@ namespace · 1 unmodified line
393395 void cmd_breakpoint_clear(const std::vector<std::string> &a, dbg_cmd_cxt_t &cxt)
394396 {
395397 if (a.size() == 1)
396− {
397− debug_print_error("Must supply a breakpoint ID or wildcard");
398− return;
399− }
398+ throw debug_cmd_error_t{ "Must supply a breakpoint ID or wildcard" };
400399
401400 auto controller = cxt.dbg.controller;
402401 if (a[1].compare("*") == 0)
@@ -417,8 +416,7 @@ namespace · 14 unmodified lines
417416 }
418417 catch (...)
419418 {
420− debug_print_error("Invalid breakpoint ID format");
421− return;
419+ throw debug_cmd_error_t{ "Invalid breakpoint ID format" };
422420 }
423421
424422 auto cookie_iter = std::find_if(
@@ -426,10 +424,7 @@ namespace · 1 unmodified line
426424 std::end(cxt.dbg.breakpoint_cookies),
427425 [breakpoint_cookie] (const cookie_t c) { return c == breakpoint_cookie; });
428426 if (cookie_iter == cxt.dbg.breakpoint_cookies.end())
429− {
430− debug_print_error("Unknown breakpoint ID");
431− return;
432− }
427+ throw debug_cmd_error_t{ "Unknown breakpoint ID" };
433428
434429 // Clear the breakpoint
435430 controller->clear_breakpoint(breakpoint_cookie);
@@ -446,7 +441,7 @@ namespace · 10 unmodified lines
446441 const auto details = cxt.dbg.controller->get_breakpoint_details(cookie);
447442 msg << std::setw(3) << cookie << ": "
448443 << std::setw(16) << details.module->module_name->str()
449− << std::setw(0) << " @ " << details.pc << "\n";
444+ << std::setw(0) << "@" << details.pc << "\n";
450445 }
451446
452447 debug_print_info(msg.str());
@@ -474,8 +469,7 @@ namespace · 21 unmodified lines
474469 }
475470 catch (...)
476471 {
477− debug_print_error("Invalid thread ID format");
478− return;
472+ throw debug_cmd_error_t{ "Invalid thread ID format" };
479473 }
480474 }
481475
@@ -496,10 +490,7 @@ namespace · 14 unmodified lines
496490 }
497491
498492 if (thread_registers.empty())
499− {
500− debug_print_error("Unknown thread ID");
501− return;
502− }
493+ throw debug_cmd_error_t{ "Unknown thread ID" };
503494
504495 auto msg = std::stringstream{};
505496 for (auto t : thread_registers)
@@ -515,10 +506,7 @@ namespace · 9 unmodified lines
515506 void cmd_switchthread(const std::vector<std::string> &a, dbg_cmd_cxt_t &cxt)
516507 {
517508 if (a.size() == 1)
518− {
519− debug_print_error("Must supply a target thread ID");
520− return;
521− }
509+ throw debug_cmd_error_t{ "Must supply a target thread ID" };
522510
523511 uint32_t thread_id;
524512 try
@@ -527,8 +515,7 @@ namespace · 2 unmodified lines
527515 }
528516 catch (...)
529517 {
530− debug_print_error("Invalid thread ID format");
531− return;
518+ throw debug_cmd_error_t{ "Invalid thread ID format" };
532519 }
533520
534521 auto all_threads = cxt.vm.get_scheduler_control().get_all_threads();
@@ -544,7 +531,7 @@ namespace · 9 unmodified lines
544531 }
545532 }
546533
547− debug_print_error("Unknown thread ID");
534+ throw debug_cmd_error_t{ "Unknown thread ID" };
548535 }
549536
550537 void cmd_disassemble(const std::vector<std::string> &a, dbg_cmd_cxt_t &cxt)
@@ -558,8 +545,7 @@ namespace · 7 unmodified lines
558545 }
559546 catch (...)
560547 {
561− debug_print_error("Invalid disassemble count format");
562− return;
548+ throw debug_cmd_error_t{ "Invalid disassemble count format" };
563549 }
564550 }
565551
@@ -589,10 +575,7 @@ namespace · 23 unmodified lines
589575 void cmd_examine(const std::vector<std::string> &a, dbg_cmd_cxt_t &cxt)
590576 {
591577 if (a.size() < 3)
592− {
593− debug_print_error("Invalid number of arguments");
594− return;
595− }
578+ throw debug_cmd_error_t{ "Invalid number of arguments" };
596579
597580 std::shared_ptr<const type_descriptor_t> base_type;
598581 word_t *base_pointer;
@@ -606,18 +589,14 @@ namespace · 7 unmodified lines
606589 {
607590 auto frame = cxt.current_register->stack.peek_frame();
608591 if (frame == nullptr)
609− {
610− debug_print_error("Invalid frame pointer ");
611− return;
612− }
592+ throw debug_cmd_error_t{ "Invalid frame pointer " };
613593
614594 base_type = frame->frame_type;
615595 base_pointer = reinterpret_cast<word_t *>(frame->base());
616596 }
617597 else
618598 {
619− debug_print_error("Invalid memory base pointer");
620− return;
599+ throw debug_cmd_error_t{ "Invalid memory base pointer" };
621600 }
622601
623602 assert(base_type != nullptr && base_pointer != nullptr);
@@ -635,15 +614,11 @@ namespace · 11 unmodified lines
635614 }
636615 catch (...)
637616 {
638− debug_print_error("Invalid memory offset format");
639− return;
617+ throw debug_cmd_error_t{ "Invalid memory offset format" };
640618 }
641619
642620 if (0 > byte_offset1 || byte_offset1 >= base_type->size_in_bytes)
643− {
644− debug_print_error("First offset out of range");
645− return;
646− }
621+ throw debug_cmd_error_t{ "First offset out of range" };
647622
648623 auto msg = std::stringstream{};
649624 if (byte_offset2 != -1)
@@ -661,16 +636,10 @@ namespace · 11 unmodified lines
661636 if (byte_offset2 != -1)
662637 {
663638 if (alloc_to_examine == nullptr)
664− {
665− debug_print_error("First offset is nil");
666− return;
667− }
639+ throw debug_cmd_error_t{ "First offset is nil" };
668640
669641 if (0 > byte_offset2 || byte_offset2 >= alloc_to_examine->alloc_type->size_in_bytes)
670− {
671− debug_print_error("Second offset out of range");
672− return;
673− }
642+ throw debug_cmd_error_t{ "Second offset out of range" };
674643
675644 // Update data pointer for 2nd indirection
676645 base_pointer = reinterpret_cast<word_t *>(alloc_to_examine->get_allocation());
@@ -862,9 +831,16 @@ namespace · 185 unmodified lines
862831 continue;
863832 }
864833
865− cmd_exec(cmd_tokens, cxt);
866− if (cxt.exit_break)
867− break;
834+ try
835+ {
836+ cmd_exec(cmd_tokens, cxt);
837+ if (cxt.exit_break)
838+ break;
839+ }
840+ catch (const debug_cmd_error_t &e)
841+ {
842+ debug_print_error(e.what());
843+ }
868844 }
869845 }
870846 }