/** * If an XDebug trace is running, this stops the trace. * * @param bool $showAsDebug If true, outputs the trace file as debug information (assuming displayDebug is used) * * @return void */ public static function end_trace($showAsDebug = false) { if (function_exists('xdebug_stop_trace')) { $file = xdebug_get_tracefile_name(); xdebug_stop_trace(); if ($showAsDebug === true) { $trace = file_get_contents($file); self::debug($trace); } } }
/** * Returns the name of the file which is used to trace. * * @throws \RuntimeException * @return string */ public function start() { if (!static::$file) { if (xdebug_get_tracefile_name()) { throw new \RuntimeException('Can not start tracing, it has already been started.'); } $file = tempnam(sys_get_temp_dir(), 'telltale'); xdebug_start_trace($file, \XDEBUG_TRACE_COMPUTERIZED); static::$file = $file . '.xt'; } return static::$file; }
public static function view($removefieldset = false) { if (!self::$_started) { return; } self::$timeEnd = self::getmicrotime(); $time = sprintf('%.5f', self::$timeEnd - self::$timeStart); $files = sprintf('%.5f', self::$filesTime); $rapportSQL = sprintf('%.2f', 100 * self::$totalTime / $time); $rapportPHP = 100 - $rapportSQL; $memoryPeak = round(memory_get_peak_usage() / 1048576, 3); $content = 'File ' . $_SERVER['SCRIPT_NAME'] . "\n" . 'Loaded in ' . $time . ' seconds' . "\n" . 'Loaded PHP files : ' . self::$filesLoaded . "\n" . 'SQL requests : ' . sprintf('%.5f', self::$totalTime) . ' seconds (' . self::$sqlNbRequests . ' requests)' . "\n" . '% SQL/PHP : ' . $rapportSQL . ' / ' . $rapportPHP . ' %' . "\n" . 'Memory Peak : ' . $memoryPeak . 'Mo' . "\n"; if (function_exists('xdebug_get_profiler_filename') && xdebug_get_profiler_filename()) { $content .= 'XDebug Profile : ' . xdebug_get_profiler_filename() . "\n"; } if (function_exists('xdebug_get_profiler_filename') && xdebug_get_tracefile_name()) { $content .= 'XDebug Trace : ' . xdebug_get_tracefile_name() . "\n"; } $content .= 'User : '******' (' . CMS_session::getUserId() . ')' : 'none') . "\n"; $content .= 'Session Id ' . Zend_Session::getId() . "\n"; //$content .= 'Current page '.CMS_session::getPageID()."\n"; if (VIEW_SQL && $_SERVER["SCRIPT_NAME"] != PATH_ADMIN_WR . '/stat.php') { $stat = array('stat_time_start' => self::$timeStart, 'stat_time_end' => self::$timeEnd, 'stat_total_time' => self::$totalTime, 'stat_sql_nb_requests' => self::$sqlNbRequests, 'stat_sql_table' => self::$sqlTable, 'stat_content_name' => basename($_SERVER["SCRIPT_NAME"]), 'stat_files_table' => self::$filesTable, 'stat_memory_table' => self::$memoryTable, 'stat_memory_peak' => $memoryPeak, 'stat_files_loaded' => self::$filesLoaded); $statName = 'stat_' . md5(rand()); //save stats to cache (for 10 min) $cache = new CMS_cache($statName, 'atm-stats', 600, false); if ($cache) { $cache->save($stat); } } $content = !$removefieldset ? '<fieldset style="width:200px;" class="atm-debug"><legend>Debug Statistics</legend><pre>' . $content . '</pre>' : 'Debug Statistics :' . "\n" . $content; if (isset($statName)) { $content .= '<a href="' . PATH_ADMIN_WR . '/stat.php?stat=' . $statName . '" target="_blank">View statistics detail</a>'; } //end xhprof profiling if (defined('APPLICATION_ENABLE_PROFILING') && APPLICATION_ENABLE_PROFILING && function_exists('xhprof_disable')) { $xhprof_data = xhprof_disable(); include_once APPLICATION_XHPROF_ROOT_FS . "/xhprof_lib/utils/xhprof_lib.php"; include_once APPLICATION_XHPROF_ROOT_FS . "/xhprof_lib/utils/xhprof_runs.php"; $xhprof_runs = new XHProfRuns_Default(); $profileName = md5($_SERVER['REQUEST_URI']); $run_id = $xhprof_runs->save_run($xhprof_data, md5($_SERVER['REQUEST_URI'])); $content .= '<br /><a href="' . APPLICATION_XHPROF_URI . 'xhprof_html/index.php?run=' . $run_id . '&source=' . $profileName . '" target="_blank">View profiling detail</a>'; } $content .= !$removefieldset ? '</fieldset>' : ''; return $content; }
/** * Stop xdebug trace. Call startXDTrace() first. * @param string $file (default = STDOUT) */ public function stopXDTrace($file = 'php://STDOUT') { $trace_file = xdebug_get_tracefile_name(); xdebug_stop_trace(); if (!$trace_file || !$file) { return; } $fh = fopen($trace_file, 'r'); fgets($fh); // ignore first line $trace_buildin = array('microtime()', 'memory_get_usage()', 'xdebug_call_class()', 'xdebug_call_function()', 'xdebug_call_line()', 'xdebug_call_file()', 'xdebug_stop_trace()'); $trace_func = array('rkphplib\\Profiler->log()'); $buildin_list = array('time', 'in_array', 'is_object', 'is_array', 'is_null', 'array_push', 'join', 'preg_match', 'array_keys', 'array_shift', 'array_pop', 'array_push', 'strpos', 'preg_split', 'count', 'substr', 'mb_substr', 'basename', 'str_replace', 'mysqli->real_escape_string', 'mysqli->mysqli', 'mysqli->real_query', 'mysqli->set_charset', 'mysqli->query', 'mysqli_result->fetch_assoc', 'mysqli_result->close', 'mysqli->prepare', 'mysqli_stmt->bind_param', 'mysqli_stmt->execute', 'mysqli_stmt->close', 'ReflectionClass->__construct', 'ReflectionClass->getMethod', 'ReflectionMethod->invokeArgs'); $buildin = array(); foreach ($buildin_list as $func) { $buildin[$func . '()'] = 0; } $custom = array(); $last = array(0, 0, '', ''); while ($line = fgets($fh)) { $col = preg_split("/ +/", trim($line)); if (empty($col[4])) { continue; } if (in_array($col[3], $trace_buildin)) { $last = $col; continue; } if (in_array($col[3], $trace_func)) { // ToDo ... ignore following continue; } if (isset($buildin[$col[3]])) { $buildin[$col[3]]++; } else { if (!isset($custom[$col[3]])) { $custom[$col[3]] = array('call' => 0, 'time' => 0, 'mem' => 0); } $custom[$col[3]]['call']++; // ToDo ... collect following $custom[$col[3]]['time'] = 0; $custom[$col[3]]['mem'] = 0; } $last = $col; } fclose($fh); $fh = fopen($file, 'a'); fwrite($fh, "\nBuildIn Functions:\n"); foreach ($buildin as $func => $call) { if ($call > 10) { fprintf($fh, "%10s%50s\n", $call . 'x ', $func); } } fwrite($fh, "\nCustom Functions:\n"); foreach ($custom as $func => $info) { if ($info['call'] > 0) { $c = $info['call']; fprintf($fh, "%10s%50s%16s%16s\n", $c . 'x ', $func, round($info['time'] / $c, 4) . ' s', round($info['mem'] / $c, 0) . ' b'); } } fclose($fh); }
throw new Exception('Directory [' . $_ENV['tracer']['cache_path'] . '] does not exist and it cannot be created.'); } if (!is_writeable($_ENV['tracer']['cache_path']) && !chmod($_ENV['tracer']['class_cache'], 0777)) { throw new Exception('Directory [' . $_ENV['tracer']['cache_path'] . '] is not writeable and cannot be modified.'); } // Update the correct paths for caching and classes. $_ENV['tracer']['class_cache'] = $_ENV['tracer']['cache_path'] . DS . $_ENV['tracer']['class_cache']; $_ENV['tracer']['trace_cache'] = $_ENV['tracer']['cache_path'] . DS . $_ENV['tracer']['trace_cache']; $_ENV['tracer']['library_dir'] = SITEROOT . DS . 'lib'; // Get a list of and add the current trace files for Tracer $tracer_traces = array(); if (file_exists($_ENV['tracer']['trace_cache'])) { $tracer_traces = file_get_contents($_ENV['tracer']['trace_cache']); $tracer_traces = explode("\r\n", $tracer_traces); } $tracer_traces[] = $current_trace = xdebug_get_tracefile_name(); file_put_contents($_ENV['tracer']['trace_cache'], $current_trace . "\r\n", FILE_APPEND); $_ENV['tracer']['tracer_traces'] = $tracer_traces; // LET'S GO!!! // Register spl_autoload functionality spl_autoload_register('autoload'); // Autoload function autoload($class_name) { static $class_list; if ($class_list === null) { $f = $_ENV['tracer']['class_cache']; file_exists($f) && (include $f); unset($f); } if (!isset($class_list[$class_name]) || !file_exists($class_list[$class_name])) {
<?php require 'tracing_basic.inc'; // Default var_dump(xdebug_start_trace()); var_dump(xdebug_get_tracefile_name()); var_dump(xdebug_stop_trace()); // Naked var_dump(xdebug_start_trace(null, XDEBUG_TRACE_NAKED_FILENAME)); var_dump(xdebug_stop_trace()); // Non default var_dump(xdebug_start_trace("/tmp/trace")); var_dump(xdebug_stop_trace()); // crc32 var_dump(xdebug_start_trace("/tmp/trace-%c")); var_dump(xdebug_get_tracefile_name()); var_dump(xdebug_stop_trace()); // pid var_dump(xdebug_start_trace("/tmp/trace-%p")); var_dump(xdebug_stop_trace()); // random var_dump(xdebug_start_trace("/tmp/trace-%r")); var_dump(xdebug_stop_trace()); // script name var_dump(xdebug_start_trace("/tmp/trace-%s")); var_dump(xdebug_stop_trace()); // timestamp (sec) var_dump(xdebug_start_trace("/tmp/trace-%t")); var_dump(xdebug_stop_trace()); // timestamp (microseconds) var_dump(xdebug_start_trace("/tmp/trace-%u"));
/** * Returns the name of the file which is used to trace the output of this script too. * This is useful when xdebug.auto_trace is enabled. * @return string */ public function getTracefileName() { return xdebug_get_tracefile_name(); }