Hello Rust users!
I've been migrating from C++ to rust starting last year and it is been a challenging but fun experience.
Now I've come across a problem that I couldn't find many documentation / examples to help me solve it. I'm writing a simple gdb GUI front-end for debugging firmware for ARM microcontrollers, my program talks with gdb through the 'mi' protocol. This protocol uses a mixture of synchronous and asynchronous messages, so I need a dedicated thread to to read from the gdb stdout (blocking), parse the messages and store the data into a queue.
The problem I'm facing is that I need to write to the gdb stdin from the main thread (send commands to gdb), but once I move the stdout pipe to the reader thread the whole Command::new() object is moved and it is no longer accessible from the main thread (as expected). I couldn't figure out a nice way to solve it.
Here is my code:
use std::process::{Command, Stdio};
use std::io::{BufRead, Write, BufReader};
use std::io::prelude::*;
use std::ffi::OsStr;
use std::thread;
pub fn start_gdb<I, S>(gdbexec: &str, args: I)
where I: IntoIterator<Item=S>, S: AsRef<OsStr>
{
let mut child = Command::new(gdbexec)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.args(args)
.spawn()
.expect("Failed to start gdb");
thread::spawn(move || {
let mut child_out = BufReader::new(child.stdout.as_mut().unwrap());
let mut val = String::new();
println!("New thread!");
loop {
child_out.read_line(&mut val).unwrap();
println!("GDB out: {}", val);
val.clear();
}
println!("Dead thread!");
});
// How to write to child stdin from here?
}
Have a nice day!