0N/A/*
2273N/A * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/javaAssertions.hpp"
1879N/A#include "classfile/javaClasses.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "classfile/vmSymbols.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "memory/oopFactory.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/handles.inline.hpp"
0N/A
0N/Abool JavaAssertions::_userDefault = false;
0N/Abool JavaAssertions::_sysDefault = false;
0N/AJavaAssertions::OptionList* JavaAssertions::_classes = 0;
0N/AJavaAssertions::OptionList* JavaAssertions::_packages = 0;
0N/A
0N/AJavaAssertions::OptionList::OptionList(const char* name, bool enabled,
0N/A OptionList* next) {
0N/A assert(name != 0, "need a name");
0N/A _name = name;
0N/A _enabled = enabled;
0N/A _next = next;
0N/A}
0N/A
0N/Aint JavaAssertions::OptionList::count(OptionList* p) {
0N/A int rc;
0N/A for (rc = 0; p != 0; p = p->next(), ++rc) /* empty */;
0N/A return rc;
0N/A}
0N/A
0N/Avoid JavaAssertions::addOption(const char* name, bool enable) {
0N/A assert(name != 0, "must have a name");
0N/A
0N/A // Copy the name. The storage needs to exist for the the lifetime of the vm;
0N/A // it is never freed, so will be leaked (along with other option strings -
0N/A // e.g., bootclasspath) if a process creates/destroys multiple VMs.
0N/A int len = (int)strlen(name);
3863N/A char *name_copy = NEW_C_HEAP_ARRAY(char, len + 1, mtClass);
0N/A strcpy(name_copy, name);
0N/A
0N/A // Figure out which list the new item should go on. Names that end in "..."
0N/A // go on the package tree list.
0N/A OptionList** head = &_classes;
0N/A if (len >= 3 && strcmp(name_copy + len - 3, "...") == 0) {
0N/A // Delete the "...".
0N/A len -= 3;
0N/A name_copy[len] = '\0';
0N/A head = &_packages;
0N/A }
0N/A
0N/A // Convert class/package names to internal format. Will have to convert back
0N/A // when copying to java in createJavaAssertionStatusDirectives, but that
0N/A // should happen only once. Alternative would require that
0N/A // JVM_DesiredAssertionStatus pass the external_name() to
0N/A // JavaAssertion::enabled(), but that is done once per loaded class.
0N/A for (int i = 0; i < len; ++i) {
0N/A if (name_copy[i] == '.') name_copy[i] = '/';
0N/A }
0N/A
0N/A if (TraceJavaAssertions) {
0N/A tty->print_cr("JavaAssertions: adding %s %s=%d",
0N/A head == &_classes ? "class" : "package",
0N/A name_copy[0] != '\0' ? name_copy : "'default'",
0N/A enable);
0N/A }
0N/A
0N/A // Prepend a new item to the list. Items added later take precedence, so
0N/A // prepending allows us to stop searching the list after the first match.
0N/A *head = new OptionList(name_copy, enable, *head);
0N/A}
0N/A
0N/Aoop JavaAssertions::createAssertionStatusDirectives(TRAPS) {
2062N/A Symbol* asd_sym = vmSymbols::java_lang_AssertionStatusDirectives();
0N/A klassOop k = SystemDictionary::resolve_or_fail(asd_sym, true, CHECK_NULL);
0N/A instanceKlassHandle asd_klass (THREAD, k);
0N/A asd_klass->initialize(CHECK_NULL);
0N/A Handle h = asd_klass->allocate_instance_handle(CHECK_NULL);
0N/A
0N/A int len;
0N/A typeArrayOop t;
0N/A len = OptionList::count(_packages);
1142N/A objArrayOop pn = oopFactory::new_objArray(SystemDictionary::String_klass(), len, CHECK_NULL);
0N/A objArrayHandle pkgNames (THREAD, pn);
0N/A t = oopFactory::new_typeArray(T_BOOLEAN, len, CHECK_NULL);
0N/A typeArrayHandle pkgEnabled(THREAD, t);
0N/A fillJavaArrays(_packages, len, pkgNames, pkgEnabled, CHECK_NULL);
0N/A
0N/A len = OptionList::count(_classes);
1142N/A objArrayOop cn = oopFactory::new_objArray(SystemDictionary::String_klass(), len, CHECK_NULL);
0N/A objArrayHandle classNames (THREAD, cn);
0N/A t = oopFactory::new_typeArray(T_BOOLEAN, len, CHECK_NULL);
0N/A typeArrayHandle classEnabled(THREAD, t);
0N/A fillJavaArrays(_classes, len, classNames, classEnabled, CHECK_NULL);
0N/A
0N/A java_lang_AssertionStatusDirectives::set_packages(h(), pkgNames());
0N/A java_lang_AssertionStatusDirectives::set_packageEnabled(h(), pkgEnabled());
0N/A java_lang_AssertionStatusDirectives::set_classes(h(), classNames());
0N/A java_lang_AssertionStatusDirectives::set_classEnabled(h(), classEnabled());
0N/A java_lang_AssertionStatusDirectives::set_deflt(h(), userClassDefault());
0N/A return h();
0N/A}
0N/A
0N/Avoid JavaAssertions::fillJavaArrays(const OptionList* p, int len,
0N/AobjArrayHandle names, typeArrayHandle enabled, TRAPS) {
0N/A // Fill in the parallel names and enabled (boolean) arrays. Start at the end
0N/A // of the array and work backwards, so the order of items in the arrays
0N/A // matches the order on the command line (the list is in reverse order, since
0N/A // it was created by prepending successive items from the command line).
0N/A int index;
0N/A for (index = len - 1; p != 0; p = p->next(), --index) {
0N/A assert(index >= 0, "length does not match list");
0N/A Handle s = java_lang_String::create_from_str(p->name(), CHECK);
0N/A s = java_lang_String::char_converter(s, '/', '.', CHECK);
0N/A names->obj_at_put(index, s());
0N/A enabled->bool_at_put(index, p->enabled());
0N/A }
0N/A assert(index == -1, "length does not match list");
0N/A}
0N/A
0N/Ainline JavaAssertions::OptionList*
0N/AJavaAssertions::match_class(const char* classname) {
0N/A for (OptionList* p = _classes; p != 0; p = p->next()) {
0N/A if (strcmp(p->name(), classname) == 0) {
0N/A return p;
0N/A }
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/AJavaAssertions::OptionList*
0N/AJavaAssertions::match_package(const char* classname) {
0N/A // Search the package list for any items that apply to classname. Each
0N/A // sub-package in classname is checked, from most-specific to least, until one
0N/A // is found.
0N/A if (_packages == 0) return 0;
0N/A
0N/A // Find the length of the "most-specific" package in classname. If classname
0N/A // does not include a package, length will be 0 which will match items for the
0N/A // default package (from options "-ea:..." or "-da:...").
0N/A size_t len = strlen(classname);
0N/A for (/* empty */; len > 0 && classname[len] != '/'; --len) /* empty */;
0N/A
0N/A do {
0N/A assert(len == 0 || classname[len] == '/', "not a package name");
0N/A for (OptionList* p = _packages; p != 0; p = p->next()) {
0N/A if (strncmp(p->name(), classname, len) == 0 && p->name()[len] == '\0') {
0N/A return p;
0N/A }
0N/A }
0N/A
0N/A // Find the length of the next package, taking care to avoid decrementing
0N/A // past 0 (len is unsigned).
0N/A while (len > 0 && classname[--len] != '/') /* empty */;
0N/A } while (len > 0);
0N/A
0N/A return 0;
0N/A}
0N/A
0N/Ainline void JavaAssertions::trace(const char* name,
0N/Aconst char* typefound, const char* namefound, bool enabled) {
0N/A if (TraceJavaAssertions) {
0N/A tty->print_cr("JavaAssertions: search for %s found %s %s=%d",
0N/A name, typefound, namefound[0] != '\0' ? namefound : "'default'", enabled);
0N/A }
0N/A}
0N/A
0N/Abool JavaAssertions::enabled(const char* classname, bool systemClass) {
0N/A assert(classname != 0, "must have a classname");
0N/A
0N/A // This will be slow if the number of assertion options on the command line is
0N/A // large--it traverses two lists, one of them multiple times. Could use a
0N/A // single n-ary tree instead of lists if someone ever notices.
0N/A
0N/A // First check options that apply to classes. If we find a match we're done.
0N/A OptionList* p;
0N/A if (p = match_class(classname)) {
0N/A trace(classname, "class", p->name(), p->enabled());
0N/A return p->enabled();
0N/A }
0N/A
0N/A // Now check packages, from most specific to least.
0N/A if (p = match_package(classname)) {
0N/A trace(classname, "package", p->name(), p->enabled());
0N/A return p->enabled();
0N/A }
0N/A
0N/A // No match. Return the default status.
0N/A bool result = systemClass ? systemClassDefault() : userClassDefault();
0N/A trace(classname, systemClass ? "system" : "user", "default", result);
0N/A return result;
0N/A}