Debian Codegraph @

testsuite/systemtap.printf/stap_merge.tcl

raw
1 #!/usr/bin/env tclsh
2 #
3 # stap_merge.tcl - systemtap merge program
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Copyright (C) Red Hat Inc, 2007-2023
19 #
20 #
21
22 proc usage {} {
23 puts stderr "$::argv0 \[-v\] \[-o output_filename\] input_files ...\n"
24 exit 1
25 }
26
27 set outfile "stdout"
28 set verbose 0
29 set index 0
30 while {[string match -* [lindex $argv $index]]} {
31 switch -glob -- [lindex $argv $index] {
32 -v {set verbose 1}
33 -o {incr index; set outfile [lindex $argv $index]}
34 default {usage}
35 }
36 incr index
37 }
38
39 if {$tcl_platform(byteOrder) == "littleEndian"} {
40 set int_format i
41 } else {
42 set int_format I
43 }
44
45 set files [lrange $argv $index end]
46
47 set n 0
48 foreach file $files {
49 if {[catch {open $file} fd($n)]} {
50 puts stderr $fd($n)
51 exit 1
52 }
53 fconfigure $fd($n) -translation binary
54 # read & ignore magic
55 read $fd($n) 4
56 if {![binary scan [read $fd($n) 4] $int_format timestamp($n)]} {
57 continue
58 }
59 set timestamp($n) [expr $timestamp($n) & 0xFFFFFFFF]
60 incr n
61 }
62 set ncpus $n
63
64 if {$outfile != "stdout"} {
65 if {[catch {open $outfile w} outfile]} {
66 puts stderr $outfile
67 exit 1
68 }
69 }
70 fconfigure $outfile -translation binary
71
72 while {1} {
73 set mincpu -1
74 for {set n 0} {$n < $ncpus} {incr n} {
75 if {[info exists fd($n)] && (![info exists min] || $timestamp($n) <= $min)} {
76 set min $timestamp($n)
77 set mincpu $n
78 }
79 }
80
81 if {![info exists min]} {break}
82
83 if {![binary scan [read $fd($mincpu) 4] $int_format len]} {
84 puts stderr "Error reading length from channel $mincpu"
85 exit 1
86 }
87
88 if {$verbose == 1} {
89 puts stderr "\[CPU:$mincpu, seq=$min, length=$len\]"
90 }
91
92 set data [read $fd($mincpu) $len]
93 puts -nonewline $outfile $data
94
95 # read & ignore magic
96 read $fd($mincpu) 4
97
98 set data [read $fd($mincpu) 4]
99 if {$data == ""} {
100 unset fd($mincpu)
101 } else {
102 binary scan $data $int_format timestamp($mincpu)
103 set timestamp($mincpu) [expr $timestamp($mincpu) & 0xFFFFFFFF]
104 }
105 unset min
106 }