Skip to content

Commit 5d1b9bb

Browse files
Bin LiaoRealCLanger
authored andcommitted
8233790: Forward output from heap dumper to jcmd/jmap
Reviewed-by: phh Backport-of: b2a9673
1 parent 9012b4a commit 5d1b9bb

File tree

4 files changed

+18
-44
lines changed

4 files changed

+18
-44
lines changed

src/hotspot/share/services/attachListener.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,7 @@ jint dump_heap(AttachOperation* op, outputStream* out) {
236236
// This helps reduces the amount of unreachable objects in the dump
237237
// and makes it easier to browse.
238238
HeapDumper dumper(live_objects_only /* request GC */);
239-
int res = dumper.dump(op->arg(0));
240-
if (res == 0) {
241-
out->print_cr("Heap dump file created");
242-
} else {
243-
// heap dump failed
244-
ResourceMark rm;
245-
char* error = dumper.error_as_C_string();
246-
if (error == NULL) {
247-
out->print_cr("Dump failed - reason unknown");
248-
} else {
249-
out->print_cr("%s", error);
250-
}
251-
}
239+
dumper.dump(op->arg(0), out);
252240
}
253241
return JNI_OK;
254242
}

src/hotspot/share/services/diagnosticCommand.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -519,19 +519,7 @@ void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
519519
// This helps reduces the amount of unreachable objects in the dump
520520
// and makes it easier to browse.
521521
HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
522-
int res = dumper.dump(_filename.value(), _overwrite.value());
523-
if (res == 0) {
524-
output()->print_cr("Heap dump file created");
525-
} else {
526-
// heap dump failed
527-
ResourceMark rm;
528-
char* error = dumper.error_as_C_string();
529-
if (error == NULL) {
530-
output()->print_cr("Dump failed - reason unknown");
531-
} else {
532-
output()->print_cr("%s", error);
533-
}
534-
}
522+
dumper.dump(_filename.value(), output(), _overwrite.value());
535523
}
536524

537525
int HeapDumpDCmd::num_arguments() {

src/hotspot/share/services/heapDumper.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,21 +1935,21 @@ void VM_HeapDumper::dump_stack_traces() {
19351935
}
19361936

19371937
// dump the heap to given path.
1938-
int HeapDumper::dump(const char* path, bool overwrite) {
1938+
int HeapDumper::dump(const char* path, outputStream* out, bool overwrite) {
19391939
assert(path != NULL && strlen(path) > 0, "path missing");
19401940

19411941
// print message in interactive case
1942-
if (print_to_tty()) {
1943-
tty->print_cr("Dumping heap to %s ...", path);
1942+
if (out != NULL) {
1943+
out->print_cr("Dumping heap to %s ...", path);
19441944
timer()->start();
19451945
}
19461946

19471947
// create the dump writer. If the file can be opened then bail
19481948
DumpWriter writer(path, overwrite);
19491949
if (writer.error() != NULL) {
19501950
set_error(writer.error());
1951-
if (print_to_tty()) {
1952-
tty->print_cr("Unable to create %s: %s", path,
1951+
if (out != NULL) {
1952+
out->print_cr("Unable to create %s: %s", path,
19531953
(error() != NULL) ? error() : "reason unknown");
19541954
}
19551955
return -1;
@@ -1969,13 +1969,13 @@ int HeapDumper::dump(const char* path, bool overwrite) {
19691969
set_error(writer.error());
19701970

19711971
// print message in interactive case
1972-
if (print_to_tty()) {
1972+
if (out != NULL) {
19731973
timer()->stop();
19741974
if (error() == NULL) {
1975-
tty->print_cr("Heap dump file created [" JULONG_FORMAT " bytes in %3.3f secs]",
1975+
out->print_cr("Heap dump file created [" JULONG_FORMAT " bytes in %3.3f secs]",
19761976
writer.bytes_written(), timer()->seconds());
19771977
} else {
1978-
tty->print_cr("Dump file is incomplete: %s", writer.error());
1978+
out->print_cr("Dump file is incomplete: %s", writer.error());
19791979
}
19801980
}
19811981

@@ -2103,8 +2103,7 @@ void HeapDumper::dump_heap(bool oome) {
21032103
dump_file_seq++; // increment seq number for next time we dump
21042104

21052105
HeapDumper dumper(false /* no GC before heap dump */,
2106-
true /* send to tty */,
21072106
oome /* pass along out-of-memory-error flag */);
2108-
dumper.dump(my_path);
2107+
dumper.dump(my_path, tty);
21092108
os::free(my_path);
21102109
}

src/hotspot/share/services/heapDumper.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,36 @@
4141
// }
4242
//
4343

44+
class outputStream;
45+
4446
class HeapDumper : public StackObj {
4547
private:
4648
char* _error;
47-
bool _print_to_tty;
4849
bool _gc_before_heap_dump;
4950
bool _oome;
5051
elapsedTimer _t;
5152

52-
HeapDumper(bool gc_before_heap_dump, bool print_to_tty, bool oome) :
53-
_gc_before_heap_dump(gc_before_heap_dump), _error(NULL), _print_to_tty(print_to_tty), _oome(oome) { }
53+
HeapDumper(bool gc_before_heap_dump, bool oome) :
54+
_gc_before_heap_dump(gc_before_heap_dump), _error(NULL), _oome(oome) { }
5455

5556
// string representation of error
5657
char* error() const { return _error; }
5758
void set_error(char* error);
5859

59-
// indicates if progress messages can be sent to tty
60-
bool print_to_tty() const { return _print_to_tty; }
61-
6260
// internal timer.
6361
elapsedTimer* timer() { return &_t; }
6462

6563
static void dump_heap(bool oome);
6664

6765
public:
6866
HeapDumper(bool gc_before_heap_dump) :
69-
_gc_before_heap_dump(gc_before_heap_dump), _error(NULL), _print_to_tty(false), _oome(false) { }
67+
_gc_before_heap_dump(gc_before_heap_dump), _error(NULL), _oome(false) { }
7068

7169
~HeapDumper();
7270

7371
// dumps the heap to the specified file, returns 0 if success.
74-
int dump(const char* path, bool overwrite = false);
72+
// additional info is written to out if not NULL.
73+
int dump(const char* path, outputStream* out = NULL, bool overwrite = false);
7574

7675
// returns error message (resource allocated), or NULL if no error
7776
char* error_as_C_string() const;

0 commit comments

Comments
 (0)