Skip to content

Commit

Permalink
trace: adjust displayed file & line for "eval'd code". unserialize lo…
Browse files Browse the repository at this point in the history
…g... handle legacy object abstraction
  • Loading branch information
bkdotcom committed Oct 3, 2023
1 parent b227541 commit 3e87e23
Show file tree
Hide file tree
Showing 22 changed files with 187 additions and 111 deletions.
19 changes: 13 additions & 6 deletions src/Backtrace/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ private static function callerInfoBuild(array $backtrace)
$return['classCalled'] = $return['class'];
}
if (isset($backtrace[$iFileLine])) {
$return['file'] = $backtrace[$iFileLine]['file'];
$return['line'] = $backtrace[$iFileLine]['line'];
$fileLineVals = \array_intersect_key($backtrace[$iFileLine], \array_flip(array(
'evalLine',
'file',
'line',
)));
$return = \array_merge($return, $fileLineVals);
}
if ($return['type'] === '->') {
$return['classContext'] = \get_class($backtrace[$iFunc]['object']);
Expand Down Expand Up @@ -230,12 +234,15 @@ private static function getExceptionTrace($exception)
if ($exception instanceof ParseError) {
return array();
}
$backtrace = $exception->getTrace();
\array_unshift($backtrace, array(
$trace = $exception->getTrace();
$fileLine = array(
'file' => $exception->getFile(),
'line' => $exception->getLine(),
));
return $backtrace;
);
if (\array_intersect_assoc($fileLine, \reset($trace)) !== $fileLine) {
\array_unshift($trace, $fileLine);
}
return $trace;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Backtrace/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ private static function findEvalCode(array $backtrace, $index)
$backtrace = \array_slice($backtrace, $index);
$lines = false;
foreach ($backtrace as $frame) {
if (!isset($frame['function'])) {
break;
}
if ($frame['function'] !== 'eval') {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Backtrace/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static function normalizeFrameFile(array $frame, array &$frameNext)
\array_intersect_key($frameNext, \array_flip(array('file', 'line')))
);
}
if (\preg_match($regexEvaldCode, $frame['file'], $matches)) {
if (\preg_match($regexEvaldCode, (string) $frame['file'], $matches)) {
// reported line = line within eval
// line inside paren is the line `eval` is on
$frame['evalLine'] = $frame['line'];
Expand Down
4 changes: 3 additions & 1 deletion src/Debug/Abstraction/Object/Abstraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public function __toString()
*/
public function __unserialize($data)
{
$this->definition = $data['classDefinition'];
$this->definition = isset($data['classDefinition'])
? $data['classDefinition']
: new ValueStore();
unset($data['classDefinition']);
$this->values = $data;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Dump/Html/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function markupType($type, array $attribs = array())
*/
public function tableAddContextRow($html, array $row, array $rowInfo, $index)
{
if ($rowInfo['context'] === Abstracter::UNDEFINED) {
if (empty($rowInfo['context']) || $rowInfo['context'] === Abstracter::UNDEFINED) {
return $html;
}
$html = \str_replace('<tr>', '<tr' . ($index === 0 ? ' class="expanded"' : '') . ' data-toggle="next">', $html);
Expand Down
8 changes: 4 additions & 4 deletions src/Debug/Dump/Html/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ protected function buildBody($rows)
foreach ($rows as $k => $row) {
$rowInfo = \array_merge(
array(
'attribs' => array(),
'class' => null,
'key' => null,
'summary' => null,
Expand All @@ -89,8 +90,7 @@ protected function buildBody($rows)
? $this->options['tableInfo']['rows'][$k]
: array()
);
$html = $this->buildRow($row, $rowInfo, $k);
$tBody .= $html;
$tBody .= $this->buildRow($row, $rowInfo, $k);
}
$tBody = \str_replace(' title=""', '', $tBody);
return '<tbody>' . "\n" . $tBody . '</tbody>' . "\n";
Expand Down Expand Up @@ -192,12 +192,12 @@ protected function buildHeader()
*
* @return string
*/
protected function buildRow($row, $rowInfo, $rowKey)
protected function buildRow($row, array $rowInfo, $rowKey)
{
$str = '';
$rowKey = $rowInfo['key'] ?: $rowKey;
$rowKeyParsed = $this->debug->html->parseTag($this->dumper->valDumper->dump($rowKey));
$str .= '<tr>';
$str .= '<tr' . $this->debug->html->buildAttribString($rowInfo['attribs']) . '>';
/*
Output key
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Plugin/Method/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ protected function doError($method, $args)
$callerInfo = $this->debug->backtrace->getCallerInfo();
$logEntry->setMeta(array(
'file' => $callerInfo['file'],
'line' => $callerInfo['line'],
'line' => $callerInfo['evalLine'] ?: $callerInfo['line'],
));
}
$this->appendLog($logEntry);
Expand Down
103 changes: 74 additions & 29 deletions src/Debug/Plugin/Method/Trace.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,15 @@ public function trace($inclContext = false, $caption = 'trace')
$this->debug,
__FUNCTION__,
\func_get_args(),
array(
'columns' => array('file','line','function'),
'detectFiles' => true,
'inclArgs' => null, // incl arguments with context?
// will default to $inclContext
// may want to set meta['cfg']['objectsExclude'] = '*'
'sortable' => false,
'trace' => null, // set to specify trace
),
array(),
$this->debug->rootInstance->getMethodDefaultArgs(__METHOD__),
array(
'caption',
'inclContext',
)
);
if ($logEntry->getMeta('inclArgs') === null) {
$logEntry->setMeta('inclArgs', $logEntry->getMeta('inclContext'));
}
$this->doTrace($logEntry);
$this->debug->log($logEntry);
return $this->debug;
}

Expand All @@ -88,27 +78,82 @@ public function trace($inclContext = false, $caption = 'trace')
*/
public function doTrace(LogEntry $logEntry)
{
$caption = $logEntry->getMeta('caption');
if (\is_string($caption) === false) {
$this->debug = $logEntry->getSubject();
$meta = $this->getMeta($logEntry);
$trace = isset($meta['trace'])
? $meta['trace']
: $this->debug->backtrace->get($meta['inclArgs'] ? Backtrace::INCL_ARGS : 0);
if ($trace && $meta['inclContext']) {
$trace = $this->debug->backtrace->addContext($trace);
$this->debug->addPlugin($this->debug->pluginHighlight, 'highlight');
}
unset($meta['trace']);
$logEntry['args'] = array($trace);
$logEntry['meta'] = $meta;
$this->evalRows($logEntry);
$this->debug->rootInstance->getPlugin('methodTable')->doTable($logEntry);
}

/**
* Set default meta values
*
* @param LogEntry $logEntry LogEntry instance
*
* @return array meta values
*/
private function getMeta(LogEntry $logEntry)
{
$meta = \array_merge(array(
'caption' => 'trace',
'columns' => array('file','line','function'),
'detectFiles' => true,
'inclArgs' => null, // incl arguments with context?
// will default to $inclContext
// may want to set meta['cfg']['objectsExclude'] = '*'
'inclContext' => false,
'sortable' => false,
'trace' => null, // set to specify trace
), $logEntry['meta']);

if (\is_string($meta['caption']) === false) {
$this->debug->warn(\sprintf(
'trace caption should be a string. %s provided',
$this->debug->php->getDebugType($caption)
$this->debug->php->getDebugType($meta['caption'])
));
$logEntry->setMeta('caption', 'trace');
$meta['caption'] = 'trace';
}
// Get trace and include args if we're including context
$inclContext = $logEntry->getMeta('inclContext');
$inclArgs = $logEntry->getMeta('inclArgs');
$backtrace = isset($logEntry['meta']['trace'])
? $logEntry['meta']['trace']
: $this->debug->backtrace->get($inclArgs ? Backtrace::INCL_ARGS : 0);
$logEntry->setMeta('trace', null);
if ($backtrace && $inclContext) {
$backtrace = $this->debug->backtrace->addContext($backtrace);
$this->debug->addPlugin($this->debug->pluginHighlight, 'highlight');
if ($meta['inclArgs'] === null) {
$meta['inclArgs'] = $meta['inclContext'];
}
$logEntry['args'] = array($backtrace);
$this->debug->rootInstance->getPlugin('methodTable')->doTable($logEntry);
$this->debug->log($logEntry);
return $meta;
}

/**
* Handle "eval()'d code" frames
*
* @param LogEntry $logEntry LogEntry instance
*
* @return void
*/
private function evalRows(LogEntry $logEntry)
{
$meta = \array_replace_recursive(array(
'tableInfo' => array(
'rows' => array(),
),
), $logEntry['meta']);
$trace = $logEntry['args'][0];
foreach ($trace as $i => $frame) {
if (!empty($frame['evalLine'])) {
$meta['tableInfo']['rows'][$i]['attribs'] = array(
'data-file' => $frame['file'],
'data-line' => $frame['line'],
);
$trace[$i]['file'] = 'eval()\'d code';
$trace[$i]['line'] = $frame['evalLine'];
}
}
$logEntry['meta'] = $meta;
$logEntry['args'] = array($trace);
}
}
3 changes: 2 additions & 1 deletion src/Debug/Route/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ private function buildBody()
? 'Command: ' . \implode(' ', $this->debug->getServerParam('argv', array()))
: 'Request: ' . $this->debug->serverRequest->getMethod()
. ' ' . $this->debug->redact((string) $this->debug->serverRequest->getUri());
$body .= "\n\n";
/*
List errors that occured
*/
$errorStr = $this->buildErrorList();
if ($errorStr) {
$body .= 'Error(s):' . "\n"
. $errorStr . "\n";
. $errorStr . "\n\n";
}
/*
"attach" serialized log to body
Expand Down
35 changes: 11 additions & 24 deletions src/Debug/Route/Html/ErrorSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,52 +151,39 @@ protected function buildFatalMoreInfo(Error $error)
/**
* Build backtrace table
*
* @param array $backtrace backtrace from error object
* @param array $trace backtrace from error object
*
* @return string
*/
protected function buildFatalBacktrace($backtrace)
protected function buildFatalBacktrace($trace)
{
$cfgWas = $this->debug->setCfg(array(
'maxDepth' => 0,
// Don't inspect objects when dumping trace arguments... potentially huge objects
'objectsExclude' => array('*'),
), Debug::CONFIG_NO_PUBLISH);
$logEntry = $this->buildFatalBacktraceLogEntry($backtrace);
$this->debug->rootInstance->getPlugin('methodTable')->doTable($logEntry);
$this->debug->setCfg($cfgWas, Debug::CONFIG_NO_PUBLISH | Debug::CONFIG_NO_RETURN);
return '<li class="m_trace" data-detect-files="true">' . $this->routeHtml->dumper->table->build(
$logEntry['args'][0],
$logEntry['meta']
) . '</li>' . "\n";
}

/**
* Create temporary LogEntry used by buildFatalBacktrace
*
* @param array $backtrace backtrace from error object
*
* @return LogEntry
*/
private function buildFatalBacktraceLogEntry($backtrace)
{
return new LogEntry(
$logEntry = new LogEntry(
$this->debug,
'table',
array($backtrace),
array(),
array(
'attribs' => array(
'class' => 'trace trace-context table-bordered',
),
'caption' => 'trace',
'columns' => array('file','line','function'),
'inclContext' => true,
'onBuildRow' => array(
array($this->routeHtml->dumper->helper, 'tableMarkupFunction'),
array($this->routeHtml->dumper->helper, 'tableAddContextRow'),
),
'trace' => $trace,
)
);
$this->debug->rootInstance->getPlugin('methodTrace')->doTrace($logEntry);
$this->debug->setCfg($cfgWas, Debug::CONFIG_NO_PUBLISH | Debug::CONFIG_NO_RETURN);
return '<li class="m_trace" data-detect-files="true">' . $this->routeHtml->dumper->table->build(
$logEntry['args'][0],
$logEntry['meta']
) . '</li>' . "\n";
}

/**
Expand Down
18 changes: 10 additions & 8 deletions src/Debug/Route/WampCrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,21 @@ public function crateLogEntry(LogEntry $logEntry)
$logEntryTmp = new LogEntry(
$this->debug,
'trace',
array($meta['trace']),
array(),
array(
'columns' => array('file','line','function'),
'inclContext' => $logEntry->getMeta('inclContext', false),
'trace' => $meta['trace'],
)
);
$this->debug->rootInstance->getPlugin('methodTable')->doTable($logEntryTmp);
$this->debug->rootInstance->getPlugin('methodTrace')->doTrace($logEntryTmp);
unset($args[2]); // error's filepath argument
$meta = \array_merge($meta, array(
'caption' => 'trace',
'tableInfo' => $logEntryTmp['meta']['tableInfo'],
'trace' => $logEntryTmp['args'][0],
));
$meta = \array_replace_recursive(
$meta,
$logEntryTmp['meta'],
array(
'trace' => $logEntryTmp['args'][0],
)
);
}
if ($this->detectFiles) {
$meta['foundFiles'] = $this->foundFiles();
Expand Down
Loading

0 comments on commit 3e87e23

Please sign in to comment.