2013-11-11 23:40:03 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 5 |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Copyright (c) 1997-2013 The PHP Group |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_01.txt |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Felipe Pena <felipe@php.net> |
|
|
|
|
| Authors: Joe Watkins <joe.watkins@live.co.uk> |
|
2013-12-18 10:02:50 +00:00
|
|
|
| Authors: Bob Weinand <bwoebi@php.net> |
|
2013-11-11 23:40:03 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2013-11-12 12:38:04 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
# include <sys/mman.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2013-11-11 23:40:03 +00:00
|
|
|
#include <fcntl.h>
|
2013-11-12 02:29:56 +00:00
|
|
|
#include "phpdbg.h"
|
2013-11-11 23:40:03 +00:00
|
|
|
#include "phpdbg_list.h"
|
2013-11-12 23:43:49 +00:00
|
|
|
#include "phpdbg_utils.h"
|
2013-11-11 23:40:03 +00:00
|
|
|
|
2013-11-14 15:46:06 +00:00
|
|
|
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
|
|
|
|
|
|
|
|
PHPDBG_LIST(lines) /* {{{ */
|
|
|
|
{
|
2013-11-17 15:57:49 +00:00
|
|
|
if (!PHPDBG_G(exec) && !zend_is_executing(TSRMLS_C)) {
|
2013-11-17 11:15:53 +00:00
|
|
|
phpdbg_error("Not executing, and execution context not set");
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (param->type) {
|
|
|
|
case NUMERIC_PARAM:
|
|
|
|
case EMPTY_PARAM:
|
|
|
|
phpdbg_list_file(phpdbg_current_file(TSRMLS_C),
|
2013-11-25 19:33:52 +00:00
|
|
|
param->type == EMPTY_PARAM ? 0 : (param->num < 0 ? 1 - param->num : param->num),
|
|
|
|
(param->type != EMPTY_PARAM && param->num < 0 ? param->num : 0) + zend_get_executed_lineno(TSRMLS_C),
|
|
|
|
0 TSRMLS_CC);
|
2013-11-17 11:15:53 +00:00
|
|
|
break;
|
|
|
|
case FILE_PARAM:
|
2013-11-17 11:28:14 +00:00
|
|
|
phpdbg_list_file(param->file.name, param->file.line, 0, 0 TSRMLS_CC);
|
2013-11-17 11:15:53 +00:00
|
|
|
break;
|
2013-11-17 11:37:14 +00:00
|
|
|
|
2013-11-17 11:31:09 +00:00
|
|
|
phpdbg_default_switch_case();
|
2013-11-17 11:15:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-26 10:02:58 +00:00
|
|
|
return SUCCESS;
|
2013-11-14 15:46:06 +00:00
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
PHPDBG_LIST(func) /* {{{ */
|
|
|
|
{
|
2013-11-26 10:02:58 +00:00
|
|
|
switch (param->type) {
|
|
|
|
case STR_PARAM:
|
|
|
|
phpdbg_list_function_byname(
|
|
|
|
param->str, param->len TSRMLS_CC);
|
|
|
|
break;
|
2013-11-17 11:28:14 +00:00
|
|
|
|
2013-11-26 10:02:58 +00:00
|
|
|
phpdbg_default_switch_case();
|
|
|
|
}
|
2013-11-17 11:15:53 +00:00
|
|
|
|
2013-11-26 10:02:58 +00:00
|
|
|
return SUCCESS;
|
2013-11-16 18:53:12 +00:00
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
PHPDBG_LIST(method) /* {{{ */
|
|
|
|
{
|
2013-11-26 10:02:58 +00:00
|
|
|
switch (param->type) {
|
|
|
|
case METHOD_PARAM: {
|
|
|
|
zend_class_entry **ce;
|
|
|
|
|
|
|
|
if (zend_lookup_class(param->method.class, strlen(param->method.class), &ce TSRMLS_CC) == SUCCESS) {
|
|
|
|
zend_function *function;
|
|
|
|
char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name));
|
|
|
|
|
|
|
|
if (zend_hash_find(&(*ce)->function_table, lcname, strlen(lcname)+1, (void**) &function) == SUCCESS) {
|
|
|
|
phpdbg_list_function(function TSRMLS_CC);
|
|
|
|
} else {
|
|
|
|
phpdbg_error("Could not find %s::%s", param->method.class, param->method.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
efree(lcname);
|
|
|
|
} else {
|
|
|
|
phpdbg_error("Could not find the class %s", param->method.class);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
phpdbg_default_switch_case();
|
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
2013-11-16 18:53:12 +00:00
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
PHPDBG_LIST(class) /* {{{ */
|
|
|
|
{
|
2013-11-26 10:02:58 +00:00
|
|
|
switch (param->type) {
|
|
|
|
case STR_PARAM: {
|
|
|
|
zend_class_entry **ce;
|
|
|
|
|
|
|
|
if (zend_lookup_class(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) {
|
|
|
|
if ((*ce)->type == ZEND_USER_CLASS) {
|
|
|
|
if ((*ce)->info.user.filename) {
|
|
|
|
phpdbg_list_file(
|
|
|
|
(*ce)->info.user.filename,
|
|
|
|
(*ce)->info.user.line_end - (*ce)->info.user.line_start + 1,
|
|
|
|
(*ce)->info.user.line_start, 0 TSRMLS_CC
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
phpdbg_error("The source of the requested class (%s) cannot be found", (*ce)->name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
phpdbg_error("The class requested (%s) is not user defined", (*ce)->name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
phpdbg_error("The requested class (%s) could not be found", param->str);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
phpdbg_default_switch_case();
|
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
2013-11-14 15:46:06 +00:00
|
|
|
} /* }}} */
|
|
|
|
|
2013-11-17 11:11:09 +00:00
|
|
|
void phpdbg_list_file(const char *filename, long count, long offset, int highlight TSRMLS_DC) /* {{{ */
|
2013-11-11 23:40:03 +00:00
|
|
|
{
|
|
|
|
unsigned char *mem, *pos, *last_pos, *end_pos;
|
|
|
|
struct stat st;
|
2013-11-12 12:38:04 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
int fd;
|
|
|
|
#else
|
|
|
|
HANDLE fd, map;
|
|
|
|
#endif
|
|
|
|
int all_content = (count == 0);
|
2013-11-20 18:07:45 +00:00
|
|
|
int line = 0, displayed = 0;
|
2013-11-11 23:40:03 +00:00
|
|
|
|
2013-11-26 10:02:58 +00:00
|
|
|
if (VCWD_STAT(filename, &st) == FAILURE) {
|
2013-11-12 23:31:46 +00:00
|
|
|
phpdbg_error("Failed to stat file %s", filename);
|
2013-11-12 02:52:43 +00:00
|
|
|
return;
|
2013-11-12 00:33:17 +00:00
|
|
|
}
|
2013-11-17 11:28:14 +00:00
|
|
|
|
2013-11-12 12:38:04 +00:00
|
|
|
#ifndef _WIN32
|
2013-11-16 22:40:01 +00:00
|
|
|
if ((fd = VCWD_OPEN(filename, O_RDONLY)) == FAILURE) {
|
2013-11-12 23:31:46 +00:00
|
|
|
phpdbg_error("Failed to open file %s to list", filename);
|
2013-11-12 02:29:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:55:29 +00:00
|
|
|
pos = last_pos = mem = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
2013-11-11 23:40:03 +00:00
|
|
|
end_pos = mem + st.st_size;
|
2013-11-12 12:38:04 +00:00
|
|
|
#else
|
2013-11-17 23:48:07 +00:00
|
|
|
|
|
|
|
fd = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2013-11-12 12:38:04 +00:00
|
|
|
if (fd == INVALID_HANDLE_VALUE) {
|
2013-11-12 23:31:46 +00:00
|
|
|
phpdbg_error("Failed to open file!");
|
2013-11-12 12:38:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-17 23:48:07 +00:00
|
|
|
map = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL);
|
2013-11-12 12:38:04 +00:00
|
|
|
if (map == NULL) {
|
2013-11-12 23:31:46 +00:00
|
|
|
phpdbg_error("Failed to map file!");
|
2013-11-12 12:38:04 +00:00
|
|
|
CloseHandle(fd);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-11 23:40:03 +00:00
|
|
|
|
2013-11-18 21:55:29 +00:00
|
|
|
pos = last_pos = mem = (char*) MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
|
2013-11-12 12:38:04 +00:00
|
|
|
if (mem == NULL) {
|
2013-11-12 23:31:46 +00:00
|
|
|
phpdbg_error("Failed to map file in memory");
|
2013-11-12 12:38:04 +00:00
|
|
|
CloseHandle(map);
|
|
|
|
CloseHandle(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
end_pos = mem + st.st_size;
|
|
|
|
#endif
|
2013-11-11 23:40:03 +00:00
|
|
|
while (1) {
|
2013-11-17 12:23:50 +00:00
|
|
|
if (pos == end_pos) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-11 23:40:03 +00:00
|
|
|
pos = memchr(last_pos, '\n', end_pos - last_pos);
|
|
|
|
|
|
|
|
if (!pos) {
|
|
|
|
/* No more line breaks */
|
2013-11-17 12:23:50 +00:00
|
|
|
pos = end_pos;
|
2013-11-11 23:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++line;
|
|
|
|
|
|
|
|
if (!offset || offset <= line) {
|
|
|
|
/* Without offset, or offset reached */
|
2013-11-17 11:11:09 +00:00
|
|
|
if (!highlight) {
|
2013-11-26 10:02:58 +00:00
|
|
|
phpdbg_writeln("%05u: %.*s", line, (int)(pos - last_pos), last_pos);
|
2013-11-17 11:11:09 +00:00
|
|
|
} else {
|
2013-11-26 10:02:58 +00:00
|
|
|
if (highlight != line) {
|
|
|
|
phpdbg_writeln(" %05u: %.*s", line, (int)(pos - last_pos), last_pos);
|
|
|
|
} else {
|
|
|
|
phpdbg_writeln(">%05u: %.*s", line, (int)(pos - last_pos), last_pos);
|
|
|
|
}
|
2013-11-17 11:11:09 +00:00
|
|
|
}
|
2013-11-11 23:40:03 +00:00
|
|
|
++displayed;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_pos = pos + 1;
|
|
|
|
|
|
|
|
if (!all_content && displayed == count) {
|
|
|
|
/* Reached max line to display */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-12 12:38:04 +00:00
|
|
|
#ifndef _WIN32
|
2013-11-11 23:40:03 +00:00
|
|
|
munmap(mem, st.st_size);
|
|
|
|
close(fd);
|
2013-11-12 12:38:04 +00:00
|
|
|
#else
|
|
|
|
UnmapViewOfFile(mem);
|
|
|
|
CloseHandle(map);
|
|
|
|
CloseHandle(fd);
|
|
|
|
#endif
|
2013-11-11 23:40:03 +00:00
|
|
|
} /* }}} */
|
2013-11-12 02:19:43 +00:00
|
|
|
|
2013-11-12 02:35:07 +00:00
|
|
|
void phpdbg_list_function(const zend_function *fbc TSRMLS_DC) /* {{{ */
|
2013-11-12 02:19:43 +00:00
|
|
|
{
|
|
|
|
const zend_op_array *ops;
|
|
|
|
|
|
|
|
if (fbc->type != ZEND_USER_FUNCTION) {
|
2013-11-26 10:02:58 +00:00
|
|
|
phpdbg_error("The function requested (%s) is not user defined", fbc->common.function_name);
|
2013-11-12 02:19:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ops = (zend_op_array*)fbc;
|
|
|
|
|
|
|
|
phpdbg_list_file(ops->filename,
|
2013-11-17 11:11:09 +00:00
|
|
|
ops->line_end - ops->line_start + 1, ops->line_start, 0 TSRMLS_CC);
|
2013-11-12 02:19:43 +00:00
|
|
|
} /* }}} */
|
2013-11-12 12:38:04 +00:00
|
|
|
|
2013-11-20 18:52:34 +00:00
|
|
|
void phpdbg_list_function_byname(const char *str, size_t len TSRMLS_DC) /* {{{ */
|
2013-11-16 23:11:39 +00:00
|
|
|
{
|
2013-11-26 10:02:58 +00:00
|
|
|
HashTable *func_table = EG(function_table);
|
|
|
|
zend_function* fbc;
|
|
|
|
char *func_name = (char*) str;
|
|
|
|
size_t func_name_len = len;
|
|
|
|
|
|
|
|
/* search active scope if begins with period */
|
|
|
|
if (func_name[0] == '.') {
|
|
|
|
if (EG(scope)) {
|
|
|
|
func_name++;
|
|
|
|
func_name_len--;
|
|
|
|
|
|
|
|
func_table = &EG(scope)->function_table;
|
|
|
|
} else {
|
|
|
|
phpdbg_error("No active class");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (!EG(function_table)) {
|
|
|
|
phpdbg_error("No function table loaded");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
func_table = EG(function_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use lowercase names, case insensitive */
|
|
|
|
func_name = zend_str_tolower_dup(func_name, func_name_len);
|
|
|
|
|
|
|
|
if (zend_hash_find(func_table, func_name, func_name_len+1, (void**)&fbc) == SUCCESS) {
|
|
|
|
phpdbg_list_function(fbc TSRMLS_CC);
|
|
|
|
} else {
|
|
|
|
phpdbg_error("Function %s not found", func_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
efree(func_name);
|
2013-11-20 18:52:34 +00:00
|
|
|
} /* }}} */
|
2013-11-16 23:11:39 +00:00
|
|
|
|