summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/other/ProcessedStack.h
blob: 375cab37a722d4b71d47ce40bcd3216b38f00ce0 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef ProcessedStack_h__
#define ProcessedStack_h__

#include <vector>

#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/ipc/MessageChannel.h"
#include "mozilla/Vector.h"
#include "nsStringFwd.h"
#include "shared-libraries.h"

namespace mozilla {
namespace Telemetry {

// This class represents a stack trace and the modules referenced in that trace.
// It is designed to be easy to read and write to disk or network and doesn't
// include any logic on how to collect or read the information it stores.
class ProcessedStack {
 public:
  ProcessedStack();
  size_t GetStackSize() const;
  size_t GetNumModules() const;

  struct Frame {
    // The offset of this program counter in its module or an absolute pc.
    uintptr_t mOffset;
    // The index to pass to GetModule to get the module this program counter
    // was in.
    uint16_t mModIndex;
  };
  struct Module {
    // The file name, /foo/bar/libxul.so for example.
    // It can contain unicode characters.
    nsString mName;
    nsCString mBreakpadId;

    bool operator==(const Module& other) const;
  };

  const Frame& GetFrame(unsigned aIndex) const;
  void AddFrame(const Frame& aFrame);
  const Module& GetModule(unsigned aIndex) const;
  void AddModule(const Module& aFrame);

  void Clear();

 private:
  std::vector<Module> mModules;
  std::vector<Frame> mStack;
};

// Get the current list of loaded modules, filter and pair it to the provided
// stack. We let the caller collect the stack since different callers have
// different needs (current thread X main thread, stopping the thread, etc).
ProcessedStack GetStackAndModules(const std::vector<uintptr_t>& aPCs);

// This class optimizes repeated calls to GetStackAndModules.
class BatchProcessedStackGenerator {
 public:
  BatchProcessedStackGenerator();
  ProcessedStack GetStackAndModules(const std::vector<uintptr_t>& aPCs);

  template <typename AllocatorPolicy>
  ProcessedStack GetStackAndModules(
      const Vector<void*, 0, AllocatorPolicy>& aPCs) {
    return GetStackAndModules(reinterpret_cast<const uintptr_t*>(aPCs.begin()),
                              reinterpret_cast<const uintptr_t*>(aPCs.end()));
  }

 private:
  ProcessedStack GetStackAndModules(const uintptr_t* aBegin,
                                    const uintptr_t* aEnd);
#if defined(MOZ_GECKO_PROFILER)
  SharedLibraryInfo mSortedRawModules;
#endif
};

}  // namespace Telemetry
}  // namespace mozilla

namespace IPC {

template <>
struct ParamTraits<mozilla::Telemetry::ProcessedStack::Module> {
  typedef mozilla::Telemetry::ProcessedStack::Module paramType;

  static void Write(MessageWriter* aWriter, const paramType& aParam) {
    WriteParam(aWriter, aParam.mName);
    WriteParam(aWriter, aParam.mBreakpadId);
  }

  static bool Read(MessageReader* aReader, paramType* aResult) {
    if (!ReadParam(aReader, &aResult->mName)) {
      return false;
    }

    if (!ReadParam(aReader, &aResult->mBreakpadId)) {
      return false;
    }

    return true;
  }
};

template <>
struct ParamTraits<mozilla::Telemetry::ProcessedStack::Frame> {
  typedef mozilla::Telemetry::ProcessedStack::Frame paramType;

  static void Write(MessageWriter* aWriter, const paramType& aParam) {
    WriteParam(aWriter, aParam.mOffset);
    WriteParam(aWriter, aParam.mModIndex);
  }

  static bool Read(MessageReader* aReader, paramType* aResult) {
    if (!ReadParam(aReader, &aResult->mOffset)) {
      return false;
    }

    if (!ReadParam(aReader, &aResult->mModIndex)) {
      return false;
    }

    return true;
  }
};

}  // namespace IPC

#endif  // ProcessedStack_h__