zend_vm_trace_map.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Dmitry Stogov <dmitry@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "zend_vm_handlers.h"
  19. #include "zend_sort.h"
  20. #define GEN_MAP(n, name) do { \
  21. ZVAL_LONG(&tmp, (zend_long)(uintptr_t)zend_opcode_handlers[n]); \
  22. zend_hash_str_add(&vm_trace_ht, #name, sizeof(#name) - 1, &tmp); \
  23. } while (0);
  24. #define VM_TRACE_START() do { \
  25. zval tmp; \
  26. zend_hash_init(&vm_trace_ht, 0, NULL, NULL, 1); \
  27. VM_HANDLERS(GEN_MAP) \
  28. zend_vm_trace_init(); \
  29. } while (0)
  30. #ifdef _WIN64
  31. # define ADDR_FMT "%016I64x"
  32. #elif SIZEOF_SIZE_T == 4
  33. # define ADDR_FMT "%08zx"
  34. #elif SIZEOF_SIZE_T == 8
  35. # define ADDR_FMT "%016zx"
  36. #else
  37. # error "Unknown SIZEOF_SIZE_T"
  38. #endif
  39. static HashTable vm_trace_ht;
  40. static int zend_vm_trace_compare(const Bucket *p1, const Bucket *p2)
  41. {
  42. if (Z_LVAL(p1->val) > Z_LVAL(p2->val)) {
  43. return 1;
  44. } else if (Z_LVAL(p1->val) < Z_LVAL(p2->val)) {
  45. return -1;
  46. } else {
  47. return 0;
  48. }
  49. }
  50. static void zend_vm_trace_init(void)
  51. {
  52. FILE *f;
  53. zend_string *key, *prev_key;
  54. zval *val;
  55. zend_long prev_addr;
  56. f = fopen("zend_vm.map", "w+");
  57. if (f) {
  58. zend_hash_sort(&vm_trace_ht, (bucket_compare_func_t)zend_vm_trace_compare, 0);
  59. prev_key = NULL;
  60. ZEND_HASH_FOREACH_STR_KEY_VAL(&vm_trace_ht, key, val) {
  61. if (prev_key) {
  62. fprintf(f, ADDR_FMT" "ADDR_FMT" t %s\n", prev_addr, Z_LVAL_P(val) - prev_addr, ZSTR_VAL(prev_key));
  63. }
  64. prev_key = key;
  65. prev_addr = Z_LVAL_P(val);
  66. } ZEND_HASH_FOREACH_END();
  67. if (prev_key) {
  68. fprintf(f, ADDR_FMT" "ADDR_FMT" t %s\n", prev_addr, 0, ZSTR_VAL(prev_key));
  69. }
  70. fclose(f);
  71. }
  72. zend_hash_destroy(&vm_trace_ht);
  73. }