C++程序崩溃终端 - 如何调试?

Jos*_*osh 3 c++ terminal serial-port arduino

不要问我怎么样 - 我没有丝毫.

以下代码崩溃了我的终端和我使用的任何运行时分析工具,同时没有引发任何静态检查工具警告.Valgrind,cppcheck和gdb对我没那么好.g ++ -Wall不会给我任何有用的东西.

代码的目标是通过USB串行连接将字符写入audrino.audrino地址作为第一个参数传递.传递unsigned int并将其强制转换为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;
}
Run Code Online (Sandbox Code Playgroud)

现在,在这看起来很健全的情况下,我将提交一份错误报告.

系统规格:最新的Arch linux 64位,用"g ++ -g -Wall code.c"编译

sth*_*sth 5

如果通过"崩溃我的终端"你的意思是终端停止工作,原因很可能是你在stdout(也就是你的终端)上将各种标志设置为零.你正在调用tcsetattr()并基本上将所有终端控制标志归零.

您应该获取当前终端标志tcgetattr(),修改您要修改的标志,然后使用该结构进行调用tcsetattr().不这样做甚至是未定义的行为:

tcsetattr()如果termios指向的结构的值termios_p不是从对tcgetattr()on 的调用结果派生的,那么效果是未定义的fildes; 应用程序应该仅修改此规范定义的字段和标志,tcgetattr()以及对tcsetattr()所有其他字段和标志保持不变的调用.

  • 同样地调用`fcntl(2)` - 你不应该只调用`fcntl(...,F_SETFL,<flags>)`,而是`fcntl(...,F_SETFL,<flags> | fcntl(. ..,F_GETFL))`保留以前的标志并添加你想要的标志. (2认同)