|
| 1 | +// Copyright 2024 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 | +package vscgo |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "log" |
| 11 | + "net" |
| 12 | + "net/http" |
| 13 | + "os" |
| 14 | + |
| 15 | + "github.com/google/pprof/profile" |
| 16 | +) |
| 17 | + |
| 18 | +func runPprofDump(args []string) error { |
| 19 | + if len(args) != 1 { |
| 20 | + return fmt.Errorf("usage: dump-pprof <profile>") |
| 21 | + } |
| 22 | + |
| 23 | + p, err := readPprof(args[0]) |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + |
| 28 | + return json.NewEncoder(os.Stdout).Encode((*Profile)(p)) |
| 29 | +} |
| 30 | + |
| 31 | +func runPprofServe(args []string) error { |
| 32 | + if len(args) != 2 { |
| 33 | + return fmt.Errorf("usage: serve-pprof <addr> <profile>") |
| 34 | + } |
| 35 | + |
| 36 | + l, err := net.Listen("tcp", args[0]) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + defer l.Close() |
| 41 | + |
| 42 | + p, err := readPprof(args[1]) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + err = json.NewEncoder(os.Stdout).Encode(map[string]any{ |
| 48 | + "Listen": l.Addr(), |
| 49 | + }) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + return http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 55 | + w.Header().Set("Access-Control-Allow-Origin", "*") |
| 56 | + w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS") |
| 57 | + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") |
| 58 | + |
| 59 | + if r.Method == "OPTIONS" { |
| 60 | + w.WriteHeader(http.StatusOK) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + w.Header().Set("Content-Type", "application/json") |
| 65 | + err := json.NewEncoder(w).Encode((*Profile)(p)) |
| 66 | + if err != nil { |
| 67 | + log.Println("Error: ", err) |
| 68 | + } |
| 69 | + })) |
| 70 | +} |
| 71 | + |
| 72 | +func readPprof(arg string) (*Profile, error) { |
| 73 | + f, err := os.Open(arg) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + defer f.Close() |
| 78 | + |
| 79 | + p, err := profile.Parse(f) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + return (*Profile)(p), nil |
| 85 | +} |
| 86 | + |
| 87 | +type Profile profile.Profile |
| 88 | + |
| 89 | +func (p *Profile) MarshalJSON() ([]byte, error) { |
| 90 | + q := struct { |
| 91 | + SampleType []*profile.ValueType |
| 92 | + DefaultSampleType string |
| 93 | + Sample []*Sample |
| 94 | + Mapping []*profile.Mapping |
| 95 | + Location []*Location |
| 96 | + Function []*profile.Function |
| 97 | + Comments []string |
| 98 | + DropFrames string |
| 99 | + KeepFrames string |
| 100 | + TimeNanos int64 |
| 101 | + DurationNanos int64 |
| 102 | + PeriodType *profile.ValueType |
| 103 | + Period int64 |
| 104 | + }{ |
| 105 | + SampleType: p.SampleType, |
| 106 | + DefaultSampleType: p.DefaultSampleType, |
| 107 | + Sample: make([]*Sample, len(p.Sample)), |
| 108 | + Mapping: p.Mapping, |
| 109 | + Location: make([]*Location, len(p.Location)), |
| 110 | + Function: p.Function, |
| 111 | + Comments: p.Comments, |
| 112 | + DropFrames: p.DropFrames, |
| 113 | + KeepFrames: p.KeepFrames, |
| 114 | + TimeNanos: p.TimeNanos, |
| 115 | + DurationNanos: p.DurationNanos, |
| 116 | + PeriodType: p.PeriodType, |
| 117 | + Period: p.Period, |
| 118 | + } |
| 119 | + for i, s := range p.Sample { |
| 120 | + q.Sample[i] = (*Sample)(s) |
| 121 | + } |
| 122 | + for i, l := range p.Location { |
| 123 | + q.Location[i] = (*Location)(l) |
| 124 | + } |
| 125 | + return json.Marshal(q) |
| 126 | +} |
| 127 | + |
| 128 | +type Sample profile.Sample |
| 129 | + |
| 130 | +func (p *Sample) MarshalJSON() ([]byte, error) { |
| 131 | + q := struct { |
| 132 | + Location []uint64 |
| 133 | + Value []int64 |
| 134 | + Label map[string][]string |
| 135 | + NumLabel map[string][]int64 |
| 136 | + NumUnit map[string][]string |
| 137 | + }{ |
| 138 | + Location: make([]uint64, len(p.Location)), |
| 139 | + Value: p.Value, |
| 140 | + Label: p.Label, |
| 141 | + NumLabel: p.NumLabel, |
| 142 | + NumUnit: p.NumUnit, |
| 143 | + } |
| 144 | + for i, l := range p.Location { |
| 145 | + q.Location[i] = l.ID |
| 146 | + } |
| 147 | + return json.Marshal(q) |
| 148 | +} |
| 149 | + |
| 150 | +type Location profile.Location |
| 151 | + |
| 152 | +func (p *Location) MarshalJSON() ([]byte, error) { |
| 153 | + q := struct { |
| 154 | + ID uint64 |
| 155 | + Mapping uint64 |
| 156 | + Address uint64 |
| 157 | + Line []Line |
| 158 | + IsFolded bool |
| 159 | + }{ |
| 160 | + ID: p.ID, |
| 161 | + Mapping: p.Mapping.ID, |
| 162 | + Address: p.Address, |
| 163 | + Line: make([]Line, len(p.Line)), |
| 164 | + IsFolded: p.IsFolded, |
| 165 | + } |
| 166 | + for i, l := range p.Line { |
| 167 | + q.Line[i] = Line(l) |
| 168 | + } |
| 169 | + return json.Marshal(q) |
| 170 | +} |
| 171 | + |
| 172 | +type Line profile.Line |
| 173 | + |
| 174 | +func (p *Line) MarshalJSON() ([]byte, error) { |
| 175 | + q := struct { |
| 176 | + Function uint64 |
| 177 | + Line int64 |
| 178 | + Column int64 |
| 179 | + }{ |
| 180 | + Function: p.Function.ID, |
| 181 | + Line: p.Line, |
| 182 | + Column: p.Column, |
| 183 | + } |
| 184 | + return json.Marshal(q) |
| 185 | +} |
0 commit comments