zend_vm_trace_handlers.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_sort.h"
  19. #define VM_TRACE(op) zend_vm_trace(#op, sizeof(#op)-1);
  20. #define VM_TRACE_START() zend_vm_trace_init();
  21. #define VM_TRACE_END() zend_vm_trace_finish();
  22. static HashTable vm_trace_ht;
  23. static void zend_vm_trace(const char *op, size_t op_len)
  24. {
  25. static const char *last = NULL;
  26. static size_t last_len = 0;
  27. char buf[256];
  28. size_t len;
  29. zval tmp, *zv;
  30. if (EXPECTED(last)) {
  31. len = last_len + 1 + op_len;
  32. memcpy(buf, last, last_len);
  33. buf[last_len] = ' ';
  34. memcpy(buf + last_len + 1, op, op_len + 1);
  35. zv = zend_hash_str_find(&vm_trace_ht, buf, len);
  36. if (EXPECTED(zv)) {
  37. if (EXPECTED(Z_LVAL_P(zv) < ZEND_LONG_MAX)) {
  38. Z_LVAL_P(zv)++;
  39. }
  40. } else {
  41. ZVAL_LONG(&tmp, 1);
  42. zend_hash_str_add_new(&vm_trace_ht, buf, len, &tmp);
  43. }
  44. }
  45. last = op;
  46. last_len = op_len;
  47. }
  48. static int zend_vm_trace_compare(const Bucket *p1, const Bucket *p2)
  49. {
  50. if (Z_LVAL(p1->val) < Z_LVAL(p2->val)) {
  51. return 1;
  52. } else if (Z_LVAL(p1->val) > Z_LVAL(p2->val)) {
  53. return -1;
  54. } else {
  55. return 0;
  56. }
  57. }
  58. static void zend_vm_trace_finish(void)
  59. {
  60. zend_string *key;
  61. zval *val;
  62. FILE *f;
  63. f = fopen("zend_vm_trace.log", "w+");
  64. if (f) {
  65. zend_hash_sort(&vm_trace_ht, (compare_func_t)zend_vm_trace_compare, 0);
  66. ZEND_HASH_FOREACH_STR_KEY_VAL(&vm_trace_ht, key, val) {
  67. fprintf(f, "%s "ZEND_LONG_FMT"\n", ZSTR_VAL(key), Z_LVAL_P(val));
  68. } ZEND_HASH_FOREACH_END();
  69. fclose(f);
  70. }
  71. zend_hash_destroy(&vm_trace_ht);
  72. }
  73. static void zend_vm_trace_init(void)
  74. {
  75. FILE *f;
  76. zend_hash_init(&vm_trace_ht, 0, NULL, NULL, 1);
  77. f = fopen("zend_vm_trace.log", "r");
  78. if (f) {
  79. char buf[256];
  80. size_t len;
  81. zval tmp;
  82. while (!feof(f)) {
  83. if (fgets(buf, sizeof(buf)-1, f)) {
  84. len = strlen(buf);
  85. while (len > 0 && buf[len-1] <= ' ') {
  86. len--;
  87. buf[len] = 0;
  88. }
  89. while (len > 0 && buf[len-1] >= '0' && buf[len-1] <= '9') {
  90. len--;
  91. }
  92. if (len > 1) {
  93. buf[len-1] = 0;
  94. ZVAL_LONG(&tmp, ZEND_STRTOL(buf + len, NULL, 10));
  95. zend_hash_str_add(&vm_trace_ht, buf, len - 1, &tmp);
  96. }
  97. }
  98. }
  99. fclose(f);
  100. }
  101. }