summaryrefslogtreecommitdiffstats
path: root/src/internal/trace/writer.go
blob: dd0b9f104eda8f7dcbf59e67672ec9b6e4a35ae4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package trace

import "bytes"

// Writer is a test trace writer.
type Writer struct {
	bytes.Buffer
}

func NewWriter() *Writer {
	w := new(Writer)
	w.Write([]byte("go 1.9 trace\x00\x00\x00\x00"))
	return w
}

// Emit writes an event record to the trace.
// See Event types for valid types and required arguments.
func (w *Writer) Emit(typ byte, args ...uint64) {
	nargs := byte(len(args)) - 1
	if nargs > 3 {
		nargs = 3
	}
	buf := []byte{typ | nargs<<6}
	if nargs == 3 {
		buf = append(buf, 0)
	}
	for _, a := range args {
		buf = appendVarint(buf, a)
	}
	if nargs == 3 {
		buf[1] = byte(len(buf) - 2)
	}
	n, err := w.Write(buf)
	if n != len(buf) || err != nil {
		panic("failed to write")
	}
}

func appendVarint(buf []byte, v uint64) []byte {
	for ; v >= 0x80; v >>= 7 {
		buf = append(buf, 0x80|byte(v))
	}
	buf = append(buf, byte(v))
	return buf
}