【问题标题】:Detach a linux process from pseudo-tty, but keep the tty running?从伪 tty 中分离一个 linux 进程,但保持 tty 运行?
【发布时间】:2013-01-10 12:57:15
【问题描述】:

我想调试一个带有 2 个 xterm 窗口的控制台 linux 应用程序:一个窗口用于 gdb,另一个用于应用程序(例如 mc)。

我现在要做的是在第二个 xterm 窗口中运行 'tty && sleep 1024d'(这给了我它的伪 tty 名称),然后在 gdb 中运行 'tty' 以将程序重定向到另一个 xterm 窗口。但是,GDB 警告它不能设置控制终端,并且某些次要功能不起作用(例如处理窗口大小调整),因为“sleep 1024d”仍在该 xterm 窗口上运行。

有没有更好的方法(而不是从 shell 启动进程并从 gdb 附加到它)?

【问题讨论】:

    标签: linux debugging terminal gdb


    【解决方案1】:

    我对相关错误中给出的程序进行了一些修改,以将文件名存储在某处 http://sourceware.org/bugzilla/show_bug.cgi?id=11403

    这是一个使用它的例子:

    $ xterm -e './disowntty ~/tty.tmp' & sleep 1 && gdb --tty $(cat ~/tty.tmp) /usr/bin/links

    /* tty;exec disowntty  */
    #include <sys/ioctl.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <limits.h>
    #include <stdlib.h>
    #include <signal.h>
    static void
    end (const char *msg)
    {
      perror (msg);
      for (;;)
        pause ();
    }
    int
    main (int argc, const char *argv[])
    {
      FILE *tty_name_file;
      const char *tty_filename;
    
      if (argc <= 1)
        return 1;
      else
        tty_filename = argv[1];
    
      void (*orig) (int signo);
      setbuf (stdout, NULL);
      orig = signal (SIGHUP, SIG_IGN);
      if (orig != SIG_DFL)
        end ("signal (SIGHUP)");
      /* Verify we are the sole owner of the tty.  */
      if (ioctl (STDIN_FILENO, TIOCSCTTY, 0) != 0)
        end ("TIOCSCTTY");
      printf("%s %s\n", tty_filename, ttyname(STDIN_FILENO));
      tty_name_file = fopen(tty_filename, "w");
      fprintf(tty_name_file, "%s\n", ttyname(STDIN_FILENO));
      fclose(tty_name_file);
    
      /* Disown the tty.  */
      if (ioctl (STDIN_FILENO, TIOCNOTTY) != 0)
        end ("TIOCNOTTY");
      end ("OK, disowned");
    
      return 1;
    }
    

    【讨论】:

    • fwiw 重新实现在程序中写入 tty 名称的原因是,xterm -e 'tty | cat >tmp.tty && disowntty' 最终会调用 bash,这会阻止我们放弃 tty。
    • 谢谢,我试试。有没有机会通过命令行 stty 来完成它而不制作单独的程序?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多