Update log2lines to Version 1.11 (Jan Roeloffzen) Bug 4342

- Added -P Pipeline option for Powershell support, see below
- Renaming some structs to avoid potential naming conflicts with windows.h
- stdin/out -> stdIn/Out for redirecting
- Fixed potential mem leak
- A little more tracing and cleanup
Updated piperead (Jan Roeloffzen) Bug 5146
- Added Pipe client function, based on msdn example
- Improved option handling (try piperead -h)
- logging, error handling
- Integrated in build (pipetools.mak)

svn path=/trunk/; revision=45287
This commit is contained in:
Daniel Reimer 2010-01-27 19:03:52 +00:00
parent 532aff4429
commit 166fa77176
4 changed files with 267 additions and 49 deletions

View file

@ -2,13 +2,10 @@
// piperead.cpp // piperead.cpp
// //
// Martin Fuchs, 30.11.2003 // Martin Fuchs, 30.11.2003
// //
// Jan Roeloffzen, 26.1.2010
// // Pipe client, based on msdn example
// Invoke as: "piperead [pipe_name]",
// for example: "piperead com_1"
//
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
@ -21,24 +18,15 @@
#endif #endif
#define BUFSIZE 1024
static void print_error(DWORD win32_error) static void print_error(DWORD win32_error)
{ {
fprintf(stderr, "WIN32 error %lu\n", win32_error); fprintf(stderr, "WIN32 error %lu\n", win32_error);
} }
static int pipeServer(char *path)
int main(int argc, char** argv)
{ {
char path[MAX_PATH];
const char* pipe_name;
if (argc > 1)
pipe_name = *++argv;
else
pipe_name = "com_1";
sprintf(path, "\\\\.\\pipe\\%s", pipe_name);
HANDLE hPipe = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX|FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_WAIT|PIPE_TYPE_BYTE, 1, 4096, 4096, 30000, NULL); HANDLE hPipe = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX|FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_WAIT|PIPE_TYPE_BYTE, 1, 4096, 4096, 30000, NULL);
if (hPipe == INVALID_HANDLE_VALUE) { if (hPipe == INVALID_HANDLE_VALUE) {
@ -48,23 +36,26 @@ int main(int argc, char** argv)
for(;;) { for(;;) {
DWORD read; DWORD read;
BYTE buffer[1024]; BYTE buffer[BUFSIZE];
if (!ReadFile(hPipe, buffer, sizeof(buffer), &read, NULL)) { if (!ReadFile(hPipe, buffer, sizeof(buffer), &read, NULL)) {
DWORD error = GetLastError(); DWORD error = GetLastError();
if (error == ERROR_PIPE_LISTENING) if (error == ERROR_PIPE_LISTENING) {
fprintf(stderr,"INVALID_HANDLE_VALUE\n");
Sleep(1000); Sleep(1000);
else if (error == ERROR_BROKEN_PIPE) { } else if (error == ERROR_BROKEN_PIPE) {
CloseHandle(hPipe); CloseHandle(hPipe);
hPipe = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX|FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_WAIT|PIPE_TYPE_BYTE, 1, 4096, 4096, 30000, NULL); hPipe = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX|FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_WAIT|PIPE_TYPE_BYTE, 1, 4096, 4096, 30000, NULL);
if (hPipe == INVALID_HANDLE_VALUE) { if (hPipe == INVALID_HANDLE_VALUE) {
fprintf(stderr,"INVALID_HANDLE_VALUE\n");
print_error(GetLastError()); print_error(GetLastError());
return 1; return 1;
} }
} else { } else {
fprintf(stderr,"error %lu\n",error);
print_error(error); print_error(error);
break; break;
} }
@ -79,3 +70,132 @@ int main(int argc, char** argv)
return 0; return 0;
} }
static int pipeClient(char *path)
{
HANDLE hPipe=INVALID_HANDLE_VALUE;
TCHAR chBuf[BUFSIZE];
BOOL fSuccess = FALSE;
DWORD cbRead;
DWORD Err;
int res = 0;
setvbuf(stdout, NULL, _IONBF, 0);
while (1) {
hPipe = CreateFile(
path, // pipe name
GENERIC_READ,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
// Break if the pipe handle is valid.
if (hPipe != INVALID_HANDLE_VALUE)
break;
// Exit if an error other than ERROR_PIPE_BUSY occurs.
if (GetLastError() != ERROR_PIPE_BUSY) {
fprintf(stderr,"Could not open pipe. Error=%lu\n", GetLastError() );
res = -1;
break;
}
// All pipe instances are busy, so wait for 20 seconds.
if ( ! WaitNamedPipe(path, 20000)) {
fprintf(stderr,"Could not open pipe: 20 second wait timed out.");
res = -2;
break;
}
}
if (!res) do {
fSuccess = ReadFile(
hPipe, // pipe handle
chBuf, // buffer to receive reply
BUFSIZE, // size of buffer
&cbRead, // number of bytes read
NULL); // not overlapped
if ( ! fSuccess ) {
Err = GetLastError();
if ( Err == ERROR_MORE_DATA ) {
fSuccess = TRUE;
} else {
fprintf(stderr, "ReadFile: Error %lu \n", Err );
res = -9;
break;
}
}
fwrite(chBuf,1,cbRead,stdout);
} while ( fSuccess);
if ( ! fSuccess) {
fprintf(stderr, "ReadFile from pipe failed. Error=%lu\n", GetLastError() );
res = -5;
}
if (hPipe != INVALID_HANDLE_VALUE)
CloseHandle(hPipe);
return res;
}
void usage(void)
{
fprintf(stderr, "Usage: piperead [-c] <named pipe>\n");
fprintf(stderr, "-c means Client mode\n");
fprintf(stderr, "Example: piperead -c \\\\.\\pipe\\kdbg | log2lines -c\n\n");
}
int main(int argc, char** argv)
{
char path[MAX_PATH];
const char* pipe_name;
const char* clientMode;
int res = 0;
pipe_name = "com_1";
clientMode = NULL;
switch (argc) {
case 3:
clientMode = *++argv;
if (strcmp(clientMode,"-c") != 0) {
clientMode = NULL;
fprintf(stderr,"Invalid option: %s\n", clientMode);
res = -6;
}
//fall through
case 2:
pipe_name = *++argv;
if (strcmp(pipe_name,"-h") == 0) {
res = -7;
}
break;
default:
res = -8;
break;
}
if (res) {
usage();
return res;
}
if ( pipe_name[0] == '\\' ) {
//assume caller specified full path
sprintf(path, "%s", pipe_name);
} else {
sprintf(path, "\\\\.\\pipe\\%s", pipe_name);
}
if ( clientMode ) {
res = pipeClient(path);
} else {
res = pipeServer(path);
}
return res;
}

View file

@ -0,0 +1,47 @@
PIPETOOLS_BASE = $(TOOLS_BASE)$(SEP)pipetools
PIPETOOLS_BASE_ = $(PIPETOOLS_BASE)$(SEP)
PIPETOOLS_INT = $(INTERMEDIATE_)$(PIPETOOLS_BASE)
PIPETOOLS_INT_ = $(PIPETOOLS_INT)$(SEP)
PIPETOOLS_OUT = $(OUTPUT_)$(PIPETOOLS_BASE)
PIPETOOLS_OUT_ = $(PIPETOOLS_OUT)$(SEP)
$(PIPETOOLS_INT): | $(TOOLS_INT)
$(ECHO_MKDIR)
${mkdir} $@
ifneq ($(INTERMEDIATE),$(OUTPUT))
$(PIPETOOLS_OUT): | $(TOOLS_OUT)
$(ECHO_MKDIR)
${mkdir} $@
endif
PIPETOOLS_TARGET = \
$(PIPETOOLS_OUT_)piperead$(EXEPOSTFIX)
PIPETOOLS_SOURCES = \
$(PIPETOOLS_BASE_)piperead.cpp
PIPETOOLS_OBJECTS = \
$(addprefix $(INTERMEDIATE_), $(PIPETOOLS_SOURCES:.cpp=.o))
PIPETOOLS_HOST_CFLAGS = $(TOOLS_CFLAGS)
PIPETOOLS_HOST_LFLAGS = $(TOOLS_LFLAGS)
.PHONY: pipetools
pipetools: $(PIPETOOLS_TARGET)
$(PIPETOOLS_TARGET): $(PIPETOOLS_OBJECTS) | $(PIPETOOLS_OUT)
$(ECHO_HOSTLD)
${host_gcc} $(PIPETOOLS_OBJECTS) $(PIPETOOLS_HOST_LFLAGS) -o $@
$(PIPETOOLS_INT_)piperead.o: $(PIPETOOLS_BASE_)piperead.cpp | $(PIPETOOLS_INT)
$(ECHO_HOSTCC)
${host_gcc} $(PIPETOOLS_HOST_CFLAGS) -c $< -o $@
.PHONY: pipetools_clean
pipetools_clean:
-@$(rm) $(PIPETOOLS_TARGET) $(PIPETOOLS_OBJECTS) 2>$(NUL)
clean: pipetools_clean

View file

@ -11,7 +11,7 @@
#include "rsym.h" #include "rsym.h"
#define LOG2LINES_VERSION "1.9" #define LOG2LINES_VERSION "1.11"
/* Assume if an offset > ABS_TRESHOLD, then it must be absolute */ /* Assume if an offset > ABS_TRESHOLD, then it must be absolute */
#define ABS_TRESHOLD 0x00400000L #define ABS_TRESHOLD 0x00400000L
@ -27,6 +27,8 @@
#define CACHEFILE "log2lines.cache" #define CACHEFILE "log2lines.cache"
#define TRKBUILDPREFIX "bootcd-" #define TRKBUILDPREFIX "bootcd-"
#define SVN_PREFIX "/trunk/reactos/" #define SVN_PREFIX "/trunk/reactos/"
#define KDBG_PROMPT "kdbg>"
#define PIPEREAD_CMD "piperead -c"
#if defined (__DJGPP__) || defined (__WIN32__) #if defined (__DJGPP__) || defined (__WIN32__)
@ -96,13 +98,13 @@ struct entry_struct
struct entry_struct *pnext; struct entry_struct *pnext;
}; };
typedef struct entry_struct LIST_ENTRY; typedef struct entry_struct LIST_MEMBER;
struct list_struct struct list_struct
{ {
off_t st_size; off_t st_size;
LIST_ENTRY *phead; LIST_MEMBER *phead;
LIST_ENTRY *ptail; LIST_MEMBER *ptail;
}; };
struct summ_struct struct summ_struct
@ -149,7 +151,7 @@ static SUMM summ;
static LINEINFO lastLine; static LINEINFO lastLine;
static REVINFO revinfo; static REVINFO revinfo;
static char *optchars = "bcd:fFhl:mMrR:sS:tTuUvz:"; static char *optchars = "bcd:fFhl:mMP:rR:sS:tTuUvz:";
static int opt_buffered = 0; // -b static int opt_buffered = 0; // -b
static int opt_help = 0; // -h static int opt_help = 0; // -h
static int opt_force = 0; // -f static int opt_force = 0; // -f
@ -158,6 +160,7 @@ static int opt_verbose = 0; // -v
static int opt_console = 0; // -c static int opt_console = 0; // -c
static int opt_mark = 0; // -m static int opt_mark = 0; // -m
static int opt_Mark = 0; // -M static int opt_Mark = 0; // -M
static char *opt_Pipe = NULL; // -P
static int opt_raw = 0; // -r static int opt_raw = 0; // -r
static int opt_stats = 0; // -s static int opt_stats = 0; // -s
static int opt_Source = 0; // -S <opt_Source>[+<opt_SrcPlus>][,<sources_path>] static int opt_Source = 0; // -S <opt_Source>[+<opt_SrcPlus>][,<sources_path>]
@ -172,6 +175,8 @@ static char opt_logFile[MAX_PATH]; // -l <opt_logFile>
static char opt_7z[MAX_PATH]; // -z <opt_7z> static char opt_7z[MAX_PATH]; // -z <opt_7z>
static char opt_scanned[LINESIZE]; // all scanned options static char opt_scanned[LINESIZE]; // all scanned options
static FILE *logFile = NULL; static FILE *logFile = NULL;
static FILE *stdIn = NULL;
static FILE *stdOut = NULL;
static char *cache_name; static char *cache_name;
static char *tmp_name; static char *tmp_name;
@ -220,6 +225,7 @@ mkPath(char *path, int isDir)
return res; return res;
} }
#if 0
static FILE * static FILE *
rfopen(char *path, char *mode) rfopen(char *path, char *mode)
{ {
@ -232,12 +238,13 @@ rfopen(char *path, char *mode)
f = fopen(tmppath, mode); f = fopen(tmppath, mode);
return f; return f;
} }
#endif
static LIST_ENTRY * static LIST_MEMBER *
entry_lookup(LIST *list, char *name) entry_lookup(LIST *list, char *name)
{ {
LIST_ENTRY *pprev = NULL; LIST_MEMBER *pprev = NULL;
LIST_ENTRY *pnext; LIST_MEMBER *pnext;
if (!name || !name[0]) if (!name || !name[0])
return NULL; return NULL;
@ -261,8 +268,8 @@ entry_lookup(LIST *list, char *name)
return NULL; return NULL;
} }
static LIST_ENTRY * static LIST_MEMBER *
entry_delete(LIST_ENTRY *pentry) entry_delete(LIST_MEMBER *pentry)
{ {
if (!pentry) if (!pentry)
return NULL; return NULL;
@ -272,8 +279,8 @@ entry_delete(LIST_ENTRY *pentry)
return NULL; return NULL;
} }
static LIST_ENTRY * static LIST_MEMBER *
entry_insert(LIST *list, LIST_ENTRY *pentry) entry_insert(LIST *list, LIST_MEMBER *pentry)
{ {
if (!pentry) if (!pentry)
return NULL; return NULL;
@ -286,10 +293,10 @@ entry_insert(LIST *list, LIST_ENTRY *pentry)
} }
#if 0 #if 0
static LIST_ENTRY * static LIST_MEMBER *
entry_remove(LIST *list, LIST_ENTRY *pentry) entry_remove(LIST *list, LIST_MEMBER *pentry)
{ {
LIST_ENTRY *pprev = NULL, *p = NULL; LIST_MEMBER *pprev = NULL, *p = NULL;
if (!pentry) if (!pentry)
return NULL; return NULL;
@ -320,17 +327,17 @@ entry_remove(LIST *list, LIST_ENTRY *pentry)
} }
#endif #endif
static LIST_ENTRY * static LIST_MEMBER *
cache_entry_create(char *Line) cache_entry_create(char *Line)
{ {
LIST_ENTRY *pentry; LIST_MEMBER *pentry;
char *s = NULL; char *s = NULL;
int l; int l;
if (!Line) if (!Line)
return NULL; return NULL;
pentry = malloc(sizeof(LIST_ENTRY)); pentry = malloc(sizeof(LIST_MEMBER));
if (!pentry) if (!pentry)
return NULL; return NULL;
@ -372,10 +379,10 @@ cache_entry_create(char *Line)
} }
static LIST_ENTRY * static LIST_MEMBER *
sources_entry_create(LIST *list, char *path, char *prefix) sources_entry_create(LIST *list, char *path, char *prefix)
{ {
LIST_ENTRY *pentry; LIST_MEMBER *pentry;
char *s = NULL; char *s = NULL;
int l; int l;
@ -384,7 +391,7 @@ sources_entry_create(LIST *list, char *path, char *prefix)
if (!prefix) if (!prefix)
prefix = ""; prefix = "";
pentry = malloc(sizeof(LIST_ENTRY)); pentry = malloc(sizeof(LIST_MEMBER));
if (!pentry) if (!pentry)
return NULL; return NULL;
@ -988,7 +995,7 @@ static int
read_cache(void) read_cache(void)
{ {
FILE *fr; FILE *fr;
LIST_ENTRY *pentry; LIST_MEMBER *pentry;
char *Line = NULL; char *Line = NULL;
int result = 0; int result = 0;
@ -1117,7 +1124,7 @@ static int
translate_file(const char *cpath, size_t offset, char *toString) translate_file(const char *cpath, size_t offset, char *toString)
{ {
size_t base = 0; size_t base = 0;
LIST_ENTRY *pentry = NULL; LIST_MEMBER *pentry = NULL;
int res = 0; int res = 0;
char *path, *dpath; char *path, *dpath;
@ -1552,6 +1559,11 @@ static char *verboseUsage =
" -m Prefix (mark) each translated line with '* '.\n\n" " -m Prefix (mark) each translated line with '* '.\n\n"
" -M Prefix (mark) each NOT translated line with '? '.\n" " -M Prefix (mark) each NOT translated line with '? '.\n"
" ( Only for lines of the form: <IMAGENAME:ADDRESS> )\n\n" " ( Only for lines of the form: <IMAGENAME:ADDRESS> )\n\n"
" -P <cmd line>\n"
" Pipeline command line. Spawn <cmd line> and pipeline its output to\n"
" log2lines (as stdin). This is for shells lacking support of (one of):\n"
" - Input file redirection.\n"
" - Pipelining byte streams, needed for the -c option.\n\n"
" -r Raw output without translation.\n\n" " -r Raw output without translation.\n\n"
" -R <cmd>\n" " -R <cmd>\n"
" Revision commands interfacing with SVN. <cmd> is one of:\n" " Revision commands interfacing with SVN. <cmd> is one of:\n"
@ -1632,6 +1644,8 @@ static char *verboseUsage =
" log2lines -c < \\\\.\\pipe\\kdbg\n\n" " log2lines -c < \\\\.\\pipe\\kdbg\n\n"
" Use kdbg debugger via console, and append copy to logFile:\n" " Use kdbg debugger via console, and append copy to logFile:\n"
" log2lines -c -l dbg.log < \\\\.\\pipe\\kdbg\n\n" " log2lines -c -l dbg.log < \\\\.\\pipe\\kdbg\n\n"
" Same as above, but for PowerShell:\n"
" log2lines -c -l dbg.log -P \"piperead -c \\\\.\\pipe\\kdbg\"\n\n"
" Use kdbg debugger to send output to logfile:\n" " Use kdbg debugger to send output to logfile:\n"
" log2lines < \\\\.\\pipe\\kdbg > dbg.log\n\n" " log2lines < \\\\.\\pipe\\kdbg > dbg.log\n\n"
" Re-translate a debug log:\n" " Re-translate a debug log:\n"
@ -1868,6 +1882,8 @@ main(int argc, const char **argv)
int i; int i;
char *s; char *s;
stdIn = stdin;
stdOut = stdout;
strcpy(opt_dir, ""); strcpy(opt_dir, "");
strcpy(sources_path, ""); strcpy(sources_path, "");
if ((s = getenv(SOURCES_ENV))) if ((s = getenv(SOURCES_ENV)))
@ -1876,9 +1892,20 @@ main(int argc, const char **argv)
strcpy(opt_scanned, ""); strcpy(opt_scanned, "");
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++)
{ {
if (strcmp(argv[i],"-P")==0)
{
//Because its argument can contain spaces we cant use getopt(), a known bug:
if (i+1 < argc)
{
free(opt_Pipe);
opt_Pipe = malloc(LINESIZE);
strcpy(opt_Pipe, argv[i+1]);
}
}
strcat(opt_scanned, argv[i]); strcat(opt_scanned, argv[i]);
strcat(opt_scanned, " "); strcat(opt_scanned, " ");
} }
l2l_dbg(4,"opt_scanned=[%s]\n",opt_scanned);
strcpy(opt_logFile, ""); strcpy(opt_logFile, "");
strcpy(opt_7z, CMD_7Z); strcpy(opt_7z, CMD_7Z);
@ -1927,8 +1954,13 @@ main(int argc, const char **argv)
case 'r': case 'r':
opt_raw++; opt_raw++;
break; break;
case 'P':
optCount++;
//just count, see above
break;
case 'R': case 'R':
optCount++; optCount++;
free(opt_Revision);
opt_Revision = malloc(LINESIZE); opt_Revision = malloc(LINESIZE);
sscanf(optarg, "%s", opt_Revision); sscanf(optarg, "%s", opt_Revision);
break; break;
@ -2001,8 +2033,7 @@ main(int argc, const char **argv)
return 0; return 0;
read_cache(); read_cache();
l2l_dbg(4, "Cache read complete\n");
l2l_dbg(3, "Cache read complete\n");
if (*opt_logFile) if (*opt_logFile)
{ {
@ -2024,6 +2055,22 @@ main(int argc, const char **argv)
return 2; return 2;
} }
} }
l2l_dbg(4, "opt_logFile processed\n");
if (opt_Pipe)
{
l2l_dbg(3, "Command line: \"%s\"\n",opt_Pipe);
if (!(stdIn = POPEN(opt_Pipe, "r")))
{
stdIn = stdin; //restore
l2l_dbg(0, "Could not popen '%s' (%s)\n", opt_Pipe, strerror(errno));
free(opt_Pipe); opt_Pipe = NULL;
}
free(opt_Pipe); opt_Pipe = NULL;
}
l2l_dbg(4, "opt_Pipe processed\n");
if (argc > 1) if (argc > 1)
{ // translate {<exefile> <offset>} { // translate {<exefile> <offset>}
@ -2043,7 +2090,7 @@ main(int argc, const char **argv)
l2l_dbg(2, "translating %s %s\n", exefile, offset); l2l_dbg(2, "translating %s %s\n", exefile, offset);
translate_file(exefile, my_atoi(offset), Line); translate_file(exefile, my_atoi(offset), Line);
printf("%s\n", Line); printf("%s\n", Line);
report(stdout); report(stdOut);
} }
else else
{ {
@ -2061,11 +2108,14 @@ main(int argc, const char **argv)
} }
else else
{ // translate logging from stdin { // translate logging from stdin
translate_files(stdin, stdout); translate_files(stdIn, stdOut);
} }
if (logFile) if (logFile)
fclose(logFile); fclose(logFile);
if (opt_Pipe)
PCLOSE(stdIn);
return res; return res;
} }

View file

@ -44,6 +44,7 @@ include tools/bin2c.mak
include tools/buildno/buildno.mak include tools/buildno/buildno.mak
include tools/gendib/gendib.mak include tools/gendib/gendib.mak
include tools/rsym/log2lines.mak include tools/rsym/log2lines.mak
include tools/pipetools/pipetools.mak
include tools/nci/nci.mak include tools/nci/nci.mak
ifeq ($(ARCH),powerpc) ifeq ($(ARCH),powerpc)
include tools/ofw_interface/ofw_interface.mak include tools/ofw_interface/ofw_interface.mak