Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
* vim: set ts=8 sts=2 et sw=2 tw=80:
3
*
4
* Copyright 2016 Mozilla Foundation
5
*
6
* Licensed under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
9
*
11
*
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
17
*/
18
19
#ifndef wasm_debug_h
20
#define wasm_debug_h
21
22
#include "js/HashTable.h"
23
#include "wasm/WasmModule.h"
24
#include "wasm/WasmTypes.h"
25
26
namespace js {
27
28
class Debugger;
29
class WasmBreakpointSite;
30
class WasmInstanceObject;
31
32
namespace wasm {
33
34
struct MetadataTier;
35
36
// The generated source location for the AST node/expression. The offset field
37
// refers an offset in an binary format file.
38
39
struct ExprLoc {
40
uint32_t lineno;
41
uint32_t column;
42
uint32_t offset;
43
ExprLoc() : lineno(0), column(0), offset(0) {}
44
ExprLoc(uint32_t lineno_, uint32_t column_, uint32_t offset_)
45
: lineno(lineno_), column(column_), offset(offset_) {}
46
};
47
48
typedef HashMap<uint32_t, uint32_t, DefaultHasher<uint32_t>, SystemAllocPolicy>
49
StepperCounters;
50
typedef HashMap<uint32_t, WasmBreakpointSite*, DefaultHasher<uint32_t>,
51
SystemAllocPolicy>
52
WasmBreakpointSiteMap;
53
54
class DebugState {
55
const SharedCode code_;
56
const SharedModule module_;
57
58
// State maintained when debugging is enabled. In this case, the Code is
59
// not actually shared, but is referenced uniquely by the instance that is
60
// being debugged.
61
62
bool enterFrameTrapsEnabled_;
63
uint32_t enterAndLeaveFrameTrapsCounter_;
64
WasmBreakpointSiteMap breakpointSites_;
65
StepperCounters stepperCounters_;
66
67
void toggleDebugTrap(uint32_t offset, bool enabled);
68
69
public:
70
DebugState(const Code& code, const Module& module);
71
72
void trace(JSTracer* trc);
73
void finalize(JSFreeOp* fop);
74
75
const Bytes& bytecode() const { return module_->debugBytecode(); }
76
77
MOZ_MUST_USE bool getLineOffsets(size_t lineno, Vector<uint32_t>* offsets);
78
MOZ_MUST_USE bool getAllColumnOffsets(Vector<ExprLoc>* offsets);
79
MOZ_MUST_USE bool getOffsetLocation(uint32_t offset, size_t* lineno,
80
size_t* column);
81
82
// The Code can track enter/leave frame events. Any such event triggers
83
// debug trap. The enter/leave frame events enabled or disabled across
84
// all functions.
85
86
void adjustEnterAndLeaveFrameTrapsState(JSContext* cx, bool enabled);
87
void ensureEnterFrameTrapsState(JSContext* cx, bool enabled);
88
bool enterFrameTrapsEnabled() const { return enterFrameTrapsEnabled_; }
89
90
// When the Code is debugEnabled, individual breakpoints can be enabled or
91
// disabled at instruction offsets.
92
93
bool hasBreakpointTrapAtOffset(uint32_t offset);
94
void toggleBreakpointTrap(JSRuntime* rt, uint32_t offset, bool enabled);
95
WasmBreakpointSite* getBreakpointSite(uint32_t offset) const;
96
WasmBreakpointSite* getOrCreateBreakpointSite(JSContext* cx,
97
Instance* instance,
98
uint32_t offset);
99
bool hasBreakpointSite(uint32_t offset);
100
void destroyBreakpointSite(JSFreeOp* fop, Instance* instance,
101
uint32_t offset);
102
void clearBreakpointsIn(JSFreeOp* fp, WasmInstanceObject* instance,
103
js::Debugger* dbg, JSObject* handler);
104
void clearAllBreakpoints(JSFreeOp* fp, WasmInstanceObject* instance);
105
106
// When the Code is debug-enabled, single-stepping mode can be toggled on
107
// the granularity of individual functions.
108
109
bool stepModeEnabled(uint32_t funcIndex) const;
110
MOZ_MUST_USE bool incrementStepperCount(JSContext* cx, uint32_t funcIndex);
111
void decrementStepperCount(JSFreeOp* fop, uint32_t funcIndex);
112
113
// Stack inspection helpers.
114
115
MOZ_MUST_USE bool debugGetLocalTypes(uint32_t funcIndex,
116
ValTypeVector* locals,
117
size_t* argsLength);
118
MOZ_MUST_USE bool debugGetResultTypes(uint32_t funcIndex,
119
ValTypeVector* results);
120
MOZ_MUST_USE bool getGlobal(Instance& instance, uint32_t globalIndex,
121
MutableHandleValue vp);
122
123
// Debug URL helpers.
124
125
MOZ_MUST_USE bool getSourceMappingURL(JSContext* cx,
126
MutableHandleString result) const;
127
128
// Accessors for commonly used elements of linked structures.
129
130
const MetadataTier& metadata(Tier t) const { return code_->metadata(t); }
131
const Metadata& metadata() const { return code_->metadata(); }
132
const CodeRangeVector& codeRanges(Tier t) const {
133
return metadata(t).codeRanges;
134
}
135
const CallSiteVector& callSites(Tier t) const {
136
return metadata(t).callSites;
137
}
138
139
uint32_t funcToCodeRangeIndex(uint32_t funcIndex) const {
140
return metadata(Tier::Debug).funcToCodeRange[funcIndex];
141
}
142
143
// about:memory reporting:
144
145
void addSizeOfMisc(MallocSizeOf mallocSizeOf, Metadata::SeenSet* seenMetadata,
146
Code::SeenSet* seenCode, size_t* code, size_t* data) const;
147
};
148
149
typedef UniquePtr<DebugState> UniqueDebugState;
150
151
} // namespace wasm
152
} // namespace js
153
154
#endif // wasm_debug_h