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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
/*
* debug routines for uftrace
*
* Copyright (C) 2014-2017, LG Electronics, Namhyung Kim <namhyung@gmail.com>
*
* Released under the GPL v2.
*/
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include "utils/utils.h"
#define TERM_COLOR_NORMAL ""
#define TERM_COLOR_RESET "\033[0m"
#define TERM_COLOR_BOLD "\033[1m"
#define TERM_COLOR_RED "\033[91m" /* bright red */
#define TERM_COLOR_GREEN "\033[32m"
#define TERM_COLOR_YELLOW "\033[33m"
#define TERM_COLOR_BLUE "\033[94m" /* bright blue */
#define TERM_COLOR_MAGENTA "\033[35m"
#define TERM_COLOR_CYAN "\033[36m"
#define TERM_COLOR_GRAY "\033[90m" /* bright black */
#define HTML_COLOR_NORMAL "<span>"
#define HTML_COLOR_RESET "</span>"
#define HTML_COLOR_BOLD "<span style='font-weight:bold'>"
#define HTML_COLOR_RED "<span style='color:red'>" /* bright red */
#define HTML_COLOR_GREEN "<span style='color:green'>"
#define HTML_COLOR_YELLOW "<span style='color:yellow'>"
#define HTML_COLOR_BLUE "<span style='color:blue'>" /* bright blue */
#define HTML_COLOR_MAGENTA "<span style='color:magenta'>"
#define HTML_COLOR_CYAN "<span style='color:cyan'>"
#define HTML_COLOR_GRAY "<span style='color:gray'>" /* bright black */
int debug;
FILE *logfp;
FILE *outfp;
enum color_setting log_color;
enum color_setting out_color;
enum format_mode format_mode;
int dbg_domain[DBG_DOMAIN_MAX];
/* colored output for argspec display */
const char *color_reset = TERM_COLOR_RESET;
const char *color_bold = TERM_COLOR_BOLD;
const char *color_string = TERM_COLOR_MAGENTA;
const char *color_symbol = TERM_COLOR_CYAN;
const char *color_struct = TERM_COLOR_CYAN;
const char *color_enum = TERM_COLOR_BLUE;
const char *color_enum_or = TERM_COLOR_RESET TERM_COLOR_BOLD "|" TERM_COLOR_RESET TERM_COLOR_BLUE;
static const struct color_code {
char code;
const char *color;
const char *html_color;
} colors[] = {
{ COLOR_CODE_NORMAL, TERM_COLOR_NORMAL, HTML_COLOR_NORMAL },
{ COLOR_CODE_RESET, TERM_COLOR_RESET, HTML_COLOR_RESET },
{ COLOR_CODE_RED, TERM_COLOR_RED, HTML_COLOR_RED },
{ COLOR_CODE_GREEN, TERM_COLOR_GREEN, HTML_COLOR_GREEN },
{ COLOR_CODE_BLUE, TERM_COLOR_BLUE, HTML_COLOR_BLUE },
{ COLOR_CODE_YELLOW, TERM_COLOR_YELLOW, HTML_COLOR_YELLOW },
{ COLOR_CODE_MAGENTA, TERM_COLOR_MAGENTA, HTML_COLOR_MAGENTA },
{ COLOR_CODE_CYAN, TERM_COLOR_CYAN, HTML_COLOR_CYAN },
{ COLOR_CODE_GRAY, TERM_COLOR_GRAY, HTML_COLOR_GRAY },
{ COLOR_CODE_BOLD, TERM_COLOR_BOLD, HTML_COLOR_BOLD },
};
static void color(const char *code, FILE *fp)
{
size_t len = strlen(code);
if ((fp == logfp && log_color == COLOR_OFF) || (fp == outfp && out_color == COLOR_OFF))
return;
if (fwrite(code, 1, len, fp) == len)
return; /* ok */
/* disable color */
log_color = COLOR_OFF;
out_color = COLOR_OFF;
len = sizeof(TERM_COLOR_RESET) - 1;
if (fwrite(TERM_COLOR_RESET, 1, len, fp) != len)
pr_dbg("resetting terminal color failed");
}
static bool check_busybox(const char *pager)
{
struct strv path_strv = STRV_INIT;
char buf[PATH_MAX];
char *path;
int i;
bool ret = false;
if (pager == NULL)
return false;
if (pager[0] == '/')
goto check;
/* search "PATH" env for absolute path */
strv_split(&path_strv, getenv("PATH"), ":");
strv_for_each(&path_strv, path, i) {
snprintf(buf, sizeof(buf), "%s/%s", path, pager);
if (!access(buf, X_OK)) {
pager = buf;
break;
}
}
strv_free(&path_strv);
check:
path = realpath(pager, NULL);
if (path) {
ret = !strncmp("busybox", uftrace_basename(path), 7);
free(path);
}
return ret;
}
void setup_color(enum color_setting color, char *pager)
{
if (likely(color == COLOR_AUTO)) {
char *term = getenv("TERM");
bool dumb = term && !strcmp(term, "dumb");
bool busybox = false;
out_color = COLOR_ON;
log_color = COLOR_ON;
if (pager) {
/* less in the busybox doesn't support color */
busybox = check_busybox(pager);
}
if (!isatty(fileno(outfp)) || dumb || busybox)
out_color = COLOR_OFF;
if (!isatty(fileno(logfp)) || dumb || busybox)
log_color = COLOR_OFF;
}
else {
log_color = color;
out_color = color;
}
if (format_mode == FORMAT_HTML) {
color_reset = HTML_COLOR_RESET;
color_bold = HTML_COLOR_BOLD;
color_string = HTML_COLOR_MAGENTA;
color_symbol = HTML_COLOR_CYAN;
color_struct = HTML_COLOR_CYAN;
color_enum = HTML_COLOR_BLUE;
color_enum_or = HTML_COLOR_RESET HTML_COLOR_BOLD
"|" HTML_COLOR_RESET HTML_COLOR_BLUE;
}
if (out_color != COLOR_ON) {
color_reset = "";
color_bold = "";
color_string = "";
color_symbol = "";
color_struct = "";
color_enum = "";
color_enum_or = "|";
}
}
static const char *get_color(char code)
{
unsigned i;
if (out_color != COLOR_ON)
return TERM_COLOR_NORMAL;
for (i = 0; i < ARRAY_SIZE(colors); i++) {
if (code == colors[i].code) {
if (format_mode == FORMAT_HTML)
return colors[i].html_color;
else
return colors[i].color;
}
}
return TERM_COLOR_NORMAL;
}
void __pr_dbg(const char *fmt, ...)
{
va_list ap;
color(TERM_COLOR_GRAY, logfp);
va_start(ap, fmt);
vfprintf(logfp, fmt, ap);
va_end(ap);
color(TERM_COLOR_RESET, logfp);
}
void __pr_err(const char *fmt, ...)
{
va_list ap;
color(TERM_COLOR_RED, logfp);
va_start(ap, fmt);
vfprintf(logfp, fmt, ap);
va_end(ap);
color(TERM_COLOR_RESET, logfp);
DTRAP();
exit(1);
}
void __pr_err_s(const char *fmt, ...)
{
va_list ap;
int saved_errno = errno;
char buf[512];
color(TERM_COLOR_RED, logfp);
va_start(ap, fmt);
vfprintf(logfp, fmt, ap);
va_end(ap);
fprintf(logfp, ": %s\n", uftrace_strerror(saved_errno, buf, sizeof(buf)));
color(TERM_COLOR_RESET, logfp);
DTRAP();
exit(1);
}
void __pr_warn(const char *fmt, ...)
{
va_list ap;
color(TERM_COLOR_YELLOW, logfp);
va_start(ap, fmt);
vfprintf(logfp, fmt, ap);
va_end(ap);
color(TERM_COLOR_RESET, logfp);
}
void __pr_out(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(outfp, fmt, ap);
va_end(ap);
}
void __pr_color(char code, const char *fmt, ...)
{
va_list ap;
const char *sc = get_color(code);
const char *ec = get_color(COLOR_CODE_RESET);
color(sc, outfp);
va_start(ap, fmt);
vfprintf(outfp, fmt, ap);
va_end(ap);
color(ec, outfp);
}
#ifndef LIBMCOUNT
static void __print_time_unit(int64_t delta_nsec, bool needs_sign)
{
uint64_t delta = llabs(delta_nsec);
uint64_t delta_small = 0;
char *units[] = {
"us", "ms", " s", " m", " h",
};
char *color_units[] = {
"us",
TERM_COLOR_GREEN "ms" TERM_COLOR_RESET,
TERM_COLOR_YELLOW " s" TERM_COLOR_RESET,
TERM_COLOR_RED " m" TERM_COLOR_RESET,
TERM_COLOR_RED " h" TERM_COLOR_RESET,
};
char *html_color_units[] = {
"us",
HTML_COLOR_GREEN "ms" HTML_COLOR_RESET,
HTML_COLOR_YELLOW " s" HTML_COLOR_RESET,
HTML_COLOR_RED " m" HTML_COLOR_RESET,
HTML_COLOR_RED " h" HTML_COLOR_RESET,
};
char *unit;
unsigned limit[] = {
1000, 1000, 1000, 60, 24, INT_MAX,
};
unsigned idx;
if (delta_nsec == 0UL) {
if (needs_sign)
pr_out(" ");
pr_out("%7s %2s", "", "");
return;
}
for (idx = 0; idx < ARRAY_SIZE(units); idx++) {
delta_small = delta % limit[idx];
delta = delta / limit[idx];
if (delta < limit[idx + 1])
break;
}
ASSERT(idx < ARRAY_SIZE(units));
/* for some error cases */
if (delta > 999)
delta = delta_small = 999;
if (out_color == COLOR_ON) {
if (format_mode == FORMAT_HTML)
unit = html_color_units[idx];
else
unit = color_units[idx];
}
else
unit = units[idx];
if (needs_sign) {
const char *signs[] = { "+", "-" };
const char *color_signs[] = {
TERM_COLOR_RED "+", TERM_COLOR_MAGENTA "+", TERM_COLOR_NORMAL "+",
TERM_COLOR_BLUE "-", TERM_COLOR_CYAN "-", TERM_COLOR_NORMAL "-",
};
const char *html_color_signs[] = {
HTML_COLOR_RED "+", HTML_COLOR_MAGENTA "+", HTML_COLOR_NORMAL "+",
HTML_COLOR_BLUE "-", HTML_COLOR_CYAN "-", HTML_COLOR_NORMAL "-",
};
int sign_idx = (delta_nsec > 0);
int indent = (delta >= 100) ? 0 : (delta >= 10) ? 1 : 2;
const char *sign = signs[sign_idx];
const char *ends = TERM_COLOR_NORMAL;
if (out_color == COLOR_ON) {
if (delta_nsec >= 100000)
sign_idx = 0;
else if (delta_nsec >= 5000)
sign_idx = 1;
else if (delta_nsec > 0)
sign_idx = 2;
else if (delta_nsec <= -100000)
sign_idx = 3;
else if (delta_nsec <= -5000)
sign_idx = 4;
else
sign_idx = 5;
if (format_mode == FORMAT_HTML) {
sign = html_color_signs[sign_idx];
ends = HTML_COLOR_RESET;
}
else {
sign = color_signs[sign_idx];
ends = TERM_COLOR_RESET;
}
}
pr_out("%*s%s%" PRId64 ".%03" PRIu64 "%s %s", indent, "", sign, delta, delta_small,
ends, unit);
}
else
pr_out("%3" PRIu64 ".%03" PRIu64 " %s", delta, delta_small, unit);
}
void print_time_unit(uint64_t delta_nsec)
{
__print_time_unit(delta_nsec, false);
}
void print_diff_percent(uint64_t base_nsec, uint64_t pair_nsec)
{
double percent;
const char *sc;
const char *ec = get_color(COLOR_CODE_RESET);
if (base_nsec == 0) {
sc = get_color(COLOR_CODE_RED);
pr_out("%s%7s%s ", sc, "N/A", ec);
return;
}
if (pair_nsec == 0) {
sc = get_color(COLOR_CODE_BLUE);
pr_out("%s%7s%s ", sc, "N/A", ec);
return;
}
percent = 100.0 * (int64_t)(pair_nsec - base_nsec) / base_nsec;
/* for some error cases */
if (percent > 999.99)
percent = 999.99;
else if (percent < -999.99)
percent = -999.99;
sc = percent > 30 ? get_color(COLOR_CODE_RED) :
percent > 3 ? get_color(COLOR_CODE_MAGENTA) :
percent < -30 ? get_color(COLOR_CODE_BLUE) :
percent < -3 ? get_color(COLOR_CODE_CYAN) :
get_color(COLOR_CODE_NORMAL);
pr_out("%s%+7.2f%s%%", sc, percent, ec);
}
void print_diff_time_unit(uint64_t base_nsec, uint64_t pair_nsec)
{
if (base_nsec == pair_nsec)
pr_out("%11s", "0 us");
else
__print_time_unit(pair_nsec - base_nsec, true);
}
void print_diff_count(uint64_t base, uint64_t pair)
{
char diff_colors[] = {
COLOR_CODE_RED,
COLOR_CODE_BLUE,
};
int sign_idx = (pair < base);
int64_t diff = pair - base;
const char *sc = get_color(diff_colors[sign_idx]);
const char *ec = get_color(COLOR_CODE_RESET);
if (diff != 0)
pr_out("%s%+9" PRId64 "%s", sc, diff, ec);
else
pr_out("%9s", "+0");
}
void print_diff_percent_point(double base, double pair)
{
double diff = pair - base;
const char *sc;
const char *ec = get_color(COLOR_CODE_RESET);
/* for some error cases */
if (diff > 999.99)
diff = 999.99;
else if (diff < -999.99)
diff = -999.99;
sc = diff > 30 ? get_color(COLOR_CODE_RED) :
diff > 3 ? get_color(COLOR_CODE_MAGENTA) :
diff < -30 ? get_color(COLOR_CODE_BLUE) :
diff < -3 ? get_color(COLOR_CODE_CYAN) :
get_color(COLOR_CODE_NORMAL);
pr_out("%s%+7.2f%%pt%s", sc, diff, ec);
}
#endif
|