Trace.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Trace.h - XRay Trace Abstraction -----------------------------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Defines the XRay Trace class representing records in an XRay trace file.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_XRAY_TRACE_H
  18. #define LLVM_XRAY_TRACE_H
  19. #include <cstdint>
  20. #include <vector>
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/Support/DataExtractor.h"
  23. #include "llvm/Support/Error.h"
  24. #include "llvm/XRay/XRayRecord.h"
  25. namespace llvm {
  26. namespace xray {
  27. /// A Trace object represents the records that have been loaded from XRay
  28. /// log files generated by instrumented binaries. We encapsulate the logic of
  29. /// reading the traces in factory functions that populate the Trace object
  30. /// appropriately.
  31. ///
  32. /// Trace objects provide an accessor to an XRayFileHeader which says more about
  33. /// details of the file from which the XRay trace was loaded from.
  34. ///
  35. /// Usage:
  36. ///
  37. /// if (auto TraceOrErr = loadTraceFile("xray-log.something.xray")) {
  38. /// auto& T = *TraceOrErr;
  39. /// // T.getFileHeader() will provide information from the trace header.
  40. /// for (const XRayRecord &R : T) {
  41. /// // ... do something with R here.
  42. /// }
  43. /// } else {
  44. /// // Handle the error here.
  45. /// }
  46. ///
  47. class Trace {
  48. XRayFileHeader FileHeader;
  49. using RecordVector = std::vector<XRayRecord>;
  50. RecordVector Records;
  51. typedef std::vector<XRayRecord>::const_iterator citerator;
  52. friend Expected<Trace> loadTrace(const DataExtractor &, bool);
  53. public:
  54. using size_type = RecordVector::size_type;
  55. using value_type = RecordVector::value_type;
  56. using const_iterator = RecordVector::const_iterator;
  57. /// Provides access to the loaded XRay trace file header.
  58. const XRayFileHeader &getFileHeader() const { return FileHeader; }
  59. const_iterator begin() const { return Records.begin(); }
  60. const_iterator end() const { return Records.end(); }
  61. bool empty() const { return Records.empty(); }
  62. size_type size() const { return Records.size(); }
  63. };
  64. /// This function will attempt to load XRay trace records from the provided
  65. /// |Filename|.
  66. Expected<Trace> loadTraceFile(StringRef Filename, bool Sort = false);
  67. /// This function will attempt to load XRay trace records from the provided
  68. /// DataExtractor.
  69. Expected<Trace> loadTrace(const DataExtractor &Extractor, bool Sort = false);
  70. } // namespace xray
  71. } // namespace llvm
  72. #endif // LLVM_XRAY_TRACE_H
  73. #ifdef __GNUC__
  74. #pragma GCC diagnostic pop
  75. #endif