如何将shell中的strace解析为纯文本?

7

我有一个由strace命令生成的跟踪日志,就像在运行PHP时一样:

sudo strace -e sendto -fp $(pgrep -n php) -o strace.log

输出结果如下:

11208 sendto(4, "set 29170397297_-cache-schema 85 0 127240\r\n\257\202\v\0?\0\0\0\2\27\10stdClass\24\7\21\3cid\21\6schema\21\4d\37ata\25\n\247\21\5block\24\6\21\6fields\24\f\21\3bid\24\2\5\21\4type 0\37erial\21\10not null\5\21\6module\24\4\16\7\21\7va\37rchar\21\6length\6@\16\t\5\21\7default\r\21\5de\2lta@\5\16\v\16\f\6 \35\7\16\r\21\0010\21\5t \207C\30@6\2\16\r\r n\4tatus@0\4\21\3int/\7\6\0\21\4size \222\finy\21\6weight\24\3 ;\0\22\300 \6\6region@8\340\5P\5custom\27\300,\17\16\23\16\24\21\nvisibility\340\t\34\7\5pages\24\2 \205\3\4tex@\206 \261\1it \365\0\5\240\0\377y\10\r\21\ftransl!N\2ble %\1ca!a\340\3Q\0\1n\31\vprimary key\24\1\6\0\16\6\21\vunique@\21\ts\24\1\21\3tmd\24\3 \31\0\20 2\v\n\6\2\16\16\21\7index \210\10\1\21\4list\24\5\240\36\0\21 \36\10\26\6\3\16\25\6\4\16\n \1\6\4\21\4name \7\0\na\317\2_ro\252\0\5!$\0\n \3\341\2\23\0\16\340\0\16A\214\2\21\3r!\354@ \v\22\21\10unsigned\5@\332\0\36\213\0\n \213\0\16 l\6%\16!\24\1\16%\271\0%@p\5\16#\16$\21\f\200l\241b@n\2\4\16\6M\2\10\16&@E\4\21\4bod\201_\5\32\16\t\4\16\23B\\\2g\16\34 \30\3info .\0\7a\255\0\200@q!L\5\6forma\201\332B/!d\2\4\16\37 y\0*y\0 \225a;\240\201\2'\21\van\0_\207\200\2\5\16\1\340\0U =@U\1\16\3@\222 \212\2lob@O\n\23\16)\21\6expire@\30\342\0\26\7\21\7create\241\17< \25\0\n\203\1\"\177\0dY\0\22 \305\5\5small\240!a\32\0.\230\0.\240\240\0\1\240\240\3,\21\vb S\2kpo\"\313\2s\24\6!\220\2\t\21\2\241q\0\10 ?\4\21\tno \213\6ort\5\21\fm\";\3ine_A\313\232\241\3\2\5\16#\340\4\16!\345\340\0U\223\340\0'AC\4sourc\202\202\340\3\27\0\v\200\27\0_C\326\340\0074\

看起来这些被普通的C转义代码所代表。

我尝试着用printf在shell中解码它们,例如:

while read line; do printf "%s" "$line"; done < <(cat strace.log | head -n2)

但是它失败了(看起来没有任何意义):

11208 sendto(4, "set 29170397297_-cache-schema 85 0 127240rn257202v0?00022710stdClass247213cid216schema214d37ata25n247215block246216fields24f213bid2425214type 037erial2110not null5216module244167217va37rchar216length6@16t5217defaultr215de2lta@516v16f6 35716r210010215t 207C30@6216rr n4tatus@04213int/760214size 222finy216weight243 ;022300 66region@83405P5custom27300,171623162421nvisibility340t3475pages242 20534tex@206 2611it 365052400377y10r21ftransl!N2ble %1ca!a3403Q01n31vprimary key2416016621vunique@21ts241213tmd243 31020 2vn621616217index 210101214list24524036021 3610266316256416n 164214name 70na3172_ro25205!$0n 3341223016340016A2142213r!354@ v222110unsigned5@3320362130n 213016 l6%16!24116%2710%@p516#16$21f200l241b@n24166M21016&@E4214bod201_53216t41623B\2g1634 303info .07a2550200@q!L56forma201332B/!d241637 y0*y0 225a;2402012'21van0_207200251613400U =@U1163@222 2122lob@On2316)216expire@303420267217create24117< 250n2031"1770dY022 30555small240!a320.`2300.240240012402403,21vb S2kpo"3132s246!2202t212241q010...

是否有更好的方法来解析 strace 命令的输出,以查看传递给 recvfrom/sendto 的纯字符串?

理想情况下,可以打印可打印字符,包括换行符(\r\n),但截断 NULL 和其他不可打印字符?


strace无法跟踪这些数据的原因是它们是二进制数据,而不是纯文本。 - that other guy
对于类似于\0\0\0\2\27\10这样的东西,您希望输出什么? - that other guy
我想从转储及其上下文中提取一些有意义的文本。无论它是什么,但转义字符使其无法读取。这很可能是memcached之间的通信。 - kenorb
1个回答

15

read命令无法正常工作的原因是,shell已经转义了特殊字符,因此字符串被双重转义,导致\r\n被打印为rn

忽略shell对字符的转义,可以使用read -r,这将允许反斜杠转义任何字符(使它们成为字面量)。下面是一个示例:

while read -r line; do printf "%b\n" "$line"; done < strace.log | strings

由于它是二进制数据,上面的示例还包括strings命令,以仅显示可打印字符串。

当指定-x时,strace也支持将所有字符串打印为十六进制,但它仍将正常工作。


这是解析实时strace输出的版本:

while read -r line;
    do printf "%b\n" "$line" | strings
done < <(sudo strace -e recvfrom,sendto -s 1000 -fp $(pgrep -n php) 2>/dev/stdout)
此外,字符串可以通过使用更具体的过滤器grep进行替换,以仅获取双引号内的内容。
grep -o '".\+[^"]"' | grep -o '[^"]\+[^"]'

不过这仍然可能会打印二进制格式。

为了避免这种情况,让我们简化整个过程,定义以下格式化程序别名:

alias format-strace='grep --line-buffered -o '\''".\+[^"]"'\'' | grep --line-buffered -o '\''[^"]*[^"]'\'' | while read -r line; do printf "%b" $line; done | tr "\r\n" "\275\276" | tr -d "[:cntrl:]" | tr "\275\276" "\r\n"'

其中:

  • grep -o '".\+[^"]"' - 选择带引号的双引号字符串
  • grep -o '[^"]*[^"]' - 选择双引号中的文本
  • while read -r line - 将每一行存储到$line中,并执行某些操作(help read)
  • printf "%b" $line - 通过扩展反斜杠转义序列来打印行
  • tr "\r\n" "\275\276" - 将\r\n临时替换为\275\276
  • tr -d "[:cntrl:]" - 移除所有控制字符
  • tr "\275\276" "\r\n" - 恢复换行符

那么追踪某个命令(例如php)的完整示例可能如下:

strace -e trace=read,write,recvfrom,sendto -s 1000 -fp $(pgrep -n php) 2>&1 | format-strace

查找类似的示例:如何在另一个bash会话中查看正在运行的进程的输出? 在Unix.SE上



对我来说不起作用。例如,\0374printf解码为fc,而实际上它是1f34 - torvin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接