php-src/phpdbg_list.c

127 lines
3.4 KiB
C
Raw Normal View History

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> |
+----------------------------------------------------------------------+
*/
#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-12 02:29:56 +00:00
void phpdbg_list_file(const char *filename, long count, long offset 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-11 23:40:03 +00:00
unsigned int line = 0, displayed = 0;
2013-11-12 02:29:56 +00:00
if (VCWD_STAT(filename, &st) == -1) {
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 12:38:04 +00:00
#ifndef _WIN32
2013-11-12 02:29:56 +00:00
if ((fd = VCWD_OPEN(filename, O_RDONLY)) == -1) {
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-11 23:40:03 +00:00
last_pos = mem = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
end_pos = mem + st.st_size;
2013-11-12 12:38:04 +00:00
#else
fd = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
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;
}
map = CreateFileMapping(fd, NULL, PAGE_EXECUTE_READ, 0, 0, 0);
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-12 12:38:04 +00:00
last_pos = mem = (char*) MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
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) {
pos = memchr(last_pos, '\n', end_pos - last_pos);
if (!pos) {
/* No more line breaks */
break;
}
++line;
if (!offset || offset <= line) {
/* Without offset, or offset reached */
2013-11-12 23:31:46 +00:00
phpdbg_writeln("%05u: %.*s", line, (int)(pos - last_pos), last_pos);
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
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) {
return;
}
ops = (zend_op_array*)fbc;
phpdbg_list_file(ops->filename,
ops->line_end - ops->line_start + 1, ops->line_start TSRMLS_CC);
2013-11-12 02:19:43 +00:00
} /* }}} */
2013-11-12 12:38:04 +00:00