GDB 可视化 TUI(Text User Interface) 模块简介

GDB 可视化 TUI(Text User Interface) 模块简介

[TOC]
GDB 可视化 TUI(Text User Interface) 模块简介

TUI 简介

一个基于 curses 库的终端 interface, 可以分别显示: 源码, 汇编代码, 程序寄存器信息, GDB 命令行.

虽然有很多开源的基于 GDB 的 GUI 工具要比 TUI 更加强大友好(TUI 的显示与交互会时时刻刻提醒我们, 它是 text 不是 graphics), 但是作为 GDB 内置的工具, 不需要进行配置就可以使用, 还是有必要掌握一下的.

本文整理自 GNU 官网手册.

启用

GDB 启动时调用

  • gdb -tui

GDB 启动后调用

  • 快捷键: - 进入后 l (小写的 L)显示源码.

TUI Overview

显示 4 类信息

  • GDB 命令行: command(cmd)

    • gdb() 为提示符的窗口, 没进入 TUI 前仅存的窗口.
  • 源码文件: source(src)

    • 显示源码文件, 会标注当前行以及断点信息. 会随着调试的进行自动更新源码文件.
  • 汇编语言: assembly(asm)

    • 显示反汇编产生的汇编代码.
  • 寄存器状态: register(reg)

    • 显示处理器的寄存器状态, 当寄存器值变化时, 寄存器会被高亮.
  • 在 standard layouts 中并不是所有 windows 可以同时显示.

    • cmd 一直会显示.

    • 其他的有如下组合:

      • source only,
      • assembly only,
      • source and assembly,
      • source and registers,
      • assembly and registers.

当前运行到的行被高亮且前面有一个 > 标记.

> 前面第一个标记的含义

  • B: 至少被触发过 1 次的断点.
  • b: 还没有被触发过的断点.
  • H: 至少被触发过 1 次的硬件断点.
  • h: 还没有被触发过的硬件断点.

> 前面第二个标记的含义

  • +: 被启用的断点.
  • -: 未被启用的断点.

command window 最上方的状态行显示的信息:

  • target: 显示当前 GDB 分析的目标.

  • process: 显示当前进程/线程号码, 如果当前没有 debug 的进程/线程, 此字段显示 No process.

  • 显示选中 frame 下当前运行的函数名称. 如果打开 demangling(悬空), 函数名字会显示为 demangled. 如果当前没有 debug 的函数, 则会显示 ??.

  • line: 显示选中 frame 当前行数, 如果没有在 debug 则显示为 ??.

  • pc: 显示当前程序的program counter address.

  • 例子:

active 与 inactive 窗口

  • 多个窗口显示时, 一些操作需要指定具体的窗口, 例如滚轮, 箭头键等.
  • 指定这些操作有效的窗口称之为 active 窗口, 其他的窗口为 inactive 窗口.

TUI 快捷键

大写字母快捷键的情况下可能需要同时按 Shift.

TUI mode 与 the GDB standard mode 都有效的快捷键

  • 进/退 TUI 模式

    • Ctrl+xCtrl+a
    • Ctrl+xa
    • Ctrl+xA
  • 窗口切换

    • Ctrl+x1

      • 除 cmd 外, 仅添加一个窗口 src 或者 asm.
      • 当 TUI mode 未被激活时, 在标准 mode 下使用此快捷键会自动跳到 TUI mode.
      • Think of this key binding as the Emacs Ctrl+x1 binding.
    • Ctrl+x2

      • 打开 2 个 window, 连续按可以轮换其他 2 个 window 的组合, 轮换的顺序类似与 queue.

      • 例如 2 次执行此快捷键的效果

      • 上方的 2 个非 cmd window 的 window 会随着其中一个 window 的滑动会同步地滑动.

    • Ctrl+xo

      • 切换 active window.
      • 除了 command window 以外的 window 的外围会被加粗框起来.
  • 命令切换

    • Ctrl+xs

      • 切换进/出 TUI SingleKey mode(可以用一个键来调用 GDB 命令).

只在 TUI mode 下有效的快捷键:

  • PgUp: Scroll the active window one page up.
  • PgDn: Scroll the active window one page down.
  • : Scroll the active window one line up.
  • : Scroll the active window one line down.
  • : Scroll the active window one column left.
  • : Scroll the active window one column right.
  • Ctrl+l : Refresh the screen. 很多时候执行几次复杂的调试命令后, 所有窗口会变得非常凌乱(如下图), 可以使用这个快捷键刷新排布.

操作 cmd window

  • cmd window 被选定为 active: 箭头键.

  • cmd window inactive

    • Ctrl+p: previous 上一条.
    • Ctrl+n: next 下一条.
    • Ctrl+b: back 命令行光标前移.
    • Ctrl+f: forward 命令行光标后移.

TUI Single Key Mode

此模式下可以用单个键实现 GDB 命令.

此模式下, GDB command 模式下每行最开始的 (gdb) 的提示符会消失.

Ctrl+xs 进入此模式, 可以使用如下快捷键下达 GDB command:

  • c: continue
  • d: down
  • f: finish
  • n: next
  • o: nexti. The shortcut letter ‘o’ stands for “step Over”.
  • q: exit the SingleKey mode.
  • r: run
  • s: step
  • i: stepi. The shortcut letter ‘i’ stands for “step Into”.
  • u: up
  • v: info locals
  • w: where

按下其他键会暂时离开此模式, 但是还会自动跳回此模式. 想要永久退出此模式必须按键: q 或者 Ctrl+xs.

此模式下的快捷键映射可以自定义: If GDB was built with Readline 8.0 or later, the TUI SingleKey keymap will be named SingleKey. This can be used in .inputrc to add additional bindings to this keymap.

TUI Mouse Support

只要 curses 库支持鼠标操作, TUI 就支持鼠标操作.

两种应用场景

  • 滚轮

    • The mouse wheel scrolls the appropriate window under the mouse cursor.
  • 使用鼠标进行快捷复制粘贴

    • The TUI itself does not directly support copying/pasting with the mouse. However, on Unix terminals, you can typically press and hold the SHIFT key on your keyboard to temporarily bypass GDB’s TUI and access the terminal’s native mouse copy/paste functionality (commonly, click-drag-release or double-click to select text, middle-click to paste). This copy/paste works with the terminal’s selection buffer, as opposed to the TUI’s buffer.

TUI-specific Commands

TUI 有自己的一套操作 window 的命令. 这些命令大部分即便当前在 GDB 标准模式, 也可以使用, 并通过它自动跳转到 TUI 模式.

tui enable

  • 启动 TUI mode 默认启动上次
    在 TUI 模式下的 active layout(此概念在下面讲解), 如果没有上次启动 TUI 模式的记录会采用 default layout.

tui disable

  • 关闭 TUI mode.

info win

  • 列出当前所有显示 windows 的名字以及大小.

help tui

  • 帮助命令

tui new-layout name window weight [window weight…]
创建新的 layout

  • name: layout 的名字. 一旦创建了, 可以使用 layout 指令访问.
  • window: 窗口的名称(与 focus 命令的 window name 相同), 按照输入的 list 的顺序从上往下依次显示所有的 windows.
  • weight: 整数, 为某个特定 window 占整个 layout 的比例. 使用固定高度: 0.
  • [-horizontal]window weight [window weight…]: 默认是从上到下排布, 加上 -horizontal 可以从左到右排布.
  • 注意此命令只在较新的版本可用(具体哪个版本没查到 -_-), 9.2 版本不可用.
  • 例子
1
(gdb) tui new-layout example {-horizontal src 1 asm 1} 2 status 0 cmd 1

创建一个名为 example 的 layout. source 与 assembly 窗口水平排布, status 与 command 窗口在下方. source 与 assembly 窗口是 command 窗口的高度的 2 倍.

layout

  • 语法格式
1
layout name
  • name 既可以是系统内建默认的名字, 也可以是自定义的名字.

  • 内建 layouts:

    • next: Display the next layout.
    • prev: Display the previous layout.
    • src: Display the source and command windows.
    • asm: Display the assembly and command windows.
    • split: Display the source, assembly, and command windows.
    • regs: When in src layout display the register, source, and command windows. When in asm or split layout display the register, assembler, and command windows.

focus 命令

  • 语法格式
1
focus name
  • 功能

    • 切换 active window, 用来 滚轮 scrolling. name 只能为下面之一:
    • next: Make the next window active for scrolling.
    • prev: Make the previous window active for scrolling.
    • src: Make the source window active for scrolling.
    • asm: Make the assembly window active for scrolling.
    • regs: Make the register window active for scrolling.
    • cmd: Make the command window active for scrolling.

refresh 命令

  • Refresh the screen. 等同于快捷键 Ctrl + l.

update 命令

  • 更新 src 以及当前执行到的地方. 对象仅限 src 窗口, 这与 refresh 不同.

改变 window 的高度

  • winheight name +count
  • winheight name -count
  • 可以使用 info win 查看当下可见的 window, 然后选取其中之一作为参数 name.

TUI Configuration Variables

描绘 window 的边框形状

  • set tui border-kind kind, 可选参数如下:

  • space: Use a space character to draw the border.

  • ascii: Use ASCII characters +, - and | to draw the border.

  • acs: Use the Alternate Character Set to draw the border. The border is drawn using character line graphics if the terminal supports them.

描绘 active 或者 inactive window 的边框形状

  • inactive 窗口的边框形状: set tui border-mode mode.
  • active 窗口的边框形状: set tui active-border-mode mode.
  • normal: Use normal attributes to display the border.
  • standout: Use standout mode.
  • reverse: Use reverse video mode.
  • half: Use half bright mode.
  • half-standout: Use half bright and standout mode.
  • bold: Use extra bright or bold mode.
  • bold-standout: Use extra bright or bold and standout mode.

设置 tab 键的长度(src asm 窗口中)

  • set tui tab-width nchars

设置 src 窗口是否紧凑的风格

  • set tui compact-source [on|off]
  • 紧凑:
  • 宽松:

GDB 可视化 TUI(Text User Interface) 模块简介

https://www.chuxin911.com/GDB_note_2_tui_20220708/

作者

cx

发布于

2022-07-08

更新于

2022-07-16

许可协议