【问题标题】:LLDB ioctl problemsLLDB ioctl 问题
【发布时间】:2017-01-08 16:47:55
【问题描述】:

我有一个程序,我在其中使用 ioctl(0, TIOCGWINSZ, (struct winsize *)) 来查找程序正在运行的终端窗口的大小。当我在终端中运行它时,它工作正常,但是当我使用 LLDB 时,ioctl 给出窗口大小为 0 x 0。

例子:

#include <unistd.h>
#include <sys/ioctl.h>
#include <stdio.h>

int main(){
    struct winsize tty_window_size;

    ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_window_size);

    printf("Rows: %i, Cols: %i\n", tty_window_size.ws_row, tty_window_size.ws_col);

    return 0;
}

终端成绩单:

$ clang test.c
$ ./a.out
Rows: 24, Cols: 80
$ lldb ./a.out
(lldb) target create "./a.out"
Current executable set to './a.out' (x86_64).
(lldb) r
Process 32763 launched: './a.out' (x86_64)
Rows: 0, Cols: 0
Process 32763 exited with status = 0 (0x00000000)

有人知道为什么会发生这种情况,或者有解决方法吗?

提前致谢。

【问题讨论】:

    标签: c lldb ioctl


    【解决方案1】:

    lldb 使用 pty 来处理程序输入和输出,但似乎是一个错误,即它们没有设置为跟踪 lldb 的终端大小。请使用 lldb.llvm.org 错误跟踪器归档。

    如果你在 OS X 上,你可以在一个单独的终端窗口中运行你的应用程序(如果你对终端做任何花哨的事情,这可能是你想要的),方法是:

    (lldb) 进程启动-tty

    我不知道这是否已经在 Linux 上实现了。

    【讨论】:

      【解决方案2】:

      不确定它是否有用,因为它是一个旧帖子。无论如何...我遇到了同样的问题并找到了解决方法。如果 stdout 上的 ioctl 失败,请尝试使用 /dev/tty

      #include <stdio.h>
      #include <fcntl.h>
      #include <unistd.h>
      #include <sys/ioctl.h>
      
      void getTerminalSize(int *row, int *col) {
        struct winsize ws;
      
        *row = *col = 0;      /* default value (indicates an error) */
        if (!isatty(STDOUT_FILENO)) {
          return;
        }
      
        ws.ws_row = ws.ws_col = 0;
        if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_row == 0 || ws.ws_col == 0) {
          int fd = open("/dev/tty", O_RDONLY);
          if (fd != -1) {
            ioctl(fd, TIOCGWINSZ, &ws);
            close (fd);
          }
        }
        *row = ws.ws_row;
        *col = ws.ws_col;
      }  
      
      int main(){
      
        int row, col;
        getTerminalSize(&row, &col);
        printf("Row: %i, Col: %i\n", row, col);
      
        return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-23
        • 2010-10-28
        • 2020-02-05
        • 2018-06-23
        • 2017-03-03
        • 2022-08-12
        • 2021-11-23
        • 1970-01-01
        • 2012-08-16
        相关资源
        最近更新 更多