Skip to content

Commit 7e3b98d

Browse files
committed
debugger: add printf/logerror nul-terminated string format
* also support lower-case hexadecimal format
1 parent 8dbb5b3 commit 7e3b98d

File tree

2 files changed

+65
-63
lines changed

2 files changed

+65
-63
lines changed

src/emu/debug/debugcmd.cpp

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,14 @@ void debugger_commands::execute_print(const std::vector<std::string_view> &param
469469
mini_printf - safe printf to a buffer
470470
-------------------------------------------------*/
471471

472-
bool debugger_commands::mini_printf(std::ostream &stream, std::string_view format, int params, u64 *param)
472+
bool debugger_commands::mini_printf(std::ostream &stream, const std::vector<std::string_view> &params)
473473
{
474+
std::string_view format(params[0]);
474475
auto f = format.begin();
475476

477+
int param = 1;
478+
u64 number;
479+
476480
// parse the string looking for % signs
477481
while (f != format.end())
478482
{
@@ -517,70 +521,87 @@ bool debugger_commands::mini_printf(std::ostream &stream, std::string_view forma
517521
break;
518522

519523
case 'X':
524+
if (param < params.size() && m_console.validate_number_parameter(params[param++], number))
525+
util::stream_format(stream, zerofill ? "%0*X" : "%*X", width, number);
526+
else
527+
{
528+
m_console.printf("Not enough parameters for format!\n");
529+
return false;
530+
}
531+
break;
520532
case 'x':
521-
if (params == 0)
533+
if (param < params.size() && m_console.validate_number_parameter(params[param++], number))
534+
util::stream_format(stream, zerofill ? "%0*x" : "%*x", width, number);
535+
else
522536
{
523537
m_console.printf("Not enough parameters for format!\n");
524538
return false;
525539
}
526-
if (u32(*param >> 32) != 0)
527-
util::stream_format(stream, zerofill ? "%0*X" : "%*X", (width <= 8) ? 1 : width - 8, u32(*param >> 32));
528-
else if (width > 8)
529-
util::stream_format(stream, zerofill ? "%0*X" : "%*X", width - 8, 0);
530-
util::stream_format(stream, zerofill ? "%0*X" : "%*X", (width < 8) ? width : 8, u32(*param));
531-
param++;
532-
params--;
533540
break;
534541

535542
case 'O':
536543
case 'o':
537-
if (params == 0)
544+
if (param < params.size() && m_console.validate_number_parameter(params[param++], number))
545+
util::stream_format(stream, zerofill ? "%0*o" : "%*o", width, number);
546+
else
538547
{
539548
m_console.printf("Not enough parameters for format!\n");
540549
return false;
541550
}
542-
if (u32(*param >> 60) != 0)
543-
{
544-
util::stream_format(stream, zerofill ? "%0*o" : "%*o", (width <= 20) ? 1 : width - 20, u32(*param >> 60));
545-
util::stream_format(stream, "%0*o", 10, u32(BIT(*param, 30, 30)));
546-
}
547-
else
548-
{
549-
if (width > 20)
550-
util::stream_format(stream, zerofill ? "%0*o" : "%*o", width - 20, 0);
551-
if (u32(BIT(*param, 30, 30)) != 0)
552-
util::stream_format(stream, zerofill ? "%0*o" : "%*o", (width <= 10) ? 1 : width - 10, u32(BIT(*param, 30, 30)));
553-
else if (width > 10)
554-
util::stream_format(stream, zerofill ? "%0*o" : "%*o", width - 10, 0);
555-
}
556-
util::stream_format(stream, zerofill ? "%0*o" : "%*o", (width < 10) ? width : 10, u32(BIT(*param, 0, 30)));
557-
param++;
558-
params--;
559551
break;
560552

561553
case 'D':
562554
case 'd':
563-
if (params == 0)
555+
if (param < params.size() && m_console.validate_number_parameter(params[param++], number))
556+
util::stream_format(stream, zerofill ? "%0*d" : "%*d", width, number);
557+
else
564558
{
565559
m_console.printf("Not enough parameters for format!\n");
566560
return false;
567561
}
568-
util::stream_format(stream, zerofill ? "%0*d" : "%*d", width, u32(*param));
569-
param++;
570-
params--;
571562
break;
563+
572564
case 'C':
573565
case 'c':
574-
if (params == 0)
566+
if (param < params.size() && m_console.validate_number_parameter(params[param++], number))
567+
stream << char(number);
568+
else
575569
{
576570
m_console.printf("Not enough parameters for format!\n");
577571
return false;
578572
}
579-
stream << char(*param);
580-
param++;
581-
params--;
582573
break;
583574

575+
case 's':
576+
if ((param + 1) < params.size())
577+
{
578+
address_space *space, *tspace;
579+
std::string s;
580+
581+
if (m_console.validate_device_space_parameter(params[param + 0], -1, space) && m_console.validate_number_parameter(params[param + 1], number))
582+
{
583+
for (u32 address = u32(number), taddress; space->device().memory().translate(space->spacenum(), device_memory_interface::TR_READ, taddress = address, tspace); address++)
584+
{
585+
u8 const data = tspace->read_byte(taddress);
586+
587+
if (data)
588+
s += data;
589+
else
590+
break;
591+
}
592+
593+
util::stream_format(stream, "%*s", width, s);
594+
param += 2;
595+
}
596+
else
597+
return false;
598+
}
599+
else
600+
{
601+
m_console.printf("Not enough parameters for format!\n");
602+
return false;
603+
}
604+
break;
584605
}
585606
}
586607

@@ -630,15 +651,9 @@ void debugger_commands::execute_index_command(std::vector<std::string_view> cons
630651

631652
void debugger_commands::execute_printf(const std::vector<std::string_view> &params)
632653
{
633-
/* validate the other parameters */
634-
u64 values[MAX_COMMAND_PARAMS];
635-
for (int i = 1; i < params.size(); i++)
636-
if (!m_console.validate_number_parameter(params[i], values[i]))
637-
return;
638-
639654
/* then do a printf */
640655
std::ostringstream buffer;
641-
if (mini_printf(buffer, params[0], params.size() - 1, &values[1]))
656+
if (mini_printf(buffer, params))
642657
m_console.printf("%s\n", std::move(buffer).str());
643658
}
644659

@@ -649,15 +664,9 @@ void debugger_commands::execute_printf(const std::vector<std::string_view> &para
649664

650665
void debugger_commands::execute_logerror(const std::vector<std::string_view> &params)
651666
{
652-
/* validate the other parameters */
653-
u64 values[MAX_COMMAND_PARAMS];
654-
for (int i = 1; i < params.size(); i++)
655-
if (!m_console.validate_number_parameter(params[i], values[i]))
656-
return;
657-
658667
/* then do a printf */
659668
std::ostringstream buffer;
660-
if (mini_printf(buffer, params[0], params.size() - 1, &values[1]))
669+
if (mini_printf(buffer, params))
661670
m_machine.logerror("%s", std::move(buffer).str());
662671
}
663672

@@ -668,15 +677,9 @@ void debugger_commands::execute_logerror(const std::vector<std::string_view> &pa
668677

669678
void debugger_commands::execute_tracelog(const std::vector<std::string_view> &params)
670679
{
671-
/* validate the other parameters */
672-
u64 values[MAX_COMMAND_PARAMS];
673-
for (int i = 1; i < params.size(); i++)
674-
if (!m_console.validate_number_parameter(params[i], values[i]))
675-
return;
676-
677680
/* then do a printf */
678681
std::ostringstream buffer;
679-
if (mini_printf(buffer, params[0], params.size() - 1, &values[1]))
682+
if (mini_printf(buffer, params))
680683
m_console.get_visible_cpu()->debug()->trace_printf("%s", std::move(buffer).str());
681684
}
682685

@@ -689,7 +692,6 @@ void debugger_commands::execute_tracesym(const std::vector<std::string_view> &pa
689692
{
690693
// build a format string appropriate for the parameters and validate them
691694
std::stringstream format;
692-
u64 values[MAX_COMMAND_PARAMS];
693695
for (int i = 0; i < params.size(); i++)
694696
{
695697
// find this symbol
@@ -704,15 +706,15 @@ void debugger_commands::execute_tracesym(const std::vector<std::string_view> &pa
704706
util::stream_format(format, "%s=%s ",
705707
params[i],
706708
sym->format().empty() ? "%16X" : sym->format());
707-
708-
// validate the parameter
709-
if (!m_console.validate_number_parameter(params[i], values[i]))
710-
return;
711709
}
712710

711+
// build parameters for printf
712+
std::vector<std::string_view> printf_params(params);
713+
printf_params.insert(printf_params.begin(), format.str());
714+
713715
// then do a printf
714716
std::ostringstream buffer;
715-
if (mini_printf(buffer, format.str(), params.size(), values))
717+
if (mini_printf(buffer, printf_params))
716718
m_console.get_visible_cpu()->debug()->trace_printf("%s", std::move(buffer).str());
717719
}
718720

src/emu/debug/debugcmd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class debugger_commands
7373
u64 global_get(global_entry *global);
7474
void global_set(global_entry *global, u64 value);
7575

76-
bool mini_printf(std::ostream &stream, std::string_view format, int params, u64 *param);
76+
bool mini_printf(std::ostream &stream, const std::vector<std::string_view> &params);
7777
template <typename T>
7878
void execute_index_command(std::vector<std::string_view> const &params, T &&apply, char const *unused_message);
7979

0 commit comments

Comments
 (0)