Skip to content

Commit

Permalink
Merge pull request #4628 from AidanHa/Issue2837
Browse files Browse the repository at this point in the history
Ensure _Filehandle does not close if printing to STDOUT/ERR
  • Loading branch information
keithc-ca authored Feb 22, 2019
2 parents f5f7920 + 7ce281d commit 999c3f6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
22 changes: 16 additions & 6 deletions runtime/rasdump/TextFileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
#include "TextFileStream.hpp"
#include "j9.h"
#include "j9port.h"
#include "rasdump_internal.h"

/* Constructor */
TextFileStream::TextFileStream(J9PortLibrary* portLibrary) :
_Buffer(NULL),
_IsOpen(false),
_BufferPos(0),
_BufferSize(16*1024),
_PortLibrary(portLibrary),
Expand All @@ -51,12 +54,16 @@ void
TextFileStream::open(const char* fileName, bool cacheWrites)
{
PORT_ACCESS_FROM_PORT(_PortLibrary);
if (NULL != strstr(fileName, "/STDOUT/")) {
_FileHandle = 1;
} else if (NULL != strstr(fileName, "/STDERR/")) {
_FileHandle = 2;
} else if (fileName[0] != '-' ) {
if (0 == strcmp(fileName, J9RAS_STDOUT_NAME)) {
_FileHandle = J9PORT_TTY_OUT;
} else if (0 == strcmp(fileName, J9RAS_STDERR_NAME)) {
_FileHandle = J9PORT_TTY_ERR;
} else {
_FileHandle = j9file_open(fileName, EsOpenWrite | EsOpenCreate | EsOpenTruncate | EsOpenCreateNoTag, 0666);
if (_FileHandle != -1) {
_IsOpen = true;
}

}
if(!cacheWrites) {
_BufferSize = 0;
Expand All @@ -73,7 +80,10 @@ TextFileStream::close(void)
j9file_write_text(_FileHandle, _Buffer, _BufferPos);
}
j9file_sync(_FileHandle);
j9file_close(_FileHandle);
if (_IsOpen) {
/*If we don't open the file stream, it means that we don't close it either */
j9file_close(_FileHandle);
}
}

_FileHandle = -1;
Expand Down
3 changes: 2 additions & 1 deletion runtime/rasdump/TextFileStream.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2003, 2014 IBM Corp. and others
* Copyright (c) 2003, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -67,6 +67,7 @@ private :
TextFileStream(const TextFileStream& source);
TextFileStream& operator=(const TextFileStream& source);
char *_Buffer;
bool _IsOpen;
UDATA _BufferPos;
UDATA _BufferSize;

Expand Down
7 changes: 6 additions & 1 deletion runtime/rasdump/dmpagent.c
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,12 @@ static omr_error_t
doJavaDump(J9RASdumpAgent *agent, char *label, J9RASdumpContext *context)
{
J9JavaVM *vm = context->javaVM;
if ((0 != strcmp(label, "/STDOUT/")) && (0 != strcmp(label, "/STDERR/"))) {

if ((0 == strcmp("-", label)) || (0 == j9_cmdla_stricmp(label, J9RAS_STDOUT_NAME))) {
strcpy(label, J9RAS_STDOUT_NAME);
} else if (0 == j9_cmdla_stricmp(label, J9RAS_STDERR_NAME)) {
strcpy(label, J9RAS_STDERR_NAME);
} else {
if (makePath(vm, label) == OMR_ERROR_INTERNAL) {
/* Nowhere available to write the dump, we are done, makePath() will have issued error message */
return OMR_ERROR_INTERNAL;
Expand Down
5 changes: 4 additions & 1 deletion runtime/rasdump/rasdump_internal.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2018 IBM Corp. and others
* Copyright (c) 1991, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -126,6 +126,9 @@ void setAllocationThreshold(J9VMThread *vmThread, UDATA min, UDATA max);

#define J9RAS_DUMP_EXCEPTION_EVENT_GROUP (J9RAS_DUMP_ON_EXCEPTION_THROW | J9RAS_DUMP_ON_EXCEPTION_SYSTHROW | J9RAS_DUMP_ON_EXCEPTION_CATCH | J9RAS_DUMP_ON_EXCEPTION_DESCRIBE)

#define J9RAS_STDOUT_NAME "/STDOUT/"
#define J9RAS_STDERR_NAME "/STDERR/"

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 999c3f6

Please sign in to comment.