Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5678b4e4909d3936e624ffc8924f8f7d2a88aafd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*******************************************************************************
 * Copyright (c) 2007, 2016 Wind River Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.ui.concurrent;

import java.lang.reflect.Field;
import java.util.concurrent.Executor;

import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DsfExecutable;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.internal.DsfPlugin;
import org.eclipse.cdt.dsf.ui.viewmodel.VMViewerUpdate;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;

/**
 * Data Request monitor that takes <code>IViewerUpdate</code> as a parent.
 * If the IViewerUpdate is canceled, this request monitor becomes canceled as well.
 * @see IViewerUpdate
 * 
 * @since 1.0
 */
public class ViewerDataRequestMonitor<V> extends DataRequestMonitor<V> {

    /**
     * Same as {@link DsfExecutable#DEBUG_MONITORS} 
     */
    static private boolean DEBUG_MONITORS = DsfPlugin.DEBUG && Boolean.parseBoolean(
            Platform.getDebugOption("org.eclipse.cdt.dsf/debug/monitors")); //$NON-NLS-1$
    

    private final IViewerUpdate fUpdate;


    public ViewerDataRequestMonitor(Executor executor, IViewerUpdate update) {
        super(executor, null);
        fUpdate = update;
        
        if (DEBUG_MONITORS) {
        	createMonitorBacktrace();
        }
    }
    
    @Override
    public synchronized boolean isCanceled() { 
        return fUpdate.isCanceled() || super.isCanceled();
    }
    
    @Override
    protected void handleSuccess() {
        fUpdate.done();
    }

    @Override
    protected void handleErrorOrWarning() {
        fUpdate.setStatus(getStatus());
        fUpdate.done();
    }
    
    @Override
    protected void handleCancel() {
        fUpdate.setStatus(getStatus());
        fUpdate.done();
    }

	/**
	 * Instrument this object with a backtrace of the monitors this instance is
	 * chained to. See {@link DsfExecutable#DEBUG_MONITORS}. The logic here has
	 * to subvert Java access protection by using reflection. That's OK; this is
	 * not production code. This stuff will only ever run when tracing is turned
	 * on.
	 */
    private void createMonitorBacktrace() {
    	StringBuilder str = new StringBuilder();
    	
    	RequestMonitor nextrm = this;
    	VMViewerUpdate nextupdate = null; 
    	String type = null;
    	while (true) {
    		StackTraceElement topFrame = null;
    		if (nextupdate != null) {
    			type = "update "; //$NON-NLS-1$  extra space to match length of 'monitor'  
    			topFrame = getCreatedAtTopFrame(nextupdate);
    			nextrm = getMonitor(nextupdate);
    			nextupdate = null;
    		}
    		else if (nextrm != null) {
				type = "monitor"; //$NON-NLS-1$
    			topFrame = getCreatedAtTopFrame(nextrm);
    			if (nextrm instanceof ViewerDataRequestMonitor<?>) {
    				ViewerDataRequestMonitor<?> vdrm = (ViewerDataRequestMonitor<?>)nextrm;
    				nextupdate = (vdrm.fUpdate instanceof VMViewerUpdate) ? (VMViewerUpdate)vdrm.fUpdate : null;
    				nextrm = null;
    			}
    			else {
    				nextrm = getParentMonitor(nextrm);
    				nextupdate = null;
    			}
    		}
    		else { 
    			break;
    		}
			if (topFrame != null) {
				str.append('[').append(type).append("] ").append(topFrame).append('\n'); //$NON-NLS-1$
			}
			else {
				str.append("<unknown>\n"); //$NON-NLS-1$
			}
    	}

		Field field;
		try {
			field = RequestMonitor.class.getDeclaredField("fMonitorBacktrace"); //$NON-NLS-1$
			field.setAccessible(true);
			field.set(this, str.toString());
		} catch (IllegalAccessException e) {			
		} catch (SecurityException e) {
		} catch (NoSuchFieldException e) {
		}
    }

	/**
	 * Utility used by {@link #createMonitorBacktrace()}. Subverts access
	 * protection.
	 */
    private static RequestMonitor getMonitor(VMViewerUpdate update) {
    	try {
			Field field = VMViewerUpdate.class.getDeclaredField("fRequestMonitor"); //$NON-NLS-1$
			field.setAccessible(true);
			return (RequestMonitor) field.get(update);
		} catch (IllegalAccessException e) {
		} catch (SecurityException e) {
		} catch (NoSuchFieldException e) {
		} catch (ClassCastException e) {
		}
		return null;
    }

	/**
	 * Utility used by {@link #createMonitorBacktrace()}. Subverts access
	 * protection.
	 */
    private static RequestMonitor getParentMonitor(RequestMonitor rm) {
    	try {
			Field field = RequestMonitor.class.getDeclaredField("fParentRequestMonitor"); //$NON-NLS-1$
			field.setAccessible(true);
			return (RequestMonitor) field.get(rm);
		} catch (IllegalAccessException e) {
		} catch (NoSuchFieldException e) {
		}
		return null;
    }

	/**
	 * Utility used by {@link #createMonitorBacktrace()}. Subverts access
	 * protection.
	 */
    private static <T extends DsfExecutable> StackTraceElement getCreatedAtTopFrame(T dsfExecutable) {
    	try {
			Field field_fCreatedAt = DsfExecutable.class.getDeclaredField("fCreatedAt"); //$NON-NLS-1$
			field_fCreatedAt.setAccessible(true);
			Object obj_fCreatedAt = field_fCreatedAt.get(dsfExecutable);
			Class<?> class_StackTraceElement = Class.forName("org.eclipse.cdt.dsf.concurrent.StackTraceWrapper"); //$NON-NLS-1$
			Field field_fStackTraceElements = class_StackTraceElement.getDeclaredField("fStackTraceElements"); //$NON-NLS-1$
			field_fStackTraceElements.setAccessible(true);
			StackTraceElement[] frames = (StackTraceElement[])field_fStackTraceElements.get(obj_fCreatedAt);
			if (frames != null && frames.length > 0) {
				return frames[0];
			}
		} catch (IllegalAccessException e) {
		} catch (NoSuchFieldException e) {
		} catch (ClassNotFoundException e) {
		}
		return null;
    }
}

Back to the top