Нема описа

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