暫無描述

profile.go 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package profile provides a representation of profile.proto and
  15. // methods to encode/decode profiles in this format.
  16. package profile
  17. import (
  18. "bytes"
  19. "compress/gzip"
  20. "fmt"
  21. "io"
  22. "io/ioutil"
  23. "path/filepath"
  24. "regexp"
  25. "sort"
  26. "strings"
  27. "sync"
  28. "time"
  29. )
  30. // Profile is an in-memory representation of profile.proto.
  31. type Profile struct {
  32. SampleType []*ValueType
  33. DefaultSampleType string
  34. Sample []*Sample
  35. Mapping []*Mapping
  36. Location []*Location
  37. Function []*Function
  38. Comments []string
  39. DropFrames string
  40. KeepFrames string
  41. TimeNanos int64
  42. DurationNanos int64
  43. PeriodType *ValueType
  44. Period int64
  45. // The following fields are modified during encoding and copying,
  46. // so are protected by a Mutex.
  47. encodeMu sync.Mutex
  48. commentX []int64
  49. dropFramesX int64
  50. keepFramesX int64
  51. stringTable []string
  52. defaultSampleTypeX int64
  53. }
  54. // ValueType corresponds to Profile.ValueType
  55. type ValueType struct {
  56. Type string // cpu, wall, inuse_space, etc
  57. Unit string // seconds, nanoseconds, bytes, etc
  58. typeX int64
  59. unitX int64
  60. }
  61. // Sample corresponds to Profile.Sample
  62. type Sample struct {
  63. Location []*Location
  64. Value []int64
  65. Label map[string][]string
  66. NumLabel map[string][]int64
  67. NumUnit map[string][]string
  68. locationIDX []uint64
  69. labelX []label
  70. }
  71. // label corresponds to Profile.Label
  72. type label struct {
  73. keyX int64
  74. // Exactly one of the two following values must be set
  75. strX int64
  76. numX int64 // Integer value for this label
  77. // can be set if numX has value
  78. unitX int64
  79. }
  80. // Mapping corresponds to Profile.Mapping
  81. type Mapping struct {
  82. ID uint64
  83. Start uint64
  84. Limit uint64
  85. Offset uint64
  86. File string
  87. BuildID string
  88. HasFunctions bool
  89. HasFilenames bool
  90. HasLineNumbers bool
  91. HasInlineFrames bool
  92. fileX int64
  93. buildIDX int64
  94. }
  95. // Location corresponds to Profile.Location
  96. type Location struct {
  97. ID uint64
  98. Mapping *Mapping
  99. Address uint64
  100. Line []Line
  101. mappingIDX uint64
  102. }
  103. // Line corresponds to Profile.Line
  104. type Line struct {
  105. Function *Function
  106. Line int64
  107. functionIDX uint64
  108. }
  109. // Function corresponds to Profile.Function
  110. type Function struct {
  111. ID uint64
  112. Name string
  113. SystemName string
  114. Filename string
  115. StartLine int64
  116. nameX int64
  117. systemNameX int64
  118. filenameX int64
  119. }
  120. // Parse parses a profile and checks for its validity. The input
  121. // may be a gzip-compressed encoded protobuf or one of many legacy
  122. // profile formats which may be unsupported in the future.
  123. func Parse(r io.Reader) (*Profile, error) {
  124. data, err := ioutil.ReadAll(r)
  125. if err != nil {
  126. return nil, err
  127. }
  128. return ParseData(data)
  129. }
  130. // ParseData parses a profile from a buffer and checks for its
  131. // validity.
  132. func ParseData(data []byte) (*Profile, error) {
  133. var p *Profile
  134. var err error
  135. if len(data) >= 2 && data[0] == 0x1f && data[1] == 0x8b {
  136. gz, err := gzip.NewReader(bytes.NewBuffer(data))
  137. if err == nil {
  138. data, err = ioutil.ReadAll(gz)
  139. }
  140. if err != nil {
  141. return nil, fmt.Errorf("decompressing profile: %v", err)
  142. }
  143. }
  144. if p, err = ParseUncompressed(data); err != nil && err != errNoData {
  145. p, err = parseLegacy(data)
  146. }
  147. if err != nil {
  148. return nil, fmt.Errorf("parsing profile: %v", err)
  149. }
  150. if err := p.CheckValid(); err != nil {
  151. return nil, fmt.Errorf("malformed profile: %v", err)
  152. }
  153. return p, nil
  154. }
  155. var errUnrecognized = fmt.Errorf("unrecognized profile format")
  156. var errMalformed = fmt.Errorf("malformed profile format")
  157. var errNoData = fmt.Errorf("empty input file")
  158. func parseLegacy(data []byte) (*Profile, error) {
  159. parsers := []func([]byte) (*Profile, error){
  160. parseCPU,
  161. parseHeap,
  162. parseGoCount, // goroutine, threadcreate
  163. parseThread,
  164. parseContention,
  165. parseJavaProfile,
  166. }
  167. for _, parser := range parsers {
  168. p, err := parser(data)
  169. if err == nil {
  170. p.addLegacyFrameInfo()
  171. return p, nil
  172. }
  173. if err != errUnrecognized {
  174. return nil, err
  175. }
  176. }
  177. return nil, errUnrecognized
  178. }
  179. // ParseUncompressed parses an uncompressed protobuf into a profile.
  180. func ParseUncompressed(data []byte) (*Profile, error) {
  181. if len(data) == 0 {
  182. return nil, errNoData
  183. }
  184. p := &Profile{}
  185. if err := unmarshal(data, p); err != nil {
  186. return nil, err
  187. }
  188. if err := p.postDecode(); err != nil {
  189. return nil, err
  190. }
  191. return p, nil
  192. }
  193. var libRx = regexp.MustCompile(`([.]so$|[.]so[._][0-9]+)`)
  194. // massageMappings applies heuristic-based changes to the profile
  195. // mappings to account for quirks of some environments.
  196. func (p *Profile) massageMappings() {
  197. // Merge adjacent regions with matching names, checking that the offsets match
  198. if len(p.Mapping) > 1 {
  199. mappings := []*Mapping{p.Mapping[0]}
  200. for _, m := range p.Mapping[1:] {
  201. lm := mappings[len(mappings)-1]
  202. if adjacent(lm, m) {
  203. lm.Limit = m.Limit
  204. if m.File != "" {
  205. lm.File = m.File
  206. }
  207. if m.BuildID != "" {
  208. lm.BuildID = m.BuildID
  209. }
  210. p.updateLocationMapping(m, lm)
  211. continue
  212. }
  213. mappings = append(mappings, m)
  214. }
  215. p.Mapping = mappings
  216. }
  217. // Use heuristics to identify main binary and move it to the top of the list of mappings
  218. for i, m := range p.Mapping {
  219. file := strings.TrimSpace(strings.Replace(m.File, "(deleted)", "", -1))
  220. if len(file) == 0 {
  221. continue
  222. }
  223. if len(libRx.FindStringSubmatch(file)) > 0 {
  224. continue
  225. }
  226. if file[0] == '[' {
  227. continue
  228. }
  229. // Swap what we guess is main to position 0.
  230. p.Mapping[0], p.Mapping[i] = p.Mapping[i], p.Mapping[0]
  231. break
  232. }
  233. // Keep the mapping IDs neatly sorted
  234. for i, m := range p.Mapping {
  235. m.ID = uint64(i + 1)
  236. }
  237. }
  238. // adjacent returns whether two mapping entries represent the same
  239. // mapping that has been split into two. Check that their addresses are adjacent,
  240. // and if the offsets match, if they are available.
  241. func adjacent(m1, m2 *Mapping) bool {
  242. if m1.File != "" && m2.File != "" {
  243. if m1.File != m2.File {
  244. return false
  245. }
  246. }
  247. if m1.BuildID != "" && m2.BuildID != "" {
  248. if m1.BuildID != m2.BuildID {
  249. return false
  250. }
  251. }
  252. if m1.Limit != m2.Start {
  253. return false
  254. }
  255. if m1.Offset != 0 && m2.Offset != 0 {
  256. offset := m1.Offset + (m1.Limit - m1.Start)
  257. if offset != m2.Offset {
  258. return false
  259. }
  260. }
  261. return true
  262. }
  263. func (p *Profile) updateLocationMapping(from, to *Mapping) {
  264. for _, l := range p.Location {
  265. if l.Mapping == from {
  266. l.Mapping = to
  267. }
  268. }
  269. }
  270. func serialize(p *Profile) []byte {
  271. p.encodeMu.Lock()
  272. p.preEncode()
  273. b := marshal(p)
  274. p.encodeMu.Unlock()
  275. return b
  276. }
  277. // Write writes the profile as a gzip-compressed marshaled protobuf.
  278. func (p *Profile) Write(w io.Writer) error {
  279. zw := gzip.NewWriter(w)
  280. defer zw.Close()
  281. _, err := zw.Write(serialize(p))
  282. return err
  283. }
  284. // WriteUncompressed writes the profile as a marshaled protobuf.
  285. func (p *Profile) WriteUncompressed(w io.Writer) error {
  286. _, err := w.Write(serialize(p))
  287. return err
  288. }
  289. // CheckValid tests whether the profile is valid. Checks include, but are
  290. // not limited to:
  291. // - len(Profile.Sample[n].value) == len(Profile.value_unit)
  292. // - Sample.id has a corresponding Profile.Location
  293. func (p *Profile) CheckValid() error {
  294. // Check that sample values are consistent
  295. sampleLen := len(p.SampleType)
  296. if sampleLen == 0 && len(p.Sample) != 0 {
  297. return fmt.Errorf("missing sample type information")
  298. }
  299. for _, s := range p.Sample {
  300. if s == nil {
  301. return fmt.Errorf("profile has nil sample")
  302. }
  303. if len(s.Value) != sampleLen {
  304. return fmt.Errorf("mismatch: sample has %d values vs. %d types", len(s.Value), len(p.SampleType))
  305. }
  306. for _, l := range s.Location {
  307. if l == nil {
  308. return fmt.Errorf("sample has nil location")
  309. }
  310. }
  311. }
  312. // Check that all mappings/locations/functions are in the tables
  313. // Check that there are no duplicate ids
  314. mappings := make(map[uint64]*Mapping, len(p.Mapping))
  315. for _, m := range p.Mapping {
  316. if m == nil {
  317. return fmt.Errorf("profile has nil mapping")
  318. }
  319. if m.ID == 0 {
  320. return fmt.Errorf("found mapping with reserved ID=0")
  321. }
  322. if mappings[m.ID] != nil {
  323. return fmt.Errorf("multiple mappings with same id: %d", m.ID)
  324. }
  325. mappings[m.ID] = m
  326. }
  327. functions := make(map[uint64]*Function, len(p.Function))
  328. for _, f := range p.Function {
  329. if f == nil {
  330. return fmt.Errorf("profile has nil function")
  331. }
  332. if f.ID == 0 {
  333. return fmt.Errorf("found function with reserved ID=0")
  334. }
  335. if functions[f.ID] != nil {
  336. return fmt.Errorf("multiple functions with same id: %d", f.ID)
  337. }
  338. functions[f.ID] = f
  339. }
  340. locations := make(map[uint64]*Location, len(p.Location))
  341. for _, l := range p.Location {
  342. if l == nil {
  343. return fmt.Errorf("profile has nil location")
  344. }
  345. if l.ID == 0 {
  346. return fmt.Errorf("found location with reserved id=0")
  347. }
  348. if locations[l.ID] != nil {
  349. return fmt.Errorf("multiple locations with same id: %d", l.ID)
  350. }
  351. locations[l.ID] = l
  352. if m := l.Mapping; m != nil {
  353. if m.ID == 0 || mappings[m.ID] != m {
  354. return fmt.Errorf("inconsistent mapping %p: %d", m, m.ID)
  355. }
  356. }
  357. for _, ln := range l.Line {
  358. if f := ln.Function; f != nil {
  359. if f.ID == 0 || functions[f.ID] != f {
  360. return fmt.Errorf("inconsistent function %p: %d", f, f.ID)
  361. }
  362. }
  363. }
  364. }
  365. return nil
  366. }
  367. // Aggregate merges the locations in the profile into equivalence
  368. // classes preserving the request attributes. It also updates the
  369. // samples to point to the merged locations.
  370. func (p *Profile) Aggregate(inlineFrame, function, filename, linenumber, address bool) error {
  371. for _, m := range p.Mapping {
  372. m.HasInlineFrames = m.HasInlineFrames && inlineFrame
  373. m.HasFunctions = m.HasFunctions && function
  374. m.HasFilenames = m.HasFilenames && filename
  375. m.HasLineNumbers = m.HasLineNumbers && linenumber
  376. }
  377. // Aggregate functions
  378. if !function || !filename {
  379. for _, f := range p.Function {
  380. if !function {
  381. f.Name = ""
  382. f.SystemName = ""
  383. }
  384. if !filename {
  385. f.Filename = ""
  386. }
  387. }
  388. }
  389. // Aggregate locations
  390. if !inlineFrame || !address || !linenumber {
  391. for _, l := range p.Location {
  392. if !inlineFrame && len(l.Line) > 1 {
  393. l.Line = l.Line[len(l.Line)-1:]
  394. }
  395. if !linenumber {
  396. for i := range l.Line {
  397. l.Line[i].Line = 0
  398. }
  399. }
  400. if !address {
  401. l.Address = 0
  402. }
  403. }
  404. }
  405. return p.CheckValid()
  406. }
  407. // NumLabelUnits returns a map of numeric label keys to the units
  408. // associated with those keys and a map of those keys to any units
  409. // that were encountered but not used.
  410. // Unit for a given key is the first encountered unit for that key. If multiple
  411. // units are encountered for values paired with a particular key, then the first
  412. // unit encountered is used and all other units are returned in sorted order
  413. // in map of ignored units.
  414. // If no units are encountered for a particular key, the unit is then inferred
  415. // based on the key.
  416. func (p *Profile) NumLabelUnits() (map[string]string, map[string][]string) {
  417. numLabelUnits := map[string]string{}
  418. ignoredUnits := map[string]map[string]bool{}
  419. encounteredKeys := map[string]bool{}
  420. // Determine units based on numeric tags for each sample.
  421. for _, s := range p.Sample {
  422. for k := range s.NumLabel {
  423. encounteredKeys[k] = true
  424. for _, unit := range s.NumUnit[k] {
  425. if unit == "" {
  426. continue
  427. }
  428. if wantUnit, ok := numLabelUnits[k]; !ok {
  429. numLabelUnits[k] = unit
  430. } else if wantUnit != unit {
  431. if v, ok := ignoredUnits[k]; ok {
  432. v[unit] = true
  433. } else {
  434. ignoredUnits[k] = map[string]bool{unit: true}
  435. }
  436. }
  437. }
  438. }
  439. }
  440. // Infer units for keys without any units associated with
  441. // numeric tag values.
  442. for key := range encounteredKeys {
  443. unit := numLabelUnits[key]
  444. if unit == "" {
  445. switch key {
  446. case "alignment", "request":
  447. numLabelUnits[key] = "bytes"
  448. default:
  449. numLabelUnits[key] = key
  450. }
  451. }
  452. }
  453. // Copy ignored units into more readable format
  454. unitsIgnored := make(map[string][]string, len(ignoredUnits))
  455. for key, values := range ignoredUnits {
  456. units := make([]string, len(values))
  457. i := 0
  458. for unit := range values {
  459. units[i] = unit
  460. i++
  461. }
  462. sort.Strings(units)
  463. unitsIgnored[key] = units
  464. }
  465. return numLabelUnits, unitsIgnored
  466. }
  467. // String dumps a text representation of a profile. Intended mainly
  468. // for debugging purposes.
  469. func (p *Profile) String() string {
  470. ss := make([]string, 0, len(p.Comments)+len(p.Sample)+len(p.Mapping)+len(p.Location))
  471. for _, c := range p.Comments {
  472. ss = append(ss, "Comment: "+c)
  473. }
  474. if pt := p.PeriodType; pt != nil {
  475. ss = append(ss, fmt.Sprintf("PeriodType: %s %s", pt.Type, pt.Unit))
  476. }
  477. ss = append(ss, fmt.Sprintf("Period: %d", p.Period))
  478. if p.TimeNanos != 0 {
  479. ss = append(ss, fmt.Sprintf("Time: %v", time.Unix(0, p.TimeNanos)))
  480. }
  481. if p.DurationNanos != 0 {
  482. ss = append(ss, fmt.Sprintf("Duration: %.4v", time.Duration(p.DurationNanos)))
  483. }
  484. ss = append(ss, "Samples:")
  485. var sh1 string
  486. for _, s := range p.SampleType {
  487. dflt := ""
  488. if s.Type == p.DefaultSampleType {
  489. dflt = "[dflt]"
  490. }
  491. sh1 = sh1 + fmt.Sprintf("%s/%s%s ", s.Type, s.Unit, dflt)
  492. }
  493. ss = append(ss, strings.TrimSpace(sh1))
  494. for _, s := range p.Sample {
  495. ss = append(ss, s.string())
  496. }
  497. ss = append(ss, "Locations")
  498. for _, l := range p.Location {
  499. ss = append(ss, l.string())
  500. }
  501. ss = append(ss, "Mappings")
  502. for _, m := range p.Mapping {
  503. ss = append(ss, m.string())
  504. }
  505. return strings.Join(ss, "\n") + "\n"
  506. }
  507. // string dumps a text representation of a mapping. Intended mainly
  508. // for debugging purposes.
  509. func (m *Mapping) string() string {
  510. bits := ""
  511. if m.HasFunctions {
  512. bits = bits + "[FN]"
  513. }
  514. if m.HasFilenames {
  515. bits = bits + "[FL]"
  516. }
  517. if m.HasLineNumbers {
  518. bits = bits + "[LN]"
  519. }
  520. if m.HasInlineFrames {
  521. bits = bits + "[IN]"
  522. }
  523. return fmt.Sprintf("%d: %#x/%#x/%#x %s %s %s",
  524. m.ID,
  525. m.Start, m.Limit, m.Offset,
  526. m.File,
  527. m.BuildID,
  528. bits)
  529. }
  530. // string dumps a text representation of a location. Intended mainly
  531. // for debugging purposes.
  532. func (l *Location) string() string {
  533. ss := []string{}
  534. locStr := fmt.Sprintf("%6d: %#x ", l.ID, l.Address)
  535. if m := l.Mapping; m != nil {
  536. locStr = locStr + fmt.Sprintf("M=%d ", m.ID)
  537. }
  538. if len(l.Line) == 0 {
  539. ss = append(ss, locStr)
  540. }
  541. for li := range l.Line {
  542. lnStr := "??"
  543. if fn := l.Line[li].Function; fn != nil {
  544. lnStr = fmt.Sprintf("%s %s:%d s=%d",
  545. fn.Name,
  546. fn.Filename,
  547. l.Line[li].Line,
  548. fn.StartLine)
  549. if fn.Name != fn.SystemName {
  550. lnStr = lnStr + "(" + fn.SystemName + ")"
  551. }
  552. }
  553. ss = append(ss, locStr+lnStr)
  554. // Do not print location details past the first line
  555. locStr = " "
  556. }
  557. return strings.Join(ss, "\n")
  558. }
  559. // string dumps a text representation of a sample. Intended mainly
  560. // for debugging purposes.
  561. func (s *Sample) string() string {
  562. ss := []string{}
  563. var sv string
  564. for _, v := range s.Value {
  565. sv = fmt.Sprintf("%s %10d", sv, v)
  566. }
  567. sv = sv + ": "
  568. for _, l := range s.Location {
  569. sv = sv + fmt.Sprintf("%d ", l.ID)
  570. }
  571. ss = append(ss, sv)
  572. const labelHeader = " "
  573. if len(s.Label) > 0 {
  574. ss = append(ss, labelHeader+labelsToString(s.Label))
  575. }
  576. if len(s.NumLabel) > 0 {
  577. ss = append(ss, labelHeader+numLabelsToString(s.NumLabel, s.NumUnit))
  578. }
  579. return strings.Join(ss, "\n")
  580. }
  581. // labelsToString returns a string representation of a
  582. // map representing labels.
  583. func labelsToString(labels map[string][]string) string {
  584. ls := []string{}
  585. for k, v := range labels {
  586. ls = append(ls, fmt.Sprintf("%s:%v", k, v))
  587. }
  588. sort.Strings(ls)
  589. return strings.Join(ls, " ")
  590. }
  591. // numLablesToString returns a string representation of a map
  592. // representing numeric labels.
  593. func numLabelsToString(numLabels map[string][]int64, numUnits map[string][]string) string {
  594. ls := []string{}
  595. for k, v := range numLabels {
  596. units := numUnits[k]
  597. var labelString string
  598. if len(units) == len(v) {
  599. values := make([]string, len(v))
  600. for i, vv := range v {
  601. values[i] = fmt.Sprintf("%d %s", vv, units[i])
  602. }
  603. labelString = fmt.Sprintf("%s:%v", k, values)
  604. } else {
  605. labelString = fmt.Sprintf("%s:%v", k, v)
  606. }
  607. ls = append(ls, labelString)
  608. }
  609. sort.Strings(ls)
  610. return strings.Join(ls, " ")
  611. }
  612. // Scale multiplies all sample values in a profile by a constant.
  613. func (p *Profile) Scale(ratio float64) {
  614. if ratio == 1 {
  615. return
  616. }
  617. ratios := make([]float64, len(p.SampleType))
  618. for i := range p.SampleType {
  619. ratios[i] = ratio
  620. }
  621. p.ScaleN(ratios)
  622. }
  623. // ScaleN multiplies each sample values in a sample by a different amount.
  624. func (p *Profile) ScaleN(ratios []float64) error {
  625. if len(p.SampleType) != len(ratios) {
  626. return fmt.Errorf("mismatched scale ratios, got %d, want %d", len(ratios), len(p.SampleType))
  627. }
  628. allOnes := true
  629. for _, r := range ratios {
  630. if r != 1 {
  631. allOnes = false
  632. break
  633. }
  634. }
  635. if allOnes {
  636. return nil
  637. }
  638. for _, s := range p.Sample {
  639. for i, v := range s.Value {
  640. if ratios[i] != 1 {
  641. s.Value[i] = int64(float64(v) * ratios[i])
  642. }
  643. }
  644. }
  645. return nil
  646. }
  647. // HasFunctions determines if all locations in this profile have
  648. // symbolized function information.
  649. func (p *Profile) HasFunctions() bool {
  650. for _, l := range p.Location {
  651. if l.Mapping != nil && !l.Mapping.HasFunctions {
  652. return false
  653. }
  654. }
  655. return true
  656. }
  657. // HasFileLines determines if all locations in this profile have
  658. // symbolized file and line number information.
  659. func (p *Profile) HasFileLines() bool {
  660. for _, l := range p.Location {
  661. if l.Mapping != nil && (!l.Mapping.HasFilenames || !l.Mapping.HasLineNumbers) {
  662. return false
  663. }
  664. }
  665. return true
  666. }
  667. // Unsymbolizable returns true if a mapping points to a binary for which
  668. // locations can't be symbolized in principle, at least now. Examples are
  669. // "[vdso]", [vsyscall]" and some others, see the code.
  670. func (m *Mapping) Unsymbolizable() bool {
  671. name := filepath.Base(m.File)
  672. return strings.HasPrefix(name, "[") || strings.HasPrefix(name, "linux-vdso") || strings.HasPrefix(m.File, "/dev/dri/")
  673. }
  674. // Copy makes a fully independent copy of a profile.
  675. func (p *Profile) Copy() *Profile {
  676. pp := &Profile{}
  677. if err := unmarshal(serialize(p), pp); err != nil {
  678. panic(err)
  679. }
  680. if err := pp.postDecode(); err != nil {
  681. panic(err)
  682. }
  683. return pp
  684. }