blob: 1a4b0c1f06fc284b3c05105e52f2d9080d708c0d [file] [log] [blame]
Joe Tsaib4e370e2018-08-15 14:59:51 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// pbdump is a tool for decoding the wire format for protocol buffer messages.
6package main
7
8import (
9 "bytes"
10 "flag"
11 "fmt"
12 "io/ioutil"
13 "log"
14 "os"
15 "path/filepath"
16 "sort"
17 "strconv"
18 "strings"
19
Joe Tsai01ab2962018-09-21 17:44:00 -070020 "github.com/golang/protobuf/v2/internal/encoding/pack"
21 "github.com/golang/protobuf/v2/internal/encoding/wire"
22 "github.com/golang/protobuf/v2/reflect/protoreflect"
23 "github.com/golang/protobuf/v2/reflect/prototype"
Joe Tsaib4e370e2018-08-15 14:59:51 -070024)
25
26func main() {
27 log.SetFlags(0)
28 log.SetOutput(os.Stderr)
29
30 var fs fields
Joe Tsai769b9e72018-09-06 06:00:40 -070031 var flagUsages []string
32 flagVar := func(value flag.Value, name, usage string) {
33 flagUsages = append(flagUsages, fmt.Sprintf(" -%-16v %v", name+" "+value.String(), usage))
34 flag.Var(value, name, usage)
35 }
36 flagBool := func(name, usage string) *bool {
37 flagUsages = append(flagUsages, fmt.Sprintf(" -%-16v %v", name, usage))
38 return flag.Bool(name, false, usage)
39 }
40 flagVar(fieldsFlag{&fs, protoreflect.BoolKind}, "bools", "List of bool fields")
41 flagVar(fieldsFlag{&fs, protoreflect.Int64Kind}, "ints", "List of int32 or int64 fields")
42 flagVar(fieldsFlag{&fs, protoreflect.Sint64Kind}, "sints", "List of sint32 or sint64 fields")
43 flagVar(fieldsFlag{&fs, protoreflect.Uint64Kind}, "uints", "List of enum, uint32, or uint64 fields")
44 flagVar(fieldsFlag{&fs, protoreflect.Fixed32Kind}, "uint32s", "List of fixed32 fields")
45 flagVar(fieldsFlag{&fs, protoreflect.Sfixed32Kind}, "int32s", "List of sfixed32 fields")
46 flagVar(fieldsFlag{&fs, protoreflect.FloatKind}, "float32s", "List of float fields")
47 flagVar(fieldsFlag{&fs, protoreflect.Fixed64Kind}, "uint64s", "List of fixed64 fields")
48 flagVar(fieldsFlag{&fs, protoreflect.Sfixed64Kind}, "int64s", "List of sfixed64 fields")
49 flagVar(fieldsFlag{&fs, protoreflect.DoubleKind}, "float64s", "List of double fields")
50 flagVar(fieldsFlag{&fs, protoreflect.StringKind}, "strings", "List of string fields")
51 flagVar(fieldsFlag{&fs, protoreflect.BytesKind}, "bytes", "List of bytes fields")
52 flagVar(fieldsFlag{&fs, protoreflect.MessageKind}, "messages", "List of message fields")
53 flagVar(fieldsFlag{&fs, protoreflect.GroupKind}, "groups", "List of group fields")
54 printDesc := flagBool("print_descriptor", "Print the message descriptor")
55 printSource := flagBool("print_source", "Print the output in valid Go syntax")
Joe Tsaib4e370e2018-08-15 14:59:51 -070056 flag.Usage = func() {
Joe Tsai769b9e72018-09-06 06:00:40 -070057 fmt.Printf("Usage: %s [OPTIONS]... [INPUTS]...\n\n%s\n", filepath.Base(os.Args[0]), strings.Join(append([]string{
Joe Tsaib4e370e2018-08-15 14:59:51 -070058 "Print structured representations of encoded protocol buffer messages.",
59 "Since the protobuf wire format is not fully self-describing, type information",
60 "about the proto message can be provided using flags (e.g., -messages).",
61 "Each field list is a comma-separated list of field identifiers,",
62 "where each field identifier is a dot-separated list of field numbers,",
63 "identifying each field relative to the root message.",
64 "",
65 "For example, \"-messages 1,3,3.1 -float32s 1.2 -bools 3.1.2\" represents:",
66 "",
Joe Tsai769b9e72018-09-06 06:00:40 -070067 " message M {",
68 " optional M1 f1 = 1; // -messages 1",
69 " message M1 {",
70 " repeated float f2 = 2; // -float32s 1.2",
71 " }",
72 " optional M3 f3 = 3; // -messages 3",
73 " message M3 {",
74 " optional M1 f1 = 1; // -messages 3.1",
75 " message M1 {",
76 " repeated bool f2 = 2; // -bools 3.1.2",
77 " }",
78 " }",
79 " }",
Joe Tsaib4e370e2018-08-15 14:59:51 -070080 "",
81 "Arbitrarily complex message schemas can be represented using these flags.",
82 "Scalar field types are marked as repeated so that pbdump can decode",
83 "the packed representations of such field types.",
84 "",
85 "If no inputs are specified, the wire data is read in from stdin, otherwise",
86 "the contents of each specified input file is concatenated and",
87 "treated as one large message.",
88 "",
89 "Options:",
Joe Tsai769b9e72018-09-06 06:00:40 -070090 }, flagUsages...), "\n"))
Joe Tsaib4e370e2018-08-15 14:59:51 -070091 }
92 flag.Parse()
93
94 // Create message types.
95 var desc protoreflect.MessageDescriptor
96 if len(fs) > 0 {
97 var err error
98 desc, err = fs.Descriptor()
99 if err != nil {
100 log.Fatalf("Descriptor error: %v", err)
101 }
102 if *printDesc {
Joe Tsai769b9e72018-09-06 06:00:40 -0700103 fmt.Printf("%#v\n", desc)
Joe Tsaib4e370e2018-08-15 14:59:51 -0700104 }
105 }
106
107 // Read message input.
108 var buf []byte
109 if flag.NArg() == 0 {
110 b, err := ioutil.ReadAll(os.Stdin)
111 if err != nil {
112 log.Fatalf("ReadAll error: %v", err)
113 }
114 buf = b
115 }
116 for _, f := range flag.Args() {
117 b, err := ioutil.ReadFile(f)
118 if err != nil {
119 log.Fatalf("ReadFile error: %v", err)
120 }
121 buf = append(buf, b...)
122 }
123
124 // Parse and print message structure.
125 defer log.Printf("fatal input: %q", buf) // debug printout if panic occurs
126 var m pack.Message
127 m.UnmarshalDescriptor(buf, desc)
128 if *printSource {
Joe Tsai769b9e72018-09-06 06:00:40 -0700129 fmt.Printf("%#v\n", m)
Joe Tsaib4e370e2018-08-15 14:59:51 -0700130 } else {
Joe Tsai769b9e72018-09-06 06:00:40 -0700131 fmt.Printf("%+v\n", m)
Joe Tsaib4e370e2018-08-15 14:59:51 -0700132 }
133 if !bytes.Equal(buf, m.Marshal()) || len(buf) != m.Size() {
134 log.Fatalf("roundtrip mismatch:\n\tgot: %d %x\n\twant: %d %x", m.Size(), m, len(buf), buf)
135 }
136 os.Exit(0) // exit cleanly, avoid debug printout
137}
138
139// fields is a tree of fields, keyed by a field number.
140// Fields representing messages or groups have sub-fields.
141type fields map[wire.Number]*field
142type field struct {
143 kind protoreflect.Kind
144 sub fields // only for MessageKind or GroupKind
145}
146
147// Set parses s as a comma-separated list (see the help above for the format)
148// and treats each field identifier as the specified kind.
149func (fs *fields) Set(s string, k protoreflect.Kind) error {
150 if *fs == nil {
151 *fs = make(fields)
152 }
153 for _, s := range strings.Split(s, ",") {
154 if err := fs.set("", strings.TrimSpace(s), k); err != nil {
155 return err
156 }
157 }
158 return nil
159}
160func (fs fields) set(prefix, s string, k protoreflect.Kind) error {
161 if s == "" {
162 return nil
163 }
164
165 // Parse next field number.
166 i := strings.IndexByte(s, '.')
167 if i < 0 {
168 i = len(s)
169 }
170 prefix = strings.TrimPrefix(prefix+"."+s[:i], ".")
171 n, _ := strconv.ParseInt(s[:i], 10, 32)
172 num := wire.Number(n)
173 if num < wire.MinValidNumber || wire.MaxValidNumber < num {
174 return fmt.Errorf("invalid field: %v", prefix)
175 }
176 s = strings.TrimPrefix(s[i:], ".")
177
178 // Handle the current field.
179 if fs[num] == nil {
180 fs[num] = &field{0, make(fields)}
181 }
182 if len(s) == 0 {
183 if fs[num].kind.IsValid() {
184 return fmt.Errorf("field %v already set as %v type", prefix, fs[num].kind)
185 }
186 fs[num].kind = k
187 }
188 if err := fs[num].sub.set(prefix, s, k); err != nil {
189 return err
190 }
191
192 // Verify that only messages or groups can have sub-fields.
193 k2 := fs[num].kind
194 if k2 > 0 && k2 != protoreflect.MessageKind && k2 != protoreflect.GroupKind && len(fs[num].sub) > 0 {
195 return fmt.Errorf("field %v of %v type cannot have sub-fields", prefix, k2)
196 }
197 return nil
198}
199
200// Descriptor returns the field tree as a message descriptor.
201func (fs fields) Descriptor() (protoreflect.MessageDescriptor, error) {
202 ftyp, err := prototype.NewFile(&prototype.File{
203 Syntax: protoreflect.Proto2,
204 Messages: []prototype.Message{fs.messageDescriptor("M")},
205 })
206 if err != nil {
207 return nil, err
208 }
209 return ftyp.Messages().Get(0), nil
210}
211func (fs fields) messageDescriptor(name protoreflect.FullName) prototype.Message {
212 m := prototype.Message{Name: name.Name()}
213 for _, n := range fs.sortedNums() {
214 f := prototype.Field{
215 Name: protoreflect.Name(fmt.Sprintf("f%d", n)),
216 Number: n,
217 Cardinality: protoreflect.Optional,
218 Kind: fs[n].kind,
219 }
220 if !f.Kind.IsValid() {
221 f.Kind = protoreflect.MessageKind
222 }
223 switch f.Kind {
224 case protoreflect.BoolKind, protoreflect.EnumKind,
225 protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Uint32Kind,
226 protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Uint64Kind,
227 protoreflect.Sfixed32Kind, protoreflect.Fixed32Kind, protoreflect.FloatKind,
228 protoreflect.Sfixed64Kind, protoreflect.Fixed64Kind, protoreflect.DoubleKind:
229 f.Cardinality = protoreflect.Repeated
230 f.IsPacked = true
231 case protoreflect.MessageKind, protoreflect.GroupKind:
232 s := name.Append(protoreflect.Name(fmt.Sprintf("M%d", n)))
233 f.MessageType = prototype.PlaceholderMessage(s)
234 m.Messages = append(m.Messages, fs[n].sub.messageDescriptor(s))
235 }
236 m.Fields = append(m.Fields, f)
237 }
238 return m
239}
240
241func (fs fields) sortedNums() (ns []wire.Number) {
242 for n := range fs {
243 ns = append(ns, n)
244 }
245 sort.Slice(ns, func(i, j int) bool { return ns[i] < ns[j] })
246 return ns
247}
248
Joe Tsai769b9e72018-09-06 06:00:40 -0700249// fieldsFlag is an implementation of flag.Value that is keyed a specific kind.
250type fieldsFlag struct {
251 f *fields
252 k protoreflect.Kind
253}
Joe Tsaib4e370e2018-08-15 14:59:51 -0700254
Joe Tsai769b9e72018-09-06 06:00:40 -0700255func (fs fieldsFlag) String() string { return "FIELDS" }
256func (fs fieldsFlag) Set(s string) error { return fs.f.Set(s, fs.k) }