C++ program crashes terminal--How do I debug this?
arduino, c++, serial-port, terminal
Solution
If by "crashed my terminal" you mean that the terminal stops working, than the reason is most probably that you are setting all kinds of flags to zero on your stdout (aka. your terminal). You are calling `tcsetattr()` and zero out basically all terminal control flags there are.
You should get the current terminal flags with `tcgetattr()`, modifiy the ones you want to modify, and then use that struct for the call to `tcsetattr()`. Not doing so is even undefined behavior:
The effect of `tcsetattr()` is undefined if the value of the `termios` structure pointed to by `termios_p` was not derived from the result of a call to `tcgetattr()` on `fildes`; an application should modify only fields and flags defined by this specification between the call to `tcgetattr()` and `tcsetattr()`, leaving all other fields and flags unmodified.
Problem
Don't ask me how--I haven't the slightest. The following code crashes my terminal and any runtime analysis tool I use, while not raising any static checking tool warnings. Valgrind, cppcheck, and gdb have done me little good. g++ -Wall does not give me anything useful. The goal of the code is to write a character to a audrino through a USB serial connection. The audrino address is passed as the first argument. A unsigned int is passed and is cast to a unsigned char. ``` #include <string.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> #include <iostream> using namespace std; int main(int argc,char** argv){ struct termios stdio; int tty_fd; unsigned char ctrlChar; unsigned int ctrlInt; tty_fd=open(argv[1], O_RDWR | O_NONBLOCK); tcgetattr(tty_fd, &stdio); cfmakeraw(&stdio); stdio.c_cc[VMIN] = 1; stdio.c_cc[VTIME] = 0; tcsetattr(tty_fd,TCSANOW, &stdio); tcsetattr(tty_fd,TCSAFLUSH, &stdio); fcntl(tty_fd, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK); cfsetospeed(&stdio,B9600); // 9600 baud cfsetispeed(&stdio,B9600); // 9600 baud cout << "ready on " << argv[1] << endl; scanf("%u", ctrlInt); cout <<"recieved initial read.\n"; while (ctrlInt < 256){ ctrlChar = ((char) ctrlInt); cout << ctrlInt << ctrlChar << endl; write(tty_fd,&ctrlChar,1); cout << "sent to audrino.\n"; scanf("%u", ctrlInt); } cout << "done!" << endl; close(tty_fd); return 0; } ``` Now, in the offchance that this seems sane, I'll file a bug report. Sys specs: up to date Arch linux 64 bit, compiled with "g++ -g -Wall code.c"