backtrace.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. function test1() {
  3. test2();
  4. }
  5. function test2() {
  6. while(true) {
  7. co::sleep(10);
  8. echo __FUNCTION__." \n";
  9. }
  10. }
  11. $cid = go(function () {
  12. test1();
  13. });
  14. go(function () use ($cid) {
  15. while(true) {
  16. echo "BackTrace[$cid]:\n-----------------------------------------------\n";
  17. echo get_debug_print_backtrace(co::getBackTrace($cid))."\n";
  18. co::sleep(3);
  19. }
  20. });
  21. function get_debug_print_backtrace($traces){
  22. $ret = array();
  23. foreach($traces as $i => $call){
  24. $object = '';
  25. if (isset($call['class'])) {
  26. $object = $call['class'].$call['type'];
  27. if (is_array($call['args'])) {
  28. foreach ($call['args'] as &$arg) {
  29. get_arg($arg);
  30. }
  31. }
  32. }
  33. $ret[] = '#'.str_pad($i - $traces_to_ignore, 3, ' ')
  34. .$object.$call['function'].'('.implode(', ', $call['args'])
  35. .') called at ['.$call['file'].':'.$call['line'].']';
  36. }
  37. return implode("\n",$ret);
  38. }
  39. function get_arg(&$arg) {
  40. if (is_object($arg)) {
  41. $arr = (array)$arg;
  42. $args = array();
  43. foreach($arr as $key => $value) {
  44. if (strpos($key, chr(0)) !== false) {
  45. $key = ''; // Private variable found
  46. }
  47. $args[] = '['.$key.'] => '.get_arg($value);
  48. }
  49. $arg = get_class($arg) . ' Object ('.implode(',', $args).')';
  50. }
  51. }