debug.c++ 7.92 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
21

Kenton Varda's avatar
Kenton Varda committed
22
#include "debug.h"
23 24
#include <stdlib.h>
#include <ctype.h>
25 26
#include <string.h>
#include <errno.h>
27

28
namespace kj {
Kenton Varda's avatar
Kenton Varda committed
29
namespace _ {  // private
30

Kenton Varda's avatar
Kenton Varda committed
31
Debug::Severity Debug::minSeverity = Debug::Severity::WARNING;
32

Kenton Varda's avatar
Kenton Varda committed
33
ArrayPtr<const char> KJ_STRINGIFY(Debug::Severity severity) {
34 35 36 37
  static const char* SEVERITY_STRINGS[] = {
    "info",
    "warning",
    "error",
38 39
    "fatal",
    "debug"
40 41 42 43 44 45
  };

  const char* s = SEVERITY_STRINGS[static_cast<uint>(severity)];
  return arrayPtr(s, strlen(s));
}

46 47 48 49 50 51 52 53
namespace {

enum DescriptionStyle {
  LOG,
  ASSERTION,
  SYSCALL
};

Kenton Varda's avatar
Kenton Varda committed
54 55
static String makeDescription(DescriptionStyle style, const char* code, int errorNumber,
                              const char* macroArgs, ArrayPtr<String> argValues) {
56
  KJ_STACK_ARRAY(ArrayPtr<const char>, argNames, argValues.size(), 8, 64);
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

  if (argValues.size() > 0) {
    size_t index = 0;
    const char* start = macroArgs;
    while (isspace(*start)) ++start;
    const char* pos = start;
    uint depth = 0;
    bool quoted = false;
    while (char c = *pos++) {
      if (quoted) {
        if (c == '\\' && *pos != '\0') {
          ++pos;
        } else if (c == '\"') {
          quoted = false;
        }
      } else {
        if (c == '(') {
          ++depth;
        } else if (c == ')') {
          --depth;
        } else if (c == '\"') {
          quoted = true;
        } else if (c == ',' && depth == 0) {
          if (index < argValues.size()) {
            argNames[index] = arrayPtr(start, pos - 1);
          }
          ++index;
          while (isspace(*pos)) ++pos;
          start = pos;
        }
      }
    }
    if (index < argValues.size()) {
      argNames[index] = arrayPtr(start, pos - 1);
    }
    ++index;

    if (index != argValues.size()) {
95 96
      getExceptionCallback().logMessage(__FILE__, __LINE__, 0,
          str("Failed to parse logging macro args into ",
97 98 99 100
              argValues.size(), " names: ", macroArgs, '\n'));
    }
  }

101 102 103 104 105 106 107 108 109 110 111 112
  if (style == SYSCALL) {
    // Strip off leading "foo = " from code, since callers will sometimes write things like:
    //   ssize_t n;
    //   RECOVERABLE_SYSCALL(n = read(fd, buffer, sizeof(buffer))) { return ""; }
    //   return std::string(buffer, n);
    const char* equalsPos = strchr(code, '=');
    if (equalsPos != nullptr && equalsPos[1] != '=') {
      code = equalsPos + 1;
      while (isspace(*code)) ++code;
    }
  }

113 114 115 116
  if (style == ASSERTION && code == nullptr) {
    style = LOG;
  }

117
  {
Kenton Varda's avatar
Kenton Varda committed
118 119 120 121 122
    StringPtr expected = "expected ";
    StringPtr codeArray = style == LOG ? nullptr : StringPtr(code);
    StringPtr sep = " = ";
    StringPtr delim = "; ";
    StringPtr colon = ": ";
123

Kenton Varda's avatar
Kenton Varda committed
124
    StringPtr sysErrorArray;
125 126 127
#if __USE_GNU
    char buffer[256];
    if (style == SYSCALL) {
Kenton Varda's avatar
Kenton Varda committed
128
      sysErrorArray = strerror_r(errorNumber, buffer, sizeof(buffer));
129 130
    }
#else
Kenton Varda's avatar
Kenton Varda committed
131 132 133 134 135
    char buffer[256];
    if (style == SYSCALL) {
      strerror_r(errorNumber, buffer, sizeof(buffer));
      sysErrorArray = buffer;
    }
136
#endif
137 138

    size_t totalSize = 0;
139 140 141 142 143 144 145 146 147
    switch (style) {
      case LOG:
        break;
      case ASSERTION:
        totalSize += expected.size() + codeArray.size();
        break;
      case SYSCALL:
        totalSize += codeArray.size() + colon.size() + sysErrorArray.size();
        break;
148
    }
149

150
    for (size_t i = 0; i < argValues.size(); i++) {
151 152 153
      if (i > 0 || style != LOG) {
        totalSize += delim.size();
      }
154 155 156 157 158 159
      if (argNames[i].size() > 0 && argNames[i][0] != '\"') {
        totalSize += argNames[i].size() + sep.size();
      }
      totalSize += argValues[i].size();
    }

Kenton Varda's avatar
Kenton Varda committed
160 161
    String result = heapString(totalSize);
    char* pos = result.begin();
162

163 164 165 166
    switch (style) {
      case LOG:
        break;
      case ASSERTION:
167
        pos = _::fill(pos, expected, codeArray);
168 169
        break;
      case SYSCALL:
170
        pos = _::fill(pos, codeArray, colon, sysErrorArray);
171
        break;
172 173
    }
    for (size_t i = 0; i < argValues.size(); i++) {
174
      if (i > 0 || style != LOG) {
175
        pos = _::fill(pos, delim);
176
      }
177
      if (argNames[i].size() > 0 && argNames[i][0] != '\"') {
178
        pos = _::fill(pos, argNames[i], sep);
179
      }
180
      pos = _::fill(pos, argValues[i]);
181 182
    }

Kenton Varda's avatar
Kenton Varda committed
183
    return result;
184 185 186
  }
}

187 188
}  // namespace

Kenton Varda's avatar
Kenton Varda committed
189 190
void Debug::logInternal(const char* file, int line, Severity severity, const char* macroArgs,
                        ArrayPtr<String> argValues) {
191 192
  getExceptionCallback().logMessage(file, line, 0,
      str(severity, ": ", makeDescription(LOG, nullptr, 0, macroArgs, argValues), '\n'));
193 194
}

Kenton Varda's avatar
Kenton Varda committed
195
Debug::Fault::~Fault() noexcept(false) {
196 197 198
  if (exception != nullptr) {
    Exception copy = mv(*exception);
    delete exception;
199
    throwRecoverableException(mv(copy));
200
  }
201 202
}

Kenton Varda's avatar
Kenton Varda committed
203
void Debug::Fault::fatal() {
204 205 206
  Exception copy = mv(*exception);
  delete exception;
  exception = nullptr;
207
  throwFatalException(mv(copy));
208 209 210
  abort();
}

Kenton Varda's avatar
Kenton Varda committed
211
void Debug::Fault::init(
212 213 214 215 216
    const char* file, int line, Exception::Nature nature, int errorNumber,
    const char* condition, const char* macroArgs, ArrayPtr<String> argValues) {
  exception = new Exception(nature, Exception::Durability::PERMANENT, file, line,
      makeDescription(nature == Exception::Nature::OS_ERROR ? SYSCALL : ASSERTION,
                      condition, errorNumber, macroArgs, argValues));
217 218
}

Kenton Varda's avatar
Kenton Varda committed
219
String Debug::makeContextDescriptionInternal(const char* macroArgs, ArrayPtr<String> argValues) {
220
  return makeDescription(LOG, nullptr, 0, macroArgs, argValues);
221 222
}

223
int Debug::getOsErrorNumber(bool nonblocking) {
224
  int result = errno;
225 226 227 228 229 230

  // On many systems, EAGAIN and EWOULDBLOCK have the same value, but this is not strictly required
  // by POSIX, so we need to check both.
  return result == EINTR ? -1
       : nonblocking && (result == EAGAIN || result == EWOULDBLOCK) ? 0
       : result;
231 232
}

Kenton Varda's avatar
Kenton Varda committed
233
Debug::Context::Context(): logged(false) {}
234
Debug::Context::~Context() noexcept(false) {}
235

Kenton Varda's avatar
Kenton Varda committed
236
Debug::Context::Value Debug::Context::ensureInitialized() {
237 238 239 240 241 242 243 244 245
  KJ_IF_MAYBE(v, value) {
    return Value(v->file, v->line, heapString(v->description));
  } else {
    Value result = evaluate();
    value = Value(result.file, result.line, heapString(result.description));
    return result;
  }
}

Kenton Varda's avatar
Kenton Varda committed
246
void Debug::Context::onRecoverableException(Exception&& exception) {
247 248
  Value v = ensureInitialized();
  exception.wrapContext(v.file, v.line, mv(v.description));
Kenton Varda's avatar
Kenton Varda committed
249
  next.onRecoverableException(kj::mv(exception));
250
}
Kenton Varda's avatar
Kenton Varda committed
251
void Debug::Context::onFatalException(Exception&& exception) {
252 253
  Value v = ensureInitialized();
  exception.wrapContext(v.file, v.line, mv(v.description));
Kenton Varda's avatar
Kenton Varda committed
254
  next.onFatalException(kj::mv(exception));
255
}
Kenton Varda's avatar
Kenton Varda committed
256
void Debug::Context::logMessage(const char* file, int line, int contextDepth, String&& text) {
257 258 259 260 261 262 263
  if (!logged) {
    Value v = ensureInitialized();
    next.logMessage(v.file, v.line, 0, str("context: ", mv(v.description), '\n'));
    logged = true;
  }

  next.logMessage(file, line, contextDepth + 1, mv(text));
264 265
}

Kenton Varda's avatar
Kenton Varda committed
266
}  // namespace _ (private)
267
}  // namespace kj