gdb: packet and bridge logging

This commit is contained in:
Lephenixnoir 2024-04-13 09:21:26 +02:00
parent 8b09299c3a
commit ccbe0e594a
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495

View file

@ -163,20 +163,24 @@ static int accept_gdb(int listen_socket)
struct options { struct options {
bool bridge_only; bool bridge_only;
bool log_packets;
}; };
/* Parse options, returns positional arguments to forward to gdb. */ /* Parse options, returns positional arguments to forward to gdb. */
static char **parse_argv(int argc, char **argv, struct options *opts) static char **parse_argv(int argc, char **argv, struct options *opts)
{ {
int bridge_only = 0; int bridge_only = 0;
int log_packets = 0;
struct option longs[] = { struct option longs[] = {
{ "bridge-only", no_argument, &bridge_only, 1 }, { "bridge-only", no_argument, &bridge_only, 1 },
{ "log-packets", no_argument, &log_packets, 1 },
{ NULL }, { NULL },
}; };
getopt_long(argc, argv, "", longs, NULL); getopt_long(argc, argv, "", longs, NULL);
opts->bridge_only = (bridge_only != 0); opts->bridge_only = (bridge_only != 0);
opts->log_packets = (log_packets != 0);
return argv + optind + (argv[optind] && !strcmp(argv[optind], "--")); return argv + optind + (argv[optind] && !strcmp(argv[optind], "--"));
} }
@ -193,6 +197,8 @@ int main(int argc, char **argv)
struct fxlink_pollfds fxlink_polled_fds = { 0 }; struct fxlink_pollfds fxlink_polled_fds = { 0 };
struct fxlink_device_list device_list = { 0 }; struct fxlink_device_list device_list = { 0 };
char socket_path[256] = { 0 }; char socket_path[256] = { 0 };
char log_path[256] = { 0 };
FILE *log_fp = NULL;
pid_t gdb_pid = -1; pid_t gdb_pid = -1;
int ret = 1; int ret = 1;
@ -211,11 +217,24 @@ int main(int argc, char **argv)
sprintf(socket_path, "/tmp/fxsdk-gdb-bridge-%03d-%03d.socket", sprintf(socket_path, "/tmp/fxsdk-gdb-bridge-%03d-%03d.socket",
fdev->busNumber, fdev->deviceAddress); fdev->busNumber, fdev->deviceAddress);
sprintf(log_path, "/tmp/fxsdk-gdb-bridge-%03d-%03d.txt",
fdev->busNumber, fdev->deviceAddress);
int listen_socket = setup_socket(socket_path); int listen_socket = setup_socket(socket_path);
if(listen_socket < 0) if(listen_socket < 0)
goto end; goto end;
if(opts.log_packets) {
log_fp = fopen(log_path, "a");
if(!log_fp)
perror("cannot open packet log file");
else {
setvbuf(log_fp, NULL, _IOLBF, 0);
hlog("gdb");
log_("writing packets to %s\n", log_path);
}
}
if(!opts.bridge_only) { if(!opts.bridge_only) {
gdb_pid = fork_gdb(gdb_argv, socket_path); gdb_pid = fork_gdb(gdb_argv, socket_path);
if(gdb_pid == -1) if(gdb_pid == -1)
@ -269,15 +288,25 @@ int main(int argc, char **argv)
struct fxlink_message *msg; struct fxlink_message *msg;
while((msg = fxlink_device_finish_bulk_IN(fdev)) != NULL) { while((msg = fxlink_device_finish_bulk_IN(fdev)) != NULL) {
if(fxlink_message_is_apptype(msg, "fxlink", "text")) {
hlog("stub");
log_("%.*s", msg->size, (char *)msg->data);
fxlink_message_free(msg, true);
fxlink_device_start_bulk_IN(fdev);
continue;
}
if(!fxlink_message_is_apptype(msg, "gdb", "remote")) { if(!fxlink_message_is_apptype(msg, "gdb", "remote")) {
hlog("gdb"); hlog("gdb");
wlog("dropped a message of type %.16s:%.16s\n", wlog("dropped a message of type %.16s:%.16s\n",
msg->application, msg->type); msg->application, msg->type);
fxlink_message_free(msg, true);
fxlink_device_start_bulk_IN(fdev); fxlink_device_start_bulk_IN(fdev);
continue; continue;
} }
if(opts.bridge_only) if(opts.bridge_only || log_fp) {
printf("CAL> %.*s\n", msg->size, (char *)msg->data); fprintf(log_fp ? log_fp : stdout,
"CAL> %.*s\n", msg->size, (char *)msg->data);
}
ssize_t send_ret = send(client_socket, msg->data, msg->size, 0); ssize_t send_ret = send(client_socket, msg->data, msg->size, 0);
if(send_ret != msg->size) { if(send_ret != msg->size) {
perror("send"); perror("send");
@ -304,8 +333,10 @@ int main(int argc, char **argv)
perror("recv"); perror("recv");
goto end; goto end;
} }
if(opts.bridge_only) if(opts.bridge_only || log_fp) {
printf("GDB> %.*s\n", (int)recv_ret, buf); fprintf(log_fp ? log_fp : stdout,
"GDB> %.*s\n", (int)recv_ret, buf);
}
if(!fxlink_device_start_bulk_OUT(fdev, "gdb", "remote", buf, recv_ret, false)) { if(!fxlink_device_start_bulk_OUT(fdev, "gdb", "remote", buf, recv_ret, false)) {
elog("unable to start bulk OUT transfer\n"); elog("unable to start bulk OUT transfer\n");
goto end; goto end;
@ -318,6 +349,8 @@ int main(int argc, char **argv)
end: end:
if(gdb_pid != -1) if(gdb_pid != -1)
waitpid(gdb_pid, NULL, 0); waitpid(gdb_pid, NULL, 0);
if(log_fp)
fclose(log_fp);
if(socket_path[0]) if(socket_path[0])
unlink(socket_path); unlink(socket_path);
if(device_list.ctx) if(device_list.ctx)