Skip to content

Instantly share code, notes, and snippets.

@yoku0825
Created September 26, 2022 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoku0825/2629031b69c06377f4dd5b5dc7552ef3 to your computer and use it in GitHub Desktop.
Save yoku0825/2629031b69c06377f4dd5b5dc7552ef3 to your computer and use it in GitHub Desktop.
mysqlコマンドラインクライアントって、自分の出力がパイプかどうかどうやって判定してるんだろ
$ strace -e write /usr/mysql/8.0.30/bin/mysql -S /usr/mysql/8.0.30/data/mysql.sock -P 64080 --prompt="mysql80 \C> " -e "SELECT 1"
write(1, "+---+\n", 6+---+
) = 6
write(1, "| 1 |\n", 6| 1 |
) = 6
write(1, "+---+\n", 6+---+
) = 6
write(1, "| 1 |\n", 6| 1 |
) = 6
write(1, "+---+\n", 6+---+
) = 6
+++ exited with 0 +++
$ strace -e write /usr/mysql/8.0.30/bin/mysql -S /usr/mysql/8.0.30/data/mysql.sock -P 64080 --prompt="mysql80 \C> " -e "SELECT 1" | cat
write(1, "1\n1\n", 41
1
) = 4
+++ exited with 0 +++
@yoku0825
Copy link
Author

$ strace -e write /usr/mysql/8.0.30/bin/mysql -S /usr/mysql/8.0.30/data/mysql.sock -P 64080 --prompt="mysql80 \C> " -e "SELECT 1" --table | cat
write(1, "+---+\n| 1 |\n+---+\n| 1 |\n+---+\n", 30+---+
| 1 |
+---+
| 1 |
+---+
) = 30
+++ exited with 0 +++

@yoku0825
Copy link
Author

パイプあり

(gdb) b print_tab_data
+b print_tab_data
Breakpoint 1 at 0x413b64: file /home/yoku0825/mysql-8.0.30/client/mysql.cc, line 3934.
(gdb) r
+r
Starting program: /usr/mysql/8.0.30/bin/mysql -S /usr/mysql/8.0.30/data/mysql.sock -P 64080 --prompt=mysql80\ \\C\>\  -e SELECT\ 1
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Breakpoint 1, com_go () at /home/yoku0825/mysql-8.0.30/client/mysql.cc:3373
3373              print_tab_data(result);
Missing separate debuginfos, use: debuginfo-install keyutils-libs-1.5.8-3.el7.x86_64 libselinux-2.5-15.el7.x86_64 ncurses-libs-5.9-14.20130511.el7_4.x86_64
(gdb) bt
+bt
#0  com_go () at /home/yoku0825/mysql-8.0.30/client/mysql.cc:3373
#1  0x0000000000415a6b in read_and_execute(bool) () at /home/yoku0825/mysql-8.0.30/client/mysql.cc:2317
#2  0x000000000040ae94 in main () at /home/yoku0825/mysql-8.0.30/client/mysql.cc:1446
#3  0x00007ffff5ea7555 in __libc_start_main (main=0x40a640 <main>, argc=8, argv=0x7fffffffe238, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe228) at ../csu/libc-start.c:266
#4  0x000000000040b8b0 in _start ()

パイプなし

(gdb) b print_tab_data
+b print_tab_data
Breakpoint 1 at 0x413b64: file /home/yoku0825/mysql-8.0.30/client/mysql.cc, line 3934.
(gdb) r
+r
Starting program: /usr/mysql/8.0.30/bin/mysql -S /usr/mysql/8.0.30/data/mysql.sock -P 64080 --prompt=mysql80\ \\C\>\  -e SELECT\ 1
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
+---+
| 1 |
+---+
| 1 |
+---+
[Inferior 1 (process 28252) exited normally]
Missing separate debuginfos, use: debuginfo-install keyutils-libs-1.5.8-3.el7.x86_64 libselinux-2.5-15.el7.x86_64 ncurses-libs-5.9-14.20130511.el7_4.x86_64

@yoku0825
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment