log.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Logging support
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/log.h"
  21. #include "qemu/range.h"
  22. #include "qemu/error-report.h"
  23. #include "qapi/error.h"
  24. #include "qemu/cutils.h"
  25. #include "trace/control.h"
  26. #include "qemu/thread.h"
  27. #include "qemu/lockable.h"
  28. #include "qemu/rcu.h"
  29. #ifdef CONFIG_LINUX
  30. #include <sys/syscall.h>
  31. #endif
  32. typedef struct RCUCloseFILE {
  33. struct rcu_head rcu;
  34. FILE *fd;
  35. } RCUCloseFILE;
  36. /* Mutex covering the other global_* variables. */
  37. static QemuMutex global_mutex;
  38. static char *global_filename;
  39. static FILE *global_file;
  40. static __thread FILE *thread_file;
  41. static __thread Notifier qemu_log_thread_cleanup_notifier;
  42. int qemu_loglevel;
  43. static bool log_per_thread;
  44. static GArray *debug_regions;
  45. /* Returns true if qemu_log() will really write somewhere. */
  46. bool qemu_log_enabled(void)
  47. {
  48. return log_per_thread || qatomic_read(&global_file) != NULL;
  49. }
  50. /* Returns true if qemu_log() will write somewhere other than stderr. */
  51. bool qemu_log_separate(void)
  52. {
  53. if (log_per_thread) {
  54. return true;
  55. } else {
  56. FILE *logfile = qatomic_read(&global_file);
  57. return logfile && logfile != stderr;
  58. }
  59. }
  60. static int log_thread_id(void)
  61. {
  62. #ifdef CONFIG_GETTID
  63. return gettid();
  64. #elif defined(SYS_gettid)
  65. return syscall(SYS_gettid);
  66. #else
  67. static int counter;
  68. return qatomic_fetch_inc(&counter);
  69. #endif
  70. }
  71. static void qemu_log_thread_cleanup(Notifier *n, void *unused)
  72. {
  73. if (thread_file != stderr) {
  74. fclose(thread_file);
  75. thread_file = NULL;
  76. }
  77. }
  78. /* Lock/unlock output. */
  79. static FILE *qemu_log_trylock_with_err(Error **errp)
  80. {
  81. FILE *logfile;
  82. logfile = thread_file;
  83. if (!logfile) {
  84. if (log_per_thread) {
  85. g_autofree char *filename
  86. = g_strdup_printf(global_filename, log_thread_id());
  87. logfile = fopen(filename, "w");
  88. if (!logfile) {
  89. error_setg_errno(errp, errno,
  90. "Error opening logfile %s for thread %d",
  91. filename, log_thread_id());
  92. return NULL;
  93. }
  94. thread_file = logfile;
  95. qemu_log_thread_cleanup_notifier.notify = qemu_log_thread_cleanup;
  96. qemu_thread_atexit_add(&qemu_log_thread_cleanup_notifier);
  97. } else {
  98. rcu_read_lock();
  99. /*
  100. * FIXME: typeof_strip_qual, as used by qatomic_rcu_read,
  101. * does not work with pointers to undefined structures,
  102. * such as we have with struct _IO_FILE and musl libc.
  103. * Since all we want is a read of a pointer, cast to void**,
  104. * which does work with typeof_strip_qual.
  105. */
  106. logfile = qatomic_rcu_read((void **)&global_file);
  107. if (!logfile) {
  108. rcu_read_unlock();
  109. return NULL;
  110. }
  111. }
  112. }
  113. qemu_flockfile(logfile);
  114. return logfile;
  115. }
  116. FILE *qemu_log_trylock(void)
  117. {
  118. return qemu_log_trylock_with_err(NULL);
  119. }
  120. void qemu_log_unlock(FILE *logfile)
  121. {
  122. if (logfile) {
  123. fflush(logfile);
  124. qemu_funlockfile(logfile);
  125. if (!log_per_thread) {
  126. rcu_read_unlock();
  127. }
  128. }
  129. }
  130. void qemu_log(const char *fmt, ...)
  131. {
  132. FILE *f = qemu_log_trylock();
  133. if (f) {
  134. va_list ap;
  135. va_start(ap, fmt);
  136. vfprintf(f, fmt, ap);
  137. va_end(ap);
  138. qemu_log_unlock(f);
  139. }
  140. }
  141. static void __attribute__((__constructor__)) startup(void)
  142. {
  143. qemu_mutex_init(&global_mutex);
  144. }
  145. static void rcu_close_file(RCUCloseFILE *r)
  146. {
  147. fclose(r->fd);
  148. g_free(r);
  149. }
  150. /**
  151. * valid_filename_template:
  152. *
  153. * Validate the filename template. Require %d if per_thread, allow it
  154. * otherwise; require no other % within the template.
  155. */
  156. typedef enum {
  157. vft_error,
  158. vft_stderr,
  159. vft_strdup,
  160. vft_pid_printf,
  161. } ValidFilenameTemplateResult;
  162. static ValidFilenameTemplateResult
  163. valid_filename_template(const char *filename, bool per_thread, Error **errp)
  164. {
  165. if (filename) {
  166. char *pidstr = strstr(filename, "%");
  167. if (pidstr) {
  168. /* We only accept one %d, no other format strings */
  169. if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
  170. error_setg(errp, "Bad logfile template: %s", filename);
  171. return 0;
  172. }
  173. return per_thread ? vft_strdup : vft_pid_printf;
  174. }
  175. }
  176. if (per_thread) {
  177. error_setg(errp, "Filename template with '%%d' required for 'tid'");
  178. return vft_error;
  179. }
  180. return filename ? vft_strdup : vft_stderr;
  181. }
  182. /* enable or disable low levels log */
  183. static bool qemu_set_log_internal(const char *filename, bool changed_name,
  184. int log_flags, Error **errp)
  185. {
  186. bool need_to_open_file;
  187. bool daemonized;
  188. bool per_thread;
  189. FILE *logfile;
  190. QEMU_LOCK_GUARD(&global_mutex);
  191. logfile = global_file;
  192. /* The per-thread flag is immutable. */
  193. if (log_per_thread) {
  194. log_flags |= LOG_PER_THREAD;
  195. } else {
  196. if (global_filename) {
  197. log_flags &= ~LOG_PER_THREAD;
  198. }
  199. }
  200. per_thread = log_flags & LOG_PER_THREAD;
  201. if (changed_name) {
  202. char *newname = NULL;
  203. /*
  204. * Once threads start opening their own log files, we have no
  205. * easy mechanism to tell them all to close and re-open.
  206. * There seems little cause to do so either -- this option
  207. * will most often be used at user-only startup.
  208. */
  209. if (log_per_thread) {
  210. error_setg(errp, "Cannot change log filename after setting 'tid'");
  211. return false;
  212. }
  213. switch (valid_filename_template(filename, per_thread, errp)) {
  214. case vft_error:
  215. return false;
  216. case vft_stderr:
  217. break;
  218. case vft_strdup:
  219. newname = g_strdup(filename);
  220. break;
  221. case vft_pid_printf:
  222. newname = g_strdup_printf(filename, getpid());
  223. break;
  224. }
  225. g_free(global_filename);
  226. global_filename = newname;
  227. filename = newname;
  228. } else {
  229. filename = global_filename;
  230. if (per_thread &&
  231. valid_filename_template(filename, true, errp) == vft_error) {
  232. return false;
  233. }
  234. }
  235. /* Once the per-thread flag is set, it cannot be unset. */
  236. if (per_thread) {
  237. log_per_thread = true;
  238. }
  239. /* The flag itself is not relevant for need_to_open_file. */
  240. log_flags &= ~LOG_PER_THREAD;
  241. #ifdef CONFIG_TRACE_LOG
  242. log_flags |= LOG_TRACE;
  243. #endif
  244. qemu_loglevel = log_flags;
  245. daemonized = is_daemonized();
  246. need_to_open_file = false;
  247. if (!daemonized) {
  248. /*
  249. * If not daemonized we only log if qemu_loglevel is set, either to
  250. * stderr or to a file (if there is a filename).
  251. * If per-thread, open the file for each thread in qemu_log_trylock().
  252. */
  253. need_to_open_file = qemu_loglevel && !log_per_thread;
  254. } else {
  255. /*
  256. * If we are daemonized, we will only log if there is a filename.
  257. */
  258. need_to_open_file = filename != NULL;
  259. }
  260. if (logfile) {
  261. fflush(logfile);
  262. if (changed_name && logfile != stderr) {
  263. RCUCloseFILE *r = g_new0(RCUCloseFILE, 1);
  264. r->fd = logfile;
  265. qatomic_rcu_set(&global_file, NULL);
  266. call_rcu(r, rcu_close_file, rcu);
  267. logfile = NULL;
  268. }
  269. }
  270. if (log_per_thread && daemonized) {
  271. logfile = thread_file;
  272. }
  273. if (!logfile && need_to_open_file) {
  274. if (filename) {
  275. if (log_per_thread) {
  276. logfile = qemu_log_trylock_with_err(errp);
  277. if (!logfile) {
  278. return false;
  279. }
  280. qemu_log_unlock(logfile);
  281. } else {
  282. logfile = fopen(filename, "w");
  283. if (!logfile) {
  284. error_setg_errno(errp, errno, "Error opening logfile %s",
  285. filename);
  286. return false;
  287. }
  288. }
  289. /* In case we are a daemon redirect stderr to logfile */
  290. if (daemonized) {
  291. dup2(fileno(logfile), STDERR_FILENO);
  292. fclose(logfile);
  293. /*
  294. * This will skip closing logfile in rcu_close_file()
  295. * or qemu_log_thread_cleanup().
  296. */
  297. logfile = stderr;
  298. }
  299. } else {
  300. /* Default to stderr if no log file specified */
  301. assert(!daemonized);
  302. logfile = stderr;
  303. }
  304. if (log_per_thread && daemonized) {
  305. thread_file = logfile;
  306. } else {
  307. qatomic_rcu_set(&global_file, logfile);
  308. }
  309. }
  310. return true;
  311. }
  312. bool qemu_set_log(int log_flags, Error **errp)
  313. {
  314. return qemu_set_log_internal(NULL, false, log_flags, errp);
  315. }
  316. bool qemu_set_log_filename(const char *filename, Error **errp)
  317. {
  318. return qemu_set_log_internal(filename, true, qemu_loglevel, errp);
  319. }
  320. bool qemu_set_log_filename_flags(const char *name, int flags, Error **errp)
  321. {
  322. return qemu_set_log_internal(name, true, flags, errp);
  323. }
  324. /* Returns true if addr is in our debug filter or no filter defined
  325. */
  326. bool qemu_log_in_addr_range(uint64_t addr)
  327. {
  328. if (debug_regions) {
  329. int i = 0;
  330. for (i = 0; i < debug_regions->len; i++) {
  331. Range *range = &g_array_index(debug_regions, Range, i);
  332. if (range_contains(range, addr)) {
  333. return true;
  334. }
  335. }
  336. return false;
  337. } else {
  338. return true;
  339. }
  340. }
  341. void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp)
  342. {
  343. gchar **ranges = g_strsplit(filter_spec, ",", 0);
  344. int i;
  345. if (debug_regions) {
  346. g_array_unref(debug_regions);
  347. debug_regions = NULL;
  348. }
  349. debug_regions = g_array_sized_new(FALSE, FALSE,
  350. sizeof(Range), g_strv_length(ranges));
  351. for (i = 0; ranges[i]; i++) {
  352. const char *r = ranges[i];
  353. const char *range_op, *r2, *e;
  354. uint64_t r1val, r2val, lob, upb;
  355. struct Range range;
  356. range_op = strstr(r, "-");
  357. r2 = range_op ? range_op + 1 : NULL;
  358. if (!range_op) {
  359. range_op = strstr(r, "+");
  360. r2 = range_op ? range_op + 1 : NULL;
  361. }
  362. if (!range_op) {
  363. range_op = strstr(r, "..");
  364. r2 = range_op ? range_op + 2 : NULL;
  365. }
  366. if (!range_op) {
  367. error_setg(errp, "Bad range specifier");
  368. goto out;
  369. }
  370. if (qemu_strtou64(r, &e, 0, &r1val)
  371. || e != range_op) {
  372. error_setg(errp, "Invalid number to the left of %.*s",
  373. (int)(r2 - range_op), range_op);
  374. goto out;
  375. }
  376. if (qemu_strtou64(r2, NULL, 0, &r2val)) {
  377. error_setg(errp, "Invalid number to the right of %.*s",
  378. (int)(r2 - range_op), range_op);
  379. goto out;
  380. }
  381. switch (*range_op) {
  382. case '+':
  383. lob = r1val;
  384. upb = r1val + r2val - 1;
  385. break;
  386. case '-':
  387. upb = r1val;
  388. lob = r1val - (r2val - 1);
  389. break;
  390. case '.':
  391. lob = r1val;
  392. upb = r2val;
  393. break;
  394. default:
  395. g_assert_not_reached();
  396. }
  397. if (lob > upb) {
  398. error_setg(errp, "Invalid range");
  399. goto out;
  400. }
  401. range_set_bounds(&range, lob, upb);
  402. g_array_append_val(debug_regions, range);
  403. }
  404. out:
  405. g_strfreev(ranges);
  406. }
  407. const QEMULogItem qemu_log_items[] = {
  408. { CPU_LOG_TB_OUT_ASM, "out_asm",
  409. "show generated host assembly code for each compiled TB" },
  410. { CPU_LOG_TB_IN_ASM, "in_asm",
  411. "show target assembly code for each compiled TB" },
  412. { CPU_LOG_TB_OP, "op",
  413. "show micro ops for each compiled TB" },
  414. { CPU_LOG_TB_OP_OPT, "op_opt",
  415. "show micro ops after optimization" },
  416. { CPU_LOG_TB_OP_IND, "op_ind",
  417. "show micro ops before indirect lowering" },
  418. { CPU_LOG_INT, "int",
  419. "show interrupts/exceptions in short format" },
  420. { CPU_LOG_EXEC, "exec",
  421. "show trace before each executed TB (lots of logs)" },
  422. { CPU_LOG_TB_CPU, "cpu",
  423. "show CPU registers before entering a TB (lots of logs)" },
  424. { CPU_LOG_TB_FPU, "fpu",
  425. "include FPU registers in the 'cpu' logging" },
  426. { CPU_LOG_MMU, "mmu",
  427. "log MMU-related activities" },
  428. { CPU_LOG_PCALL, "pcall",
  429. "x86 only: show protected mode far calls/returns/exceptions" },
  430. { CPU_LOG_RESET, "cpu_reset",
  431. "show CPU state before CPU resets" },
  432. { LOG_UNIMP, "unimp",
  433. "log unimplemented functionality" },
  434. { LOG_GUEST_ERROR, "guest_errors",
  435. "log when the guest OS does something invalid (eg accessing a\n"
  436. "non-existent register)" },
  437. { CPU_LOG_PAGE, "page",
  438. "dump pages at beginning of user mode emulation" },
  439. { CPU_LOG_TB_NOCHAIN, "nochain",
  440. "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
  441. "complete traces" },
  442. #ifdef CONFIG_PLUGIN
  443. { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins"},
  444. #endif
  445. { LOG_STRACE, "strace",
  446. "log every user-mode syscall, its input, and its result" },
  447. { LOG_PER_THREAD, "tid",
  448. "open a separate log file per thread; filename must contain '%d'" },
  449. { 0, NULL, NULL },
  450. };
  451. /* takes a comma separated list of log masks. Return 0 if error. */
  452. int qemu_str_to_log_mask(const char *str)
  453. {
  454. const QEMULogItem *item;
  455. int mask = 0;
  456. char **parts = g_strsplit(str, ",", 0);
  457. char **tmp;
  458. for (tmp = parts; tmp && *tmp; tmp++) {
  459. if (g_str_equal(*tmp, "all")) {
  460. for (item = qemu_log_items; item->mask != 0; item++) {
  461. mask |= item->mask;
  462. }
  463. #ifdef CONFIG_TRACE_LOG
  464. } else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') {
  465. trace_enable_events((*tmp) + 6);
  466. mask |= LOG_TRACE;
  467. #endif
  468. } else {
  469. for (item = qemu_log_items; item->mask != 0; item++) {
  470. if (g_str_equal(*tmp, item->name)) {
  471. goto found;
  472. }
  473. }
  474. goto error;
  475. found:
  476. mask |= item->mask;
  477. }
  478. }
  479. g_strfreev(parts);
  480. return mask;
  481. error:
  482. g_strfreev(parts);
  483. return 0;
  484. }
  485. void qemu_print_log_usage(FILE *f)
  486. {
  487. const QEMULogItem *item;
  488. fprintf(f, "Log items (comma separated):\n");
  489. for (item = qemu_log_items; item->mask != 0; item++) {
  490. fprintf(f, "%-15s %s\n", item->name, item->help);
  491. }
  492. #ifdef CONFIG_TRACE_LOG
  493. fprintf(f, "trace:PATTERN enable trace events\n");
  494. fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
  495. #endif
  496. }