Skip to content

Commit 23b1ac9

Browse files
rmn30nwf-msr
authored andcommitted
Add a hacky mechanism to track new lines in terminal and trace output and insert extra new lines to keep them separate.
1 parent c040c15 commit 23b1ac9

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

c_emulator/riscv_platform_impl.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ uint64_t rv_htif_tohost = UINT64_C(0x80001000);
4949
uint64_t rv_insns_per_tick = UINT64_C(100);
5050

5151
int term_fd = 1; // set during startup
52+
extern bool have_newline;
5253
void plat_term_write_impl(char c)
5354
{
5455
if (write(term_fd, &c, sizeof(c)) < 0) {
5556
fprintf(stderr, "Unable to write to terminal!\n");
5657
}
58+
/* Trace output always goes to stdout. If the terminal is also going to stdout
59+
* we try to separate them by inserting new lines before trace output when
60+
* necessary (see riscv_prelude.c). */
61+
if (term_fd == 1)
62+
have_newline = c == '\n';
5763
}

c_emulator/riscv_prelude.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,57 @@
22
#include "riscv_config.h"
33
#include "riscv_platform_impl.h"
44

5+
/* Attempt to keep track of whether output is at the start of a line so that we
6+
* can produce nicer output if trace output interrupts terminal output. */
7+
bool have_newline = true;
8+
9+
void ensure_newline()
10+
{
11+
if (!have_newline)
12+
fputc('\n', trace_log);
13+
}
14+
515
unit print_string(sail_string prefix, sail_string msg)
616
{
7-
printf("%s%s\n", prefix, msg);
17+
ensure_newline();
18+
fprintf(trace_log, "%s%s\n", prefix, msg);
19+
have_newline = true;
820
return UNIT;
921
}
1022

1123
unit print_instr(sail_string s)
1224
{
25+
ensure_newline();
1326
if (config_print_instr)
1427
fprintf(trace_log, "%s\n", s);
28+
have_newline = true;
1529
return UNIT;
1630
}
1731

1832
unit print_reg(sail_string s)
1933
{
34+
ensure_newline();
2035
if (config_print_reg)
2136
fprintf(trace_log, "%s\n", s);
37+
have_newline = true;
2238
return UNIT;
2339
}
2440

2541
unit print_mem_access(sail_string s)
2642
{
43+
ensure_newline();
2744
if (config_print_mem_access)
2845
fprintf(trace_log, "%s\n", s);
46+
have_newline = true;
2947
return UNIT;
3048
}
3149

3250
unit print_platform(sail_string s)
3351
{
52+
ensure_newline();
3453
if (config_print_platform)
3554
fprintf(trace_log, "%s\n", s);
55+
have_newline = true;
3656
return UNIT;
3757
}
3858

@@ -59,4 +79,4 @@ bool get_config_print_platform(unit u)
5979
bool get_config_print_exception(unit u)
6080
{
6181
return (config_print_exception) ? true : false;
62-
}
82+
}

0 commit comments

Comments
 (0)