trace-hmp-cmds.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * HMP commands related to tracing
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "monitor/hmp.h"
  26. #include "monitor/monitor.h"
  27. #include "qapi/error.h"
  28. #include "qapi/qapi-commands-trace.h"
  29. #include "qapi/qmp/qdict.h"
  30. #include "trace/control.h"
  31. #ifdef CONFIG_TRACE_SIMPLE
  32. #include "trace/simple.h"
  33. #endif
  34. void hmp_trace_event(Monitor *mon, const QDict *qdict)
  35. {
  36. const char *tp_name = qdict_get_str(qdict, "name");
  37. bool new_state = qdict_get_bool(qdict, "option");
  38. bool has_vcpu = qdict_haskey(qdict, "vcpu");
  39. int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
  40. Error *local_err = NULL;
  41. if (vcpu < 0) {
  42. monitor_printf(mon, "argument vcpu must be positive");
  43. return;
  44. }
  45. qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
  46. if (local_err) {
  47. error_report_err(local_err);
  48. }
  49. }
  50. #ifdef CONFIG_TRACE_SIMPLE
  51. void hmp_trace_file(Monitor *mon, const QDict *qdict)
  52. {
  53. const char *op = qdict_get_try_str(qdict, "op");
  54. const char *arg = qdict_get_try_str(qdict, "arg");
  55. if (!op) {
  56. st_print_trace_file_status();
  57. } else if (!strcmp(op, "on")) {
  58. st_set_trace_file_enabled(true);
  59. } else if (!strcmp(op, "off")) {
  60. st_set_trace_file_enabled(false);
  61. } else if (!strcmp(op, "flush")) {
  62. st_flush_trace_buffer();
  63. } else if (!strcmp(op, "set")) {
  64. if (arg) {
  65. st_set_trace_file(arg);
  66. }
  67. } else {
  68. monitor_printf(mon, "unexpected argument \"%s\"\n", op);
  69. hmp_help_cmd(mon, "trace-file");
  70. }
  71. }
  72. #endif
  73. void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
  74. {
  75. const char *name = qdict_get_try_str(qdict, "name");
  76. bool has_vcpu = qdict_haskey(qdict, "vcpu");
  77. int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
  78. TraceEventInfoList *events;
  79. TraceEventInfoList *elem;
  80. Error *local_err = NULL;
  81. if (name == NULL) {
  82. name = "*";
  83. }
  84. if (vcpu < 0) {
  85. monitor_printf(mon, "argument vcpu must be positive");
  86. return;
  87. }
  88. events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
  89. if (local_err) {
  90. error_report_err(local_err);
  91. return;
  92. }
  93. for (elem = events; elem != NULL; elem = elem->next) {
  94. monitor_printf(mon, "%s : state %u\n",
  95. elem->value->name,
  96. elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
  97. }
  98. qapi_free_TraceEventInfoList(events);
  99. }
  100. void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
  101. {
  102. size_t len;
  103. len = strlen(str);
  104. readline_set_completion_index(rs, len);
  105. if (nb_args == 2) {
  106. TraceEventIter iter;
  107. TraceEvent *ev;
  108. char *pattern = g_strdup_printf("%s*", str);
  109. trace_event_iter_init_pattern(&iter, pattern);
  110. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  111. readline_add_completion(rs, trace_event_get_name(ev));
  112. }
  113. g_free(pattern);
  114. }
  115. }
  116. void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
  117. {
  118. size_t len;
  119. len = strlen(str);
  120. readline_set_completion_index(rs, len);
  121. if (nb_args == 2) {
  122. TraceEventIter iter;
  123. TraceEvent *ev;
  124. char *pattern = g_strdup_printf("%s*", str);
  125. trace_event_iter_init_pattern(&iter, pattern);
  126. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  127. readline_add_completion(rs, trace_event_get_name(ev));
  128. }
  129. g_free(pattern);
  130. } else if (nb_args == 3) {
  131. readline_add_completion_of(rs, str, "on");
  132. readline_add_completion_of(rs, str, "off");
  133. }
  134. }