Skip to content

Commit 505430f

Browse files
jm-observervadimcn
authored andcommitted
Support stdio stream mode
1 parent 199184a commit 505430f

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

adapter/codelldb/src/lib.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod must_initialize;
3131
mod platform;
3232
mod python;
3333
mod shared;
34+
mod stdio_stream;
3435
mod terminal;
3536

3637
pub fn debug_server(matches: &ArgMatches) -> Result<(), Error> {
@@ -63,46 +64,52 @@ pub fn debug_server(matches: &ArgMatches) -> Result<(), Error> {
6364
debugger.command_interpreter().handle_command(&command, &mut command_result, false);
6465
}
6566

66-
let (port, connect) = if let Some(port) = matches.value_of("connect") {
67-
(port.parse()?, true)
67+
let (use_stdio, port, connect) = if let Some(port) = matches.value_of("connect") {
68+
(false, port.parse()?, true)
6869
} else if let Some(port) = matches.value_of("port") {
69-
(port.parse()?, false)
70+
(false, port.parse()?, false)
7071
} else {
71-
return Err("Either --connect or --port must be specified".into());
72+
(true, 0, false)
7273
};
7374
let multi_session = matches.is_present("multi-session");
7475
let auth_token = matches.value_of("auth-token");
7576

76-
let localhost = net::Ipv4Addr::new(127, 0, 0, 1);
77-
let addr = net::SocketAddr::new(localhost.into(), port);
78-
7977
let rt = tokio::runtime::Builder::new_multi_thread() //
8078
.worker_threads(2)
8179
.enable_all()
8280
.build()
8381
.unwrap();
8482

8583
rt.block_on(async {
86-
if connect {
87-
debug!("Connecting to {}", addr);
88-
let mut tcp_stream = TcpStream::connect(addr).await?;
89-
tcp_stream.set_nodelay(true).unwrap();
90-
if let Some(auth_token) = auth_token {
91-
let auth_header = format!("Auth-Token: {}\r\n", auth_token);
92-
tcp_stream.write_all(&auth_header.as_bytes()).await?;
93-
}
94-
let framed_stream = dap_codec::DAPCodec::new().framed(tcp_stream);
84+
if use_stdio {
85+
debug!("Starting on stdio");
86+
let stream = stdio_stream::StdioStream::new();
87+
let framed_stream = dap_codec::DAPCodec::new().framed(stream);
9588
run_debug_session(Box::new(framed_stream), adapter_settings.clone()).await;
9689
} else {
97-
let listener = TcpListener::bind(&addr).await?;
98-
while {
99-
debug!("Listening on {}", listener.local_addr()?);
100-
let (tcp_stream, _) = listener.accept().await?;
90+
let localhost = net::Ipv4Addr::new(127, 0, 0, 1);
91+
let addr = net::SocketAddr::new(localhost.into(), port);
92+
if connect {
93+
debug!("Connecting to {}", addr);
94+
let mut tcp_stream = TcpStream::connect(addr).await?;
10195
tcp_stream.set_nodelay(true).unwrap();
96+
if let Some(auth_token) = auth_token {
97+
let auth_header = format!("Auth-Token: {}\r\n", auth_token);
98+
tcp_stream.write_all(&auth_header.as_bytes()).await?;
99+
}
102100
let framed_stream = dap_codec::DAPCodec::new().framed(tcp_stream);
103101
run_debug_session(Box::new(framed_stream), adapter_settings.clone()).await;
104-
multi_session
105-
} {}
102+
} else {
103+
let listener = TcpListener::bind(&addr).await?;
104+
while {
105+
debug!("Listening on {}", listener.local_addr()?);
106+
let (tcp_stream, _) = listener.accept().await?;
107+
tcp_stream.set_nodelay(true).unwrap();
108+
let framed_stream = dap_codec::DAPCodec::new().framed(tcp_stream);
109+
run_debug_session(Box::new(framed_stream), adapter_settings.clone()).await;
110+
multi_session
111+
} {}
112+
}
106113
}
107114
Ok::<(), Error>(())
108115
})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::pin::Pin;
2+
use std::task::{Context, Poll};
3+
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
4+
5+
pub struct StdioStream {
6+
std_in: tokio::io::Stdin,
7+
std_out: tokio::io::Stdout,
8+
}
9+
10+
impl AsyncRead for StdioStream {
11+
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
12+
Pin::new(&mut self.get_mut().std_in).poll_read(cx, buf)
13+
}
14+
}
15+
16+
impl AsyncWrite for StdioStream {
17+
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, std::io::Error>> {
18+
Pin::new(&mut self.get_mut().std_out).poll_write(cx, buf)
19+
}
20+
21+
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), std::io::Error>> {
22+
Pin::new(&mut self.get_mut().std_out).poll_flush(cx)
23+
}
24+
25+
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), std::io::Error>> {
26+
Pin::new(&mut self.get_mut().std_out).poll_shutdown(cx)
27+
}
28+
}
29+
30+
impl StdioStream {
31+
pub fn new() -> Self {
32+
StdioStream {
33+
std_in: tokio::io::stdin(),
34+
std_out: tokio::io::stdout(),
35+
}
36+
}
37+
}

adapter/codelldb/src/terminal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ impl Terminal {
9898
pub fn attach_console(&self) {
9999
unsafe {
100100
let pid = self.data.parse::<u32>().unwrap();
101-
dbg!(winapi::um::wincon::FreeConsole());
102-
dbg!(winapi::um::wincon::AttachConsole(pid));
101+
winapi::um::wincon::FreeConsole();
102+
winapi::um::wincon::AttachConsole(pid);
103103
}
104104
}
105105

0 commit comments

Comments
 (0)