File: HeapwalkingDebuggee.java

package info (click to toggle)
openjdk-11-jre-dcevm 11.0.15%2B1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 221,420 kB
  • sloc: java: 1,363,561; cpp: 967,919; ansic: 69,480; xml: 62,475; sh: 8,106; asm: 3,475; python: 1,667; javascript: 943; makefile: 397; sed: 172; perl: 114
file content (231 lines) | stat: -rw-r--r-- 9,801 bytes parent folder | download | duplicates (10)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package nsk.share.jdi;

import java.io.*;
import java.util.*;
import nsk.share.Log;
import nsk.share.ObjectInstancesManager;
import nsk.share.TestBug;
import nsk.share.jpda.DebugeeArgumentHandler;
import nsk.share.jpda.IOPipe;

/*
 * Debuggee class used in tests for heapwalking(tests for VirtualMachine.instanceCounts, ReferenceType.instances, ObjectReference.referrers).
 * Handle commands related to creation of objects instances with given reference type
 * and given referrers number, use for this purposes nsk.share.ObjectInstancesManager.
 */
public class HeapwalkingDebuggee extends AbstractJDIDebuggee {
    protected ObjectInstancesManager objectInstancesManager;

    // reference of this type should be included in ObjectReference.referringObjects
    public static Set<String> includedIntoReferrersCountTypes = new HashSet<String>();

    // reference of this type should be included in ReferenceType.instances
    public static Set<String> includedIntoInstancesCountTypes = new HashSet<String>();

    static {
        includedIntoInstancesCountTypes.add(ObjectInstancesManager.STRONG_REFERENCE);
        includedIntoInstancesCountTypes.add(ObjectInstancesManager.WEAK_REFERENCE);
        includedIntoInstancesCountTypes.add(ObjectInstancesManager.SOFT_REFERENCE);
        includedIntoInstancesCountTypes.add(ObjectInstancesManager.PHANTOM_REFERENCE);
        includedIntoInstancesCountTypes.add(ObjectInstancesManager.JNI_GLOBAL_REFERENCE);
        includedIntoInstancesCountTypes.add(ObjectInstancesManager.JNI_LOCAL_REFERENCE);

        includedIntoReferrersCountTypes.add(ObjectInstancesManager.STRONG_REFERENCE);
        includedIntoReferrersCountTypes.add(ObjectInstancesManager.WEAK_REFERENCE);
        includedIntoReferrersCountTypes.add(ObjectInstancesManager.SOFT_REFERENCE);
        includedIntoReferrersCountTypes.add(ObjectInstancesManager.PHANTOM_REFERENCE);
    }

    //create number instance of class with given name, command format: createInstances:class_name:instance_count[:referrer_count:referrer_type]
    static public final String COMMAND_CREATE_INSTANCES = "createInstances";

    //'delete'(make unreachable) number instance of class with given name, command format: deleteInstances:class_name:instance_count:referrer_count
    static public final String COMMAND_DELETE_INSTANCES = "deleteInstances";

    //delete number referrers
    static public final String COMMAND_DELETE_REFERRERS = "deleteReferrers";

    //create instance with all type referrers
    static public final String COMMAND_CREATE_ALL_TYPE_REFERENCES = "createAllTypeReferences";

    protected void init(String args[]) {
        super.init(args);
        objectInstancesManager = new ObjectInstancesManager(log);
    }

    public void initDebuggee(DebugeeArgumentHandler argHandler, Log log, IOPipe pipe, String args[], boolean callExit) {
        super.initDebuggee(argHandler, log, pipe, args, callExit);
        objectInstancesManager = new ObjectInstancesManager(log);
    }

    public boolean parseCommand(String command) {
        if (super.parseCommand(command))
            return true;

        try {
            StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(command));
            tokenizer.whitespaceChars(':', ':');
            tokenizer.wordChars('_', '_');
            tokenizer.wordChars('$', '$');
            tokenizer.wordChars('[', ']');
            tokenizer.wordChars('|', '|');

            if (command.startsWith(COMMAND_CREATE_INSTANCES)) {
                //createInstances:class_name:instance_count[:referrer_count:referrer_type]

                tokenizer.nextToken();

                if (tokenizer.nextToken() != StreamTokenizer.TT_WORD)
                    throw new TestBug("Invalid command format: " + command);

                String className = tokenizer.sval;

                if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER)
                    throw new TestBug("Invalid command format: " + command);

                int instanceCounts = (int) tokenizer.nval;

                int referrerCount = 1;
                Set<String> referrerType = new HashSet<String>();

                if (tokenizer.nextToken() == StreamTokenizer.TT_NUMBER) {
                    referrerCount = (int) tokenizer.nval;

                    if (tokenizer.nextToken() == StreamTokenizer.TT_WORD)
                        referrerType.addAll(Arrays.asList(tokenizer.sval.split("\\|")));
                }
                if (referrerType.isEmpty()) {
                    referrerType.add(ObjectInstancesManager.STRONG_REFERENCE);
                }

                objectInstancesManager.createReferences(instanceCounts, className, referrerCount, referrerType);

                return true;
            } else if (command.startsWith(COMMAND_DELETE_INSTANCES)) {
                //deleteInstances:class_name:instance_count:referrer_count

                tokenizer.nextToken();

                if (tokenizer.nextToken() != StreamTokenizer.TT_WORD)
                    throw new TestBug("Invalid command format: " + command);

                String className = tokenizer.sval;

                if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER)
                    throw new TestBug("Invalid command format: " + command);

                int instanceCounts = (int) tokenizer.nval;

                objectInstancesManager.deleteAllReferrers(instanceCounts, className);

                return true;
            } else if (command.startsWith(COMMAND_DELETE_REFERRERS)) {
                tokenizer.nextToken();

                if (tokenizer.nextToken() != StreamTokenizer.TT_WORD)
                    throw new TestBug("Invalid command format: " + command);

                String className = tokenizer.sval;

                if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER)
                    throw new TestBug("Invalid command format: " + command);

                int referrersCount = (int) tokenizer.nval;

                Set<String> referrerTypes = new HashSet<String>();
                if (tokenizer.nextToken() == StreamTokenizer.TT_WORD) {
                    referrerTypes.addAll(Arrays.asList(tokenizer.sval.split("\\|")));
                }

                objectInstancesManager.deleteReferrers(className, referrersCount, referrerTypes);

                return true;
            } else if (command.startsWith(COMMAND_CREATE_ALL_TYPE_REFERENCES)) {
                tokenizer.nextToken();

                if (tokenizer.nextToken() != StreamTokenizer.TT_WORD)
                    throw new TestBug("Invalid command format: " + command);

                String className = tokenizer.sval;

                if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER)
                    throw new TestBug("Invalid command format: " + command);

                int instanceCounts = (int) tokenizer.nval;

                objectInstancesManager.createAllTypeReferences(className, instanceCounts);

                return true;
            }
        } catch (IOException e) {
            throw new TestBug("Invalid command format: " + command);
        }

        return false;
    }

    // instances of some classes couldn't be strictly controlled during test execution, use non-strict checks for this classes
    public static boolean useStrictCheck(String className, boolean otherThreadPresent) {
        if (className.equals("java.lang.String"))
            return false;

        if (className.equals("char[]"))
            return false;

        if (className.equals("byte[]"))
            return false;

        if (className.equals("java.lang.Thread")) {
            if (otherThreadPresent)
                return false;
        }

        return true;
    }

    // is reference with given type should be included in ObjectReference.referringObjects
    static public boolean isIncludedIntoReferrersCount(String referenceType) {
        if (!ObjectInstancesManager.allReferenceTypes.contains(referenceType)) {
            throw new TestBug("Invalid reference type: " + referenceType);
        }

        return includedIntoReferrersCountTypes.contains(referenceType);
    }

    // is reference with given type should be included in ReferenceType.instances
    static public boolean isIncludedIntoInstancesCount(String referenceType) {
        if (!ObjectInstancesManager.allReferenceTypes.contains(referenceType)) {
            throw new TestBug("Invalid reference type: " + referenceType);
        }

        return includedIntoInstancesCountTypes.contains(referenceType);
    }

    public static void main(String args[]) {
        HeapwalkingDebuggee debuggee = new HeapwalkingDebuggee();
        debuggee.init(args);
        debuggee.doTest();
    }
}