2
0

hmp-target.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * QEMU monitor, target-dependent part
  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-internal.h"
  26. #include "monitor/qdev.h"
  27. #include "net/slirp.h"
  28. #include "system/device_tree.h"
  29. #include "monitor/hmp-target.h"
  30. #include "monitor/hmp.h"
  31. #include "block/block-hmp-cmds.h"
  32. #include "qapi/qapi-commands-control.h"
  33. #include "qapi/qapi-commands-misc.h"
  34. #include "qapi/qapi-commands-machine.h"
  35. #include "qapi/error.h"
  36. #include "qemu/cutils.h"
  37. #if defined(TARGET_S390X)
  38. #include "hw/s390x/storage-keys.h"
  39. #include "hw/s390x/storage-attributes.h"
  40. #endif
  41. /* Make devices configuration available for use in hmp-commands*.hx templates */
  42. #include CONFIG_DEVICES
  43. static HMPCommand hmp_info_cmds[];
  44. /**
  45. * Is @name in the '|' separated list of names @list?
  46. */
  47. int hmp_compare_cmd(const char *name, const char *list)
  48. {
  49. const char *p, *pstart;
  50. int len;
  51. len = strlen(name);
  52. p = list;
  53. for (;;) {
  54. pstart = p;
  55. p = qemu_strchrnul(p, '|');
  56. if ((p - pstart) == len && !memcmp(pstart, name, len)) {
  57. return 1;
  58. }
  59. if (*p == '\0') {
  60. break;
  61. }
  62. p++;
  63. }
  64. return 0;
  65. }
  66. /* Please update hmp-commands.hx when adding or changing commands */
  67. static HMPCommand hmp_info_cmds[] = {
  68. #include "hmp-commands-info.h"
  69. { NULL, NULL, },
  70. };
  71. /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
  72. HMPCommand hmp_cmds[] = {
  73. #include "hmp-commands.h"
  74. { NULL, NULL, },
  75. };
  76. /*
  77. * Set @pval to the value in the register identified by @name.
  78. * return 0 if OK, -1 if not found
  79. */
  80. int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
  81. {
  82. const MonitorDef *md = target_monitor_defs();
  83. CPUState *cs = mon_get_cpu(mon);
  84. void *ptr;
  85. uint64_t tmp = 0;
  86. int ret;
  87. if (cs == NULL || md == NULL) {
  88. return -1;
  89. }
  90. for(; md->name != NULL; md++) {
  91. if (hmp_compare_cmd(name, md->name)) {
  92. if (md->get_value) {
  93. *pval = md->get_value(mon, md, md->offset);
  94. } else {
  95. CPUArchState *env = mon_get_cpu_env(mon);
  96. ptr = (uint8_t *)env + md->offset;
  97. switch(md->type) {
  98. case MD_I32:
  99. *pval = *(int32_t *)ptr;
  100. break;
  101. case MD_TLONG:
  102. *pval = *(target_long *)ptr;
  103. break;
  104. default:
  105. *pval = 0;
  106. break;
  107. }
  108. }
  109. return 0;
  110. }
  111. }
  112. ret = target_get_monitor_def(cs, name, &tmp);
  113. if (!ret) {
  114. *pval = (target_long) tmp;
  115. }
  116. return ret;
  117. }
  118. static int
  119. compare_mon_cmd(const void *a, const void *b)
  120. {
  121. return strcmp(((const HMPCommand *)a)->name,
  122. ((const HMPCommand *)b)->name);
  123. }
  124. static void __attribute__((__constructor__)) sortcmdlist(void)
  125. {
  126. qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
  127. sizeof(*hmp_cmds),
  128. compare_mon_cmd);
  129. qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
  130. sizeof(*hmp_info_cmds),
  131. compare_mon_cmd);
  132. }
  133. void monitor_register_hmp(const char *name, bool info,
  134. void (*cmd)(Monitor *mon, const QDict *qdict))
  135. {
  136. HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
  137. while (table->name != NULL) {
  138. if (strcmp(table->name, name) == 0) {
  139. g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
  140. table->cmd = cmd;
  141. return;
  142. }
  143. table++;
  144. }
  145. g_assert_not_reached();
  146. }
  147. void monitor_register_hmp_info_hrt(const char *name,
  148. HumanReadableText *(*handler)(Error **errp))
  149. {
  150. HMPCommand *table = hmp_info_cmds;
  151. while (table->name != NULL) {
  152. if (strcmp(table->name, name) == 0) {
  153. g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
  154. table->cmd_info_hrt = handler;
  155. return;
  156. }
  157. table++;
  158. }
  159. g_assert_not_reached();
  160. }