vieyahn2017 / shellv

shell command test and study
4 stars 1 forks source link

【3.23】od代替hexdump #82

Open vieyahn2017 opened 1 year ago

vieyahn2017 commented 1 year ago

hexdump -C od -t xCc

vieyahn2017 commented 1 year ago

/proc/3239 # cat stack | hexdump -C 00000000 5b 3c 30 3e 5d 20 68 72 74 69 6d 65 72 5f 6e 61 |[<0>] hrtimer_na| 00000010 6e 6f 73 6c 65 65 70 2b 30 78 64 30 2f 30 78 31 |nosleep+0xd0/0x1| 00000020 63 30 0a 5b 3c 30 3e 5d 20 5f 5f 78 36 34 5f 73 |c0.[<0>] __x64_s| 00000030 79 73 5f 6e 61 6e 6f 73 6c 65 65 70 2b 30 78 37 |ysnanosleep+0x7| 00000040 30 2f 30 78 61 30 0a 5b 3c 30 3e 5d 20 64 6f 5f |0/0xa0.[<0>] do| 00000050 73 79 73 63 61 6c 6c 5f 36 34 2b 30 78 35 62 2f |syscall_64+0x5b/| 00000060 30 78 31 62 30 0a 5b 3c 30 3e 5d 20 65 6e 74 72 |0x1b0.[<0>] entr| 00000070 79 5f 53 59 53 43 41 4c 4c 5f 36 34 5f 61 66 74 |y_SYSCALL_64_aft| 00000080 65 72 5f 68 77 66 72 61 6d 65 2b 30 78 36 35 2f |er_hwframe+0x65/| 00000090 30 78 63 61 0a 5b 3c 30 3e 5d 20 30 78 66 66 66 |0xca.[<0>] 0xfff| 000000a0 66 66 66 66 66 66 66 66 66 66 66 66 66 0a |fffffffffffff.| 000000ae

vieyahn2017 commented 1 year ago

/proc/3239 # cat stack | od -t xCc 0000000 5b 3c 30 3e 5d 20 68 72 74 69 6d 65 72 5f 6e 61 [ < 0 > ] h r t i m e r n a 0000020 6e 6f 73 6c 65 65 70 2b 30 78 64 30 2f 30 78 31 n o s l e e p + 0 x d 0 / 0 x 1 0000040 63 30 0a 5b 3c 30 3e 5d 20 5f 5f 78 36 34 5f 73 c 0 \n [ < 0 > ] x 6 4 s 0000060 79 73 5f 6e 61 6e 6f 73 6c 65 65 70 2b 30 78 37 y s n a n o s l e e p + 0 x 7 0000100 30 2f 30 78 61 30 0a 5b 3c 30 3e 5d 20 64 6f 5f 0 / 0 x a 0 \n [ < 0 > ] d o 0000120 73 79 73 63 61 6c 6c 5f 36 34 2b 30 78 35 62 2f s y s c a l l 6 4 + 0 x 5 b / 0000140 30 78 31 62 30 0a 5b 3c 30 3e 5d 20 65 6e 74 72 0 x 1 b 0 \n [ < 0 > ] e n t r 0000160 79 5f 53 59 53 43 41 4c 4c 5f 36 34 5f 61 66 74 y S Y S C A L L 6 4 a f t 0000200 65 72 5f 68 77 66 72 61 6d 65 2b 30 78 36 35 2f e r _ h w f r a m e + 0 x 6 5 / 0000220 30 78 63 61 0a 5b 3c 30 3e 5d 20 30 78 66 66 66 0 x c a \n [ < 0 > ] 0 x f f f 0000240 66 66 66 66 66 66 66 66 66 66 66 66 66 0a f f f f f f f f f f f f f \n 0000256

vieyahn2017 commented 1 year ago

终于实现了od代替hexdump linux-ZDyHXU:/proc/3239 # cat stack | od -t xCc | awk '{if(FNR%2 != 0) {printf "%s ",$0;getline;}; gsub(" ", "", $0);print(" |" $0 "|")}' 0000000 5b 3c 30 3e 5d 20 68 72 74 69 6d 65 72 5f 6e 61 |[<0>]hrtimer_na| 0000020 6e 6f 73 6c 65 65 70 2b 30 78 64 30 2f 30 78 31 |nosleep+0xd0/0x1| 0000040 63 30 0a 5b 3c 30 3e 5d 20 5f 5f 78 36 34 5f 73 |c0\n[<0>]__x64_s| 0000060 79 73 5f 6e 61 6e 6f 73 6c 65 65 70 2b 30 78 37 |ysnanosleep+0x7| 0000100 30 2f 30 78 61 30 0a 5b 3c 30 3e 5d 20 64 6f 5f |0/0xa0\n[<0>]do| 0000120 73 79 73 63 61 6c 6c 5f 36 34 2b 30 78 35 62 2f |syscall_64+0x5b/| 0000140 30 78 31 62 30 0a 5b 3c 30 3e 5d 20 65 6e 74 72 |0x1b0\n[<0>]entr| 0000160 79 5f 53 59 53 43 41 4c 4c 5f 36 34 5f 61 66 74 |y_SYSCALL_64_aft| 0000200 65 72 5f 68 77 66 72 61 6d 65 2b 30 78 36 35 2f |er_hwframe+0x65/| 0000220 30 78 63 61 0a 5b 3c 30 3e 5d 20 30 78 66 66 66 |0xca\n[<0>]0xfff| 0000240 66 66 66 66 66 66 66 66 66 66 66 66 66 0a |fffffffffffff\n| 0000256 |0000256|

vieyahn2017 commented 1 year ago

主要是od的双行,存在一个偶数行合并到奇数行的操作 cat stack | od -t xc | awk '{if(FNR%2 != 0) {printf "%s ",$0;getline;};print;}' cat stack | od -t xc | awk '/^000/{{printf "%s",$0;getline;};print $0;}'

vieyahn2017 commented 1 year ago

gsub返回的是匹配次数

cat stack | od -t xc | awk '{if(FNR%2 != 0) {printf "%s ",$0;getline;};print(gsub(" ", "", $0));}' 0000000 3e303c5b 7268205d 656d6974 616e5f72 56 0000020 6c736f6e 2b706565 30647830 3178302f 55 0000040 5b0a3063 5d3e303c 785f5f20 735f3436 55 0000060 6e5f7379 736f6e61 7065656c 3778302b 55 0000100 78302f30 5b0a3061 5d3e303c 5f6f6420 55 0000120 63737973 5f6c6c61 302b3436 2f623578 55 0000140 62317830 3c5b0a30 205d3e30 72746e65 55 0000160 59535f79 4c414353 34365f4c 7466615f 55 0000200 685f7265 61726677 302b656d 2f353678 55 0000220 61637830 303c5b0a 30205d3e 66666678 55 0000240 66666666 66666666 66666666 00000a66 48 0000256 0

vieyahn2017 commented 1 year ago

结论 hexdump -C od -t xCc | awk '{if(FNR%2 != 0) {printf "%s ",$0;getline;}; gsub(" ", "", $0);print(" |" $0 "|")}' od -t xCc | awk 'FNR%2!=0 {{printf "%s |",$0;getline;}; gsub(" ","",$0);print $0"|"}' 精简了好多

vieyahn2017 commented 1 year ago

不要FNR的判据居然也可以

linux-ZDyHXU:/proc/3239 # cat stack | od -t xCc | awk '{{printf "%s |",$0;getline;}; gsub(" ","",$0);print $0"|"}' 0000000 5b 3c 30 3e 5d 20 68 72 74 69 6d 65 72 5f 6e 61 |[<0>]hrtimer_na| 0000020 6e 6f 73 6c 65 65 70 2b 30 78 64 30 2f 30 78 31 |nosleep+0xd0/0x1| 0000040 63 30 0a 5b 3c 30 3e 5d 20 5f 5f 78 36 34 5f 73 |c0\n[<0>]__x64_s| 0000060 79 73 5f 6e 61 6e 6f 73 6c 65 65 70 2b 30 78 37 |ysnanosleep+0x7| 0000100 30 2f 30 78 61 30 0a 5b 3c 30 3e 5d 20 64 6f 5f |0/0xa0\n[<0>]do| 0000120 73 79 73 63 61 6c 6c 5f 36 34 2b 30 78 35 62 2f |syscall_64+0x5b/| 0000140 30 78 31 62 30 0a 5b 3c 30 3e 5d 20 65 6e 74 72 |0x1b0\n[<0>]entr| 0000160 79 5f 53 59 53 43 41 4c 4c 5f 36 34 5f 61 66 74 |y_SYSCALL_64_aft| 0000200 65 72 5f 68 77 66 72 61 6d 65 2b 30 78 36 35 2f |er_hwframe+0x65/| 0000220 30 78 63 61 0a 5b 3c 30 3e 5d 20 30 78 66 66 66 |0xca\n[<0>]0xfff| 0000240 66 66 66 66 66 66 66 66 66 66 66 66 66 0a |fffffffffffff\n| 0000256 |0000256|