Skip to content

Commit

Permalink
target: semihosting: Drop gdb_hostio for hosted completely
Browse files Browse the repository at this point in the history
* Drop all the function pointers from struct target_controller and its instance, gdb_controller;
* Hide the entirety of gdb_hostio.c behind PC_HOSTED macro,
  use `#endif /* PC_HOSTED */` style, like header guards, for long sections;
* Hide all the wrappers like tc_write();
* Hide parsing the F-packet reply from gdb_main_loop (as BMDA is not expected to emit F-requests)
  • Loading branch information
ALTracer committed Nov 11, 2023
1 parent e00db7c commit d81f6a5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/gdb_hostio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "gdb_hostio.h"
#include "gdb_packet.h"

#if PC_HOSTED == 0

int hostio_reply(target_controller_s *const tc, char *const pbuf, const int len)
{
(void)len;
Expand Down Expand Up @@ -149,3 +151,4 @@ int hostio_system(target_controller_s *tc, target_addr_t cmd, size_t cmd_len)
gdb_putpacket_f("Fsystem,%08" PRIx32 "/%zX", cmd, cmd_len);
return hostio_get_response(tc);
}
#endif /* PC_HOSTED */
10 changes: 9 additions & 1 deletion src/gdb_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ target_controller_s gdb_controller = {
.destroy_callback = gdb_target_destroy_callback,
.printf = gdb_target_printf,

#if PC_HOSTED == 0
.open = hostio_open,
.close = hostio_close,
.read = hostio_read,
Expand All @@ -128,12 +129,17 @@ target_controller_s gdb_controller = {
.gettimeofday = hostio_gettimeofday,
.isatty = hostio_isatty,
.system = hostio_system,
#endif
};

/* execute gdb remote command stored in 'pbuf'. returns immediately, no busy waiting. */

int gdb_main_loop(target_controller_s *tc, char *pbuf, size_t pbuf_size, size_t size, bool in_syscall)
{
#if PC_HOSTED == 1
(void)tc;
(void)in_syscall;
#endif
bool single_step = false;

/* GDB protocol main loop */
Expand Down Expand Up @@ -280,14 +286,16 @@ int gdb_main_loop(target_controller_s *tc, char *pbuf, size_t pbuf_size, size_t
break;
}

case 'F': /* Semihosting call finished */
#if PC_HOSTED == 0
case 'F': /* File-I/O call finished */
if (in_syscall)
return hostio_reply(tc, pbuf, size);
else {
DEBUG_GDB("*** F packet when not in syscall! '%s'\n", pbuf);
gdb_putpacketz("");
}
break;
#endif

case '!': /* Enable Extended GDB Protocol. */
/*
Expand Down
2 changes: 2 additions & 0 deletions src/include/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ struct target_controller {
void (*destroy_callback)(target_controller_s *, target_s *target);
void (*printf)(target_controller_s *, const char *fmt, va_list);

#if PC_HOSTED == 0
/* Interface to host system calls */
int (*open)(target_controller_s *, target_addr_t path, size_t path_len, target_open_flags_e flags, mode_t mode);
int (*close)(target_controller_s *, int fd);
Expand All @@ -170,6 +171,7 @@ struct target_controller {
int (*gettimeofday)(target_controller_s *, target_addr_t tv, target_addr_t tz);
int (*isatty)(target_controller_s *, int fd);
int (*system)(target_controller_s *, target_addr_t cmd, size_t cmd_len);
#endif
target_errno_e errno_;
bool interrupted;
};
Expand Down
6 changes: 4 additions & 2 deletions src/target/target.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ void tc_printf(target_s *t, const char *fmt, ...)
va_end(ap);
}

#if PC_HOSTED == 0
/* Interface to host system calls */
int tc_open(target_s *t, target_addr_t path, size_t plen, target_open_flags_e flags, mode_t mode)
{
Expand Down Expand Up @@ -602,7 +603,7 @@ int tc_read(target_s *t, int fd, target_addr_t buf, unsigned int count)

int tc_write(target_s *t, int fd, target_addr_t buf, unsigned int count)
{
#if PC_HOSTED == 0
/* BMP feature: redirect semihosting writes to debug_serial */
if (t->stdout_redirected && (fd == STDOUT_FILENO || fd == STDERR_FILENO)) {
while (count) {
uint8_t tmp[STDOUT_READ_BUF_SIZE];
Expand All @@ -616,8 +617,8 @@ int tc_write(target_s *t, int fd, target_addr_t buf, unsigned int count)
}
return 0;
}
#endif

/* Delegate to gdb_hostio */
if (t->tc->write == NULL)
return 0;
return t->tc->write(t->tc, fd, buf, count);
Expand Down Expand Up @@ -688,3 +689,4 @@ int tc_system(target_s *t, target_addr_t cmd, size_t cmdlen)
}
return t->tc->system(t->tc, cmd, cmdlen);
}
#endif /* PC_HOSTED */

0 comments on commit d81f6a5

Please sign in to comment.