Skip to content

Commit f346478

Browse files
authored
Add BYN_DEBUG/BYN_TRACE macros similar to LLVM's debug system (#2496)
This allows for debug trace message to be split my channel. So you can pass `--debug` to simply debug everything, or `--debug=opt` to only debug wasm-opt. This change is the initial introduction but as a followup I hope to convert all tracing over to this new system so we can more easily control the debug output.
1 parent 9cbe295 commit f346478

5 files changed

Lines changed: 127 additions & 19 deletions

File tree

src/support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ SET(support_SOURCES
33
bits.cpp
44
colors.cpp
55
command-line.cpp
6+
debug.cpp
67
file.cpp
78
path.cpp
89
safe_integer.cpp

src/support/command-line.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "support/command-line.h"
1818
#include "config.h"
19+
#include "support/debug.h"
1920

2021
using namespace wasm;
2122

@@ -91,8 +92,11 @@ Options::Options(const std::string& command, const std::string& description)
9192
add("--debug",
9293
"-d",
9394
"Print debug information to stderr",
94-
Arguments::Zero,
95-
[&](Options* o, const std::string& arguments) { debug = true; });
95+
Arguments::Optional,
96+
[&](Options* o, const std::string& arguments) {
97+
debug = true;
98+
setDebugEnabled(arguments.c_str());
99+
});
96100
}
97101

98102
Options::~Options() {}

src/support/debug.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2019 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "support/debug.h"
18+
19+
#include <cstring>
20+
#include <set>
21+
#include <string>
22+
23+
static bool debugEnabled = false;
24+
static std::set<std::string> debugTypesEnabled;
25+
26+
bool wasm::isDebugEnabled(const char* type) {
27+
if (!debugEnabled) {
28+
return false;
29+
}
30+
if (debugTypesEnabled.empty()) {
31+
return true;
32+
}
33+
return debugTypesEnabled.count(type) > 0;
34+
}
35+
36+
void wasm::setDebugEnabled(const char* types) {
37+
debugEnabled = true;
38+
// split types on comma and add each string to debugTypesEnabled
39+
size_t start = 0;
40+
size_t end = strlen(types);
41+
while (start < end) {
42+
const char* type_end = strchr(types + start, ',');
43+
if (type_end == nullptr) {
44+
type_end = types + end;
45+
}
46+
size_t type_size = type_end - types + start;
47+
std::string type(types + start, type_size);
48+
debugTypesEnabled.insert(type);
49+
start += type_size + 1;
50+
}
51+
}

src/support/debug.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2019 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Implements BYN_DEBUG macro similar to llvm's include/llvm/Support/Debug.h,
18+
// which can include any code, and in addition and printf-file BYN_TRACE.
19+
//
20+
// To use these macros you must define DEBUG_TYPE to a C string within your
21+
// source code which then acts as the name of a channel which can be
22+
// individually enabled via --debug=<chan>. Specifying --debug without any
23+
// argument enables all channels.
24+
25+
#ifndef wasm_support_debug_h
26+
#define wasm_support_debug_h
27+
28+
#ifndef NDEBUG
29+
30+
namespace wasm {
31+
bool isDebugEnabled(const char* type);
32+
void setDebugEnabled(const char* types);
33+
} // namespace wasm
34+
35+
#define BYN_DEBUG_WITH_TYPE(TYPE, X) \
36+
do { \
37+
if (::wasm::isDebugEnabled(TYPE)) { \
38+
X; \
39+
} \
40+
} while (false)
41+
42+
#define BYN_TRACE_WITH_TYPE(TYPE, MSG) \
43+
BYN_DEBUG_WITH_TYPE(TYPE, std::cerr << MSG);
44+
45+
#else
46+
47+
#define BYN_DEBUG_WITH_TYPE(...) \
48+
do { \
49+
} while (false)
50+
#define BYN_TRACE_WITH_TYPE(...) \
51+
do { \
52+
} while (false)
53+
#define isDebugEnabled() (false)
54+
#define setDebugEnabled()
55+
56+
#endif
57+
58+
#define BYN_DEBUG(X) BYN_DEBUG_WITH_TYPE(DEBUG_TYPE, X)
59+
#define BYN_TRACE(MSG) BYN_TRACE_WITH_TYPE(DEBUG_TYPE, MSG)
60+
61+
#endif // wasm_support_debug_h

src/tools/wasm-opt.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "shell-interface.h"
3030
#include "spec-wrapper.h"
3131
#include "support/command-line.h"
32+
#include "support/debug.h"
3233
#include "support/file.h"
3334
#include "wasm-binary.h"
3435
#include "wasm-interpreter.h"
@@ -37,6 +38,8 @@
3738
#include "wasm-s-parser.h"
3839
#include "wasm-validator.h"
3940

41+
#define DEBUG_TYPE "opt"
42+
4043
using namespace wasm;
4144

4245
// runs a command and returns its output TODO: portability, return code checking
@@ -203,9 +206,7 @@ int main(int argc, const char* argv[]) {
203206

204207
Module wasm;
205208

206-
if (options.debug) {
207-
std::cerr << "reading...\n";
208-
}
209+
BYN_TRACE("reading...\n");
209210

210211
if (!translateToFuzz) {
211212
ModuleReader reader;
@@ -282,10 +283,7 @@ int main(int argc, const char* argv[]) {
282283
std::string firstOutput;
283284

284285
if (extraFuzzCommand.size() > 0 && options.extra.count("output") > 0) {
285-
if (options.debug) {
286-
std::cerr << "writing binary before opts, for extra fuzz command..."
287-
<< std::endl;
288-
}
286+
BYN_TRACE("writing binary before opts, for extra fuzz command...\n");
289287
ModuleWriter writer;
290288
writer.setDebug(options.debug);
291289
writer.setBinary(emitBinary);
@@ -323,9 +321,7 @@ int main(int argc, const char* argv[]) {
323321
std::cerr << "warning: no passes specified, not doing any work\n";
324322
}
325323
} else {
326-
if (options.debug) {
327-
std::cerr << "running passes...\n";
328-
}
324+
BYN_TRACE("running passes...\n");
329325
auto runPasses = [&]() {
330326
options.runPasses(*curr);
331327
if (options.passOptions.validate) {
@@ -348,10 +344,7 @@ int main(int argc, const char* argv[]) {
348344
};
349345
auto lastSize = getSize();
350346
while (1) {
351-
if (options.debug) {
352-
std::cerr << "running iteration for convergence (" << lastSize
353-
<< ")...\n";
354-
}
347+
BYN_TRACE("running iteration for convergence (" << lastSize << ")..\n");
355348
runPasses();
356349
auto currSize = getSize();
357350
if (currSize >= lastSize) {
@@ -373,9 +366,7 @@ int main(int argc, const char* argv[]) {
373366
return 0;
374367
}
375368

376-
if (options.debug) {
377-
std::cerr << "writing..." << std::endl;
378-
}
369+
BYN_TRACE("writing...\n");
379370
ModuleWriter writer;
380371
writer.setDebug(options.debug);
381372
writer.setBinary(emitBinary);

0 commit comments

Comments
 (0)