Skip to content

Commit 19a5ff1

Browse files
committed
ds2: add support for serial port connections
This adds in a new connection type: character device. This allows use of DS2 with a serial port much as is possible with gdbserver.
1 parent 2af8459 commit 19a5ff1

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ endif ()
3434
if ("${OS_NAME}" MATCHES "Linux" OR "${OS_NAME}" MATCHES "Android")
3535
include(CheckIncludeFile)
3636
CHECK_INCLUDE_FILE(sys/personality.h HAVE_SYS_PERSONALITY_H)
37+
CHECK_INCLUDE_FILE(termios.h HAVE_TERMIOS_H)
3738

3839
include(CheckSymbolExists)
3940
set(CMAKE_REQUIRED_DEFINITIONS -D_XOPEN_SOURCE=600;-D_GNU_SOURCE)
@@ -355,7 +356,7 @@ if ("${OS_NAME}" MATCHES "Windows")
355356
endif ()
356357

357358
if ("${OS_NAME}" MATCHES "Linux" OR "${OS_NAME}" MATCHES "Android")
358-
foreach (CHECK SYS_PERSONALITY_H GETTID POSIX_OPENPT TGKILL
359+
foreach (CHECK SYS_PERSONALITY_H TERMIOS_H GETTID POSIX_OPENPT TGKILL
359360
PROCESS_VM_READV PROCESS_VM_WRITEV
360361
ENUM_PTRACE_REQUEST)
361362
if (HAVE_${CHECK})

Sources/main.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
#include <thread>
3434

3535
#include <fcntl.h>
36-
36+
#if HAVE_TERMIOS_H
37+
#include <termios.h>
38+
#endif
3739
#if !defined(OS_WIN32)
3840
#include <unistd.h>
3941
#endif
@@ -297,6 +299,7 @@ static int GdbserverMain(int argc, char **argv) {
297299

298300
enum class channel_type {
299301
file_descriptor,
302+
character_device,
300303
named_pipe,
301304
network,
302305
};
@@ -322,6 +325,10 @@ static int GdbserverMain(int argc, char **argv) {
322325
connection_type = channel_type::named_pipe;
323326

324327
const std::string &address = opts.getPositional("[host]:port");
328+
#if defined(OS_POSIX)
329+
if (!address.empty() && address.find_first_of(":") == std::string::npos)
330+
connection_type = channel_type::character_device;
331+
#endif
325332

326333
switch (connection_type) {
327334
case channel_type::file_descriptor:
@@ -412,6 +419,35 @@ static int GdbserverMain(int argc, char **argv) {
412419
}
413420
break;
414421

422+
case channel_type::character_device:
423+
#if defined(OS_POSIX)
424+
if (std::filesystem::exists(address))
425+
if (std::filesystem::is_character_file(address) ||
426+
std::filesystem::is_fifo(address))
427+
fd = ::open(address.c_str(), O_RDWR);
428+
if (fd < 0) {
429+
DS2LOG(Error, "unable to open %s: %s", address.c_str(), strerror(errno));
430+
::exit(EXIT_FAILURE);
431+
}
432+
433+
#if HAVE_TERMIOS_H
434+
{
435+
struct termios termios;
436+
(void)tcgetattr(fd, &termios);
437+
termios.c_iflag = 0;
438+
termios.c_oflag = 0;
439+
termios.c_cflag = (termios.c_cflag & ~(CSIZE | PARENB)) | CLOCAL | CS8;
440+
termios.c_lflag = 0;
441+
termios.c_cc[VMIN] = 1;
442+
termios.c_cc[VTIME] = 0;
443+
(void)tcsetattr(fd, TCSANOW, &termios);
444+
}
445+
#endif
446+
447+
[[fallthrough]];
448+
#else
449+
DS2BUG("connecting with chardev is not supported on this platform");
450+
#endif
415451
case channel_type::file_descriptor:
416452
#if defined(OS_POSIX)
417453
socket = CreateFDSocket(fd);

0 commit comments

Comments
 (0)