Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 229449 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/mat/hprof/messages.properties (+4 lines)
Lines 17-22 Link Here
17
HprofRandomAccessParser_Error_IllegalDumpSegment=Illegal dump segment {0}
17
HprofRandomAccessParser_Error_IllegalDumpSegment=Illegal dump segment {0}
18
HprofRandomAccessParser_Error_MissingClass=missing fake class {0}
18
HprofRandomAccessParser_Error_MissingClass=missing fake class {0}
19
HprofRandomAccessParser_Error_MissingFakeClass=missing fake class
19
HprofRandomAccessParser_Error_MissingFakeClass=missing fake class
20
JMapHeapDumpProvider_ErrorCreatingDump=Error creating heap dump. jmap exit code = 
21
JMapHeapDumpProvider_HeapDumpNotCreated=Heap dump file was not created. jmap exit code = 
22
JMapHeapDumpProvider_WaitForHeapDump=Waiting while the heap dump is written to the disk
23
LocalJavaProcessesUtils_ErrorGettingProcesses=Error getting list of processes
20
Pass1Parser_Error_IllegalRecordLength=Illegal record length at byte {0}
24
Pass1Parser_Error_IllegalRecordLength=Illegal record length at byte {0}
21
Pass1Parser_Error_IllegalType=Illegal primitive object array type
25
Pass1Parser_Error_IllegalType=Illegal primitive object array type
22
Pass1Parser_Error_InvalidHeapDumpFile=Error: Invalid heap dump file.\n Unsupported segment type {0} at position {1}
26
Pass1Parser_Error_InvalidHeapDumpFile=Error: Invalid heap dump file.\n Unsupported segment type {0} at position {1}
(-)src/org/eclipse/mat/hprof/Messages.java (-1 / +5 lines)
Lines 24-30 Link Here
24
    public static String HprofRandomAccessParser_Error_IllegalDumpSegment;
24
    public static String HprofRandomAccessParser_Error_IllegalDumpSegment;
25
    public static String HprofRandomAccessParser_Error_MissingClass;
25
    public static String HprofRandomAccessParser_Error_MissingClass;
26
    public static String HprofRandomAccessParser_Error_MissingFakeClass;
26
    public static String HprofRandomAccessParser_Error_MissingFakeClass;
27
    public static String Pass1Parser_Error_IllegalRecordLength;
27
    public static String JMapHeapDumpProvider_ErrorCreatingDump;
28
	public static String JMapHeapDumpProvider_HeapDumpNotCreated;
29
	public static String JMapHeapDumpProvider_WaitForHeapDump;
30
	public static String LocalJavaProcessesUtils_ErrorGettingProcesses;
31
	public static String Pass1Parser_Error_IllegalRecordLength;
28
    public static String Pass1Parser_Error_IllegalType;
32
    public static String Pass1Parser_Error_IllegalType;
29
    public static String Pass1Parser_Error_InvalidHeapDumpFile;
33
    public static String Pass1Parser_Error_InvalidHeapDumpFile;
30
    public static String Pass1Parser_Error_invalidHPROFFile;
34
    public static String Pass1Parser_Error_invalidHPROFFile;
(-)src/org/eclipse/mat/hprof/acquire/JMapHeapDumpProvider.java (+73 lines)
Line 0 Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 SAP AG.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    SAP AG - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.mat.hprof.acquire;
12
13
import java.io.File;
14
import java.util.List;
15
import java.util.logging.Logger;
16
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.mat.SnapshotException;
19
import org.eclipse.mat.hprof.Messages;
20
import org.eclipse.mat.hprof.acquire.LocalJavaProcessesUtils.StreamCollector;
21
import org.eclipse.mat.snapshot.acquire.IHeapDumpProvider;
22
import org.eclipse.mat.snapshot.acquire.VmInfo;
23
import org.eclipse.mat.util.IProgressListener;
24
25
public class JMapHeapDumpProvider implements IHeapDumpProvider
26
{
27
28
	private static final String FILE_PATTERN = "java_pid%pid%.hprof"; //$NON-NLS-1$
29
30
	public File acquireDump(VmInfo info, File preferredLocation, IProgressListener listener) throws Exception
31
	{
32
		listener.beginTask(Messages.JMapHeapDumpProvider_WaitForHeapDump, IProgressMonitor.UNKNOWN);
33
34
		String execLine = "jmap -dump:format=b,file=" + preferredLocation.getAbsolutePath() + " " + info.getPid(); //$NON-NLS-1$ //$NON-NLS-2$
35
		Logger.getLogger(getClass().getName()).info("Executing: " + execLine); //$NON-NLS-1$
36
		Process p = Runtime.getRuntime().exec(execLine);
37
38
		StreamCollector error = new StreamCollector(p.getErrorStream());
39
		error.start();
40
		StreamCollector output = new StreamCollector(p.getInputStream());
41
		output.start();
42
43
		if (listener.isCanceled()) return null;
44
45
		int exitCode = p.waitFor();
46
		if (exitCode != 0)
47
		{
48
			throw new SnapshotException(Messages.JMapHeapDumpProvider_ErrorCreatingDump + exitCode + "\r\n" + error.buf.toString()); //$NON-NLS-2$
49
		}
50
51
		if (!preferredLocation.exists())
52
		{
53
			throw new SnapshotException(Messages.JMapHeapDumpProvider_HeapDumpNotCreated + exitCode + "\r\nstdout:\r\n" + output.buf.toString() //$NON-NLS-2$
54
					+ "\r\nstderr:\r\n" + error.buf.toString()); //$NON-NLS-1$
55
		}
56
57
		listener.done();
58
59
		return preferredLocation;
60
	}
61
62
	public List<VmInfo> getAvailableVMs()
63
	{
64
		List<VmInfo> jvms = LocalJavaProcessesUtils.getLocalVMsUsingJPS();
65
		for (VmInfo vmInfo : jvms)
66
		{
67
			vmInfo.setProposedFileName(FILE_PATTERN);
68
			vmInfo.setHeapDumpProvider(this);
69
		}
70
		return jvms;
71
	}
72
73
}
(-)src/org/eclipse/mat/hprof/acquire/LocalJavaProcessesUtils.java (+99 lines)
Line 0 Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 SAP AG.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    SAP AG - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.mat.hprof.acquire;
12
13
import java.io.BufferedReader;
14
import java.io.IOException;
15
import java.io.InputStream;
16
import java.io.InputStreamReader;
17
import java.util.ArrayList;
18
import java.util.List;
19
import java.util.StringTokenizer;
20
import java.util.logging.Level;
21
import java.util.logging.Logger;
22
23
import org.eclipse.mat.hprof.Messages;
24
import org.eclipse.mat.snapshot.acquire.VmInfo;
25
26
public class LocalJavaProcessesUtils
27
{
28
	static List<VmInfo> getLocalVMsUsingJPS()
29
	{
30
		try
31
		{
32
			Process p = Runtime.getRuntime().exec("jps"); //$NON-NLS-1$
33
			StreamCollector error = new StreamCollector(p.getErrorStream());
34
			error.start();
35
			StreamCollector output = new StreamCollector(p.getInputStream());
36
			output.start();
37
38
			int exitVal = p.waitFor();
39
40
			if (exitVal != 0) return null;
41
42
			List<VmInfo> vms = new ArrayList<VmInfo>();
43
			StringTokenizer tok = new StringTokenizer(output.buf.toString(), "\r\n"); //$NON-NLS-1$
44
			while (tok.hasMoreTokens())
45
			{
46
				String token = tok.nextToken();
47
48
				// System.err.println(token);
49
				VmInfo info = parseJPSLine(token);
50
				if (info != null) vms.add(info);
51
			}
52
53
			return vms;
54
		}
55
		catch (Throwable t)
56
		{
57
			// $JL-EXC$
58
			return null;
59
		}
60
	}
61
62
	private static VmInfo parseJPSLine(String line)
63
	{
64
		int firstSpaceIdx = line.indexOf(' ');
65
		if (firstSpaceIdx == -1) return null;
66
		int pid = Integer.parseInt(line.substring(0, firstSpaceIdx));
67
		String description = line.substring(firstSpaceIdx);
68
		return new VmInfo(pid, description, false, null, null);
69
	}
70
71
	static class StreamCollector extends Thread
72
	{
73
		InputStream is;
74
		StringBuilder buf;
75
76
		StreamCollector(InputStream is)
77
		{
78
			this.is = is;
79
			this.buf = new StringBuilder();
80
		}
81
82
		public void run()
83
		{
84
			try
85
			{
86
				InputStreamReader isr = new InputStreamReader(is);
87
				BufferedReader br = new BufferedReader(isr);
88
				String line = null;
89
				while ((line = br.readLine()) != null)
90
					buf.append(line).append("\r\n"); //$NON-NLS-1$
91
			}
92
			catch (IOException ioe)
93
			{
94
				Logger.getLogger(getClass().getName()).log(Level.SEVERE, Messages.LocalJavaProcessesUtils_ErrorGettingProcesses, ioe);
95
			}
96
		}
97
	}
98
99
}
(-)plugin.xml (+6 lines)
Lines 25-29 Link Here
25
		      priority="normal">
25
		      priority="normal">
26
		</content-type>
26
		</content-type>
27
	</extension>
27
	</extension>
28
 <extension
29
       point="org.eclipse.mat.api.heapDumpProvider">
30
    <provider
31
          impl="org.eclipse.mat.hprof.acquire.JMapHeapDumpProvider">
32
    </provider>
33
 </extension>
28
34
29
</plugin>
35
</plugin>

Return to bug 229449