compilerOracle.cpp revision 3465:d2a62e0f25eb
16059Samurai/*
26059Samurai * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
36059Samurai * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
46059Samurai *
56059Samurai * This code is free software; you can redistribute it and/or modify it
66059Samurai * under the terms of the GNU General Public License version 2 only, as
76059Samurai * published by the Free Software Foundation.
86059Samurai *
96059Samurai * This code is distributed in the hope that it will be useful, but WITHOUT
106059Samurai * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
116059Samurai * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
126059Samurai * version 2 for more details (a copy is included in the LICENSE file that
136059Samurai * accompanied this code).
146059Samurai *
156059Samurai * You should have received a copy of the GNU General Public License version
166059Samurai * 2 along with this work; if not, write to the Free Software Foundation,
176059Samurai * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
186059Samurai *
196059Samurai * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2036285Sbrian * or visit www.oracle.com if you need additional information or have any
218857Srgrimes * questions.
226059Samurai *
2330715Sbrian */
2436285Sbrian
256059Samurai#include "precompiled.hpp"
2631176Sbrian#include "compiler/compilerOracle.hpp"
2730715Sbrian#include "memory/allocation.inline.hpp"
2830715Sbrian#include "memory/oopFactory.hpp"
2930715Sbrian#include "memory/resourceArea.hpp"
3030715Sbrian#include "oops/klass.hpp"
3131176Sbrian#include "oops/methodOop.hpp"
3236285Sbrian#include "oops/oop.inline.hpp"
3336285Sbrian#include "oops/symbol.hpp"
3436285Sbrian#include "runtime/handles.inline.hpp"
3520287Swollman#include "runtime/jniHandles.hpp"
3620287Swollman
3730715Sbrianclass MethodMatcher : public CHeapObj<mtCompiler> {
386059Samurai public:
396059Samurai  enum Mode {
4030715Sbrian    Exact,
4136285Sbrian    Prefix = 1,
4220287Swollman    Suffix = 2,
4331343Sbrian    Substring = Prefix | Suffix,
4430715Sbrian    Any,
459439Samurai    Unknown = -1
4631070Sbrian  };
4736285Sbrian
4836285Sbrian protected:
4936285Sbrian  Symbol*        _class_name;
5036285Sbrian  Symbol*        _method_name;
5136285Sbrian  Symbol*        _signature;
5236285Sbrian  Mode           _class_mode;
5336285Sbrian  Mode           _method_mode;
5436285Sbrian  MethodMatcher* _next;
5536285Sbrian
5636285Sbrian  static bool match(Symbol* candidate, Symbol* match, Mode match_mode);
5731690Sbrian
5836285Sbrian  Symbol* class_name() const { return _class_name; }
5936285Sbrian  Symbol* method_name() const { return _method_name; }
6036285Sbrian  Symbol* signature() const { return _signature; }
6136285Sbrian
6230715Sbrian public:
6336285Sbrian  MethodMatcher(Symbol* class_name, Mode class_mode,
646059Samurai                Symbol* method_name, Mode method_mode,
656059Samurai                Symbol* signature, MethodMatcher* next);
6636285Sbrian  MethodMatcher(Symbol* class_name, Symbol* method_name, MethodMatcher* next);
6736285Sbrian
686059Samurai  // utility method
6931343Sbrian  MethodMatcher* find(methodHandle method) {
7031176Sbrian    Symbol* class_name  = Klass::cast(method->method_holder())->name();
7131176Sbrian    Symbol* method_name = method->name();
7231176Sbrian    for (MethodMatcher* current = this; current != NULL; current = current->_next) {
736059Samurai      if (match(class_name, current->class_name(), current->_class_mode) &&
7431176Sbrian          match(method_name, current->method_name(), current->_method_mode) &&
7531176Sbrian          (current->signature() == NULL || current->signature() == method->signature())) {
7631176Sbrian        return current;
7731541Sbrian      }
7831176Sbrian    }
7931541Sbrian    return NULL;
8031176Sbrian  }
8131541Sbrian
8231176Sbrian  bool match(methodHandle method) {
8331176Sbrian    return find(method) != NULL;
8431176Sbrian  }
8531176Sbrian
8631176Sbrian  MethodMatcher* next() const { return _next; }
8731176Sbrian
8831176Sbrian  static void print_symbol(Symbol* h, Mode mode) {
8931176Sbrian    ResourceMark rm;
9031176Sbrian
9131176Sbrian    if (mode == Suffix || mode == Substring || mode == Any) {
9231176Sbrian      tty->print("*");
9331176Sbrian    }
9431176Sbrian    if (mode != Any) {
9531176Sbrian      h->print_symbol_on(tty);
9631176Sbrian    }
9731176Sbrian    if (mode == Prefix || mode == Substring) {
9831791Sbrian      tty->print("*");
9931176Sbrian    }
10031176Sbrian  }
10131176Sbrian
10231176Sbrian  void print_base() {
10331176Sbrian    print_symbol(class_name(), _class_mode);
10431176Sbrian    tty->print(".");
10531176Sbrian    print_symbol(method_name(), _method_mode);
10631176Sbrian    if (signature() != NULL) {
10731176Sbrian      tty->print(" ");
10831176Sbrian      signature()->print_symbol_on(tty);
10931176Sbrian    }
11031176Sbrian  }
11131541Sbrian
11231541Sbrian  virtual void print() {
11334536Sbrian    print_base();
11434536Sbrian    tty->cr();
11531962Sbrian  }
11631541Sbrian};
11731541Sbrian
11831541SbrianMethodMatcher::MethodMatcher(Symbol* class_name, Symbol* method_name, MethodMatcher* next) {
11931541Sbrian  _class_name  = class_name;
12031541Sbrian  _method_name = method_name;
12131541Sbrian  _next        = next;
12231541Sbrian  _class_mode  = MethodMatcher::Exact;
12331541Sbrian  _method_mode = MethodMatcher::Exact;
12431791Sbrian  _signature   = NULL;
12534536Sbrian}
12631541Sbrian
12736285Sbrian
12831541SbrianMethodMatcher::MethodMatcher(Symbol* class_name, Mode class_mode,
12931541Sbrian                             Symbol* method_name, Mode method_mode,
13031176Sbrian                             Symbol* signature, MethodMatcher* next):
13131176Sbrian    _class_mode(class_mode)
13231176Sbrian  , _method_mode(method_mode)
13331176Sbrian  , _next(next)
13431541Sbrian  , _class_name(class_name)
13531176Sbrian  , _method_name(method_name)
13626516Sbrian  , _signature(signature) {
13731176Sbrian}
13836285Sbrian
1396059Samuraibool MethodMatcher::match(Symbol* candidate, Symbol* match, Mode match_mode) {
1406059Samurai  if (match_mode == Any) {
14132663Sbrian    return true;
14231176Sbrian  }
14328679Sbrian
14431176Sbrian  if (match_mode == Exact) {
14531176Sbrian    return candidate == match;
14631176Sbrian  }
14731176Sbrian
14831176Sbrian  ResourceMark rm;
14931176Sbrian  const char * candidate_string = candidate->as_C_string();
15031176Sbrian  const char * match_string = match->as_C_string();
15131176Sbrian
15231176Sbrian  switch (match_mode) {
15331176Sbrian  case Prefix:
15431176Sbrian    return strstr(candidate_string, match_string) == candidate_string;
15531176Sbrian
15631176Sbrian  case Suffix: {
15731176Sbrian    size_t clen = strlen(candidate_string);
15831176Sbrian    size_t mlen = strlen(match_string);
15931739Sbrian    return clen >= mlen && strcmp(candidate_string + clen - mlen, match_string) == 0;
16031176Sbrian  }
16131739Sbrian
16231739Sbrian  case Substring:
16331176Sbrian    return strstr(candidate_string, match_string) != NULL;
16431739Sbrian
16531739Sbrian  default:
16631176Sbrian    return false;
16731739Sbrian  }
16831739Sbrian}
16931176Sbrian
17031176Sbrian
17131176Sbrianclass MethodOptionMatcher: public MethodMatcher {
1726059Samurai  const char * option;
1736059Samurai public:
17431739Sbrian  MethodOptionMatcher(Symbol* class_name, Mode class_mode,
17531739Sbrian                             Symbol* method_name, Mode method_mode,
17631739Sbrian                             Symbol* signature, const char * opt, MethodMatcher* next):
17731739Sbrian    MethodMatcher(class_name, class_mode, method_name, method_mode, signature, next) {
1786059Samurai    option = opt;
17936285Sbrian  }
1806059Samurai
18136285Sbrian  bool match(methodHandle method, const char* opt) {
18236285Sbrian    MethodOptionMatcher* current = this;
1836059Samurai    while (current != NULL) {
18436285Sbrian      current = (MethodOptionMatcher*)current->find(method);
18536285Sbrian      if (current == NULL) {
18631827Sbrian        return false;
18736285Sbrian      }
18836285Sbrian      if (strcmp(current->option, opt) == 0) {
18936285Sbrian        return true;
19036285Sbrian      }
19136285Sbrian      current = current->next();
1926059Samurai    }
1936059Samurai    return false;
19432616Sbrian  }
19531176Sbrian
19631176Sbrian  MethodOptionMatcher* next() {
19732425Sbrian    return (MethodOptionMatcher*)_next;
19831354Sbrian  }
19931176Sbrian
20031176Sbrian  virtual void print() {
20132425Sbrian    print_base();
20232021Sbrian    tty->print(" %s", option);
20331176Sbrian    tty->cr();
20431176Sbrian  }
20531176Sbrian};
20631176Sbrian
20731176Sbrian
20831176Sbrian
20931176Sbrian// this must parallel the command_names below
21031176Sbrianenum OracleCommand {
21131176Sbrian  UnknownCommand = -1,
21231176Sbrian  OracleFirstCommand = 0,
21331176Sbrian  BreakCommand = OracleFirstCommand,
21431176Sbrian  PrintCommand,
21536285Sbrian  ExcludeCommand,
21631176Sbrian  InlineCommand,
21731176Sbrian  DontInlineCommand,
21831176Sbrian  CompileOnlyCommand,
21931176Sbrian  LogCommand,
22031176Sbrian  OptionCommand,
22131176Sbrian  QuietCommand,
22231176Sbrian  HelpCommand,
22331176Sbrian  OracleCommandCount
22431176Sbrian};
22531176Sbrian
22632425Sbrian// this must parallel the enum OracleCommand
22731354Sbrianstatic const char * command_names[] = {
22831176Sbrian  "break",
22931176Sbrian  "print",
23032425Sbrian  "exclude",
23132425Sbrian  "inline",
23232425Sbrian  "dontinline",
23332425Sbrian  "compileonly",
23432425Sbrian  "log",
23532425Sbrian  "option",
23632425Sbrian  "quiet",
23732425Sbrian  "help"
23832425Sbrian};
23936285Sbrian
24032425Sbrianstatic const char * command_name(OracleCommand command) {
24132425Sbrian  if (command < OracleFirstCommand || command >= OracleCommandCount) {
24232425Sbrian    return "unknown command";
24332425Sbrian  }
24432425Sbrian  return command_names[command];
24532425Sbrian}
24632425Sbrian
24732425Sbrianclass MethodMatcher;
24832425Sbrianstatic MethodMatcher* lists[OracleCommandCount] = { 0, };
24932425Sbrian
25032425Sbrian
25132425Sbrianstatic bool check_predicate(OracleCommand command, methodHandle method) {
25236285Sbrian  return ((lists[command] != NULL) &&
25336285Sbrian          !method.is_null() &&
25431354Sbrian          lists[command]->match(method));
25531176Sbrian}
25631176Sbrian
25731176Sbrian
25831176Sbrianstatic MethodMatcher* add_predicate(OracleCommand command,
25936285Sbrian                                    Symbol* class_name, MethodMatcher::Mode c_mode,
26031354Sbrian                                    Symbol* method_name, MethodMatcher::Mode m_mode,
26131354Sbrian                                    Symbol* signature) {
26236285Sbrian  assert(command != OptionCommand, "must use add_option_string");
26331354Sbrian  if (command == LogCommand && !LogCompilation && lists[LogCommand] == NULL)
26432425Sbrian    tty->print_cr("Warning:  +LogCompilation must be enabled in order for individual methods to be logged.");
26536285Sbrian  lists[command] = new MethodMatcher(class_name, c_mode, method_name, m_mode, signature, lists[command]);
26631354Sbrian  return lists[command];
26731354Sbrian}
26831354Sbrian
26932425Sbrian
27031176Sbrian
27131354Sbrianstatic MethodMatcher* add_option_string(Symbol* class_name, MethodMatcher::Mode c_mode,
27231354Sbrian                                        Symbol* method_name, MethodMatcher::Mode m_mode,
27331176Sbrian                                        Symbol* signature,
27431176Sbrian                                        const char* option) {
2756059Samurai  lists[OptionCommand] = new MethodOptionMatcher(class_name, c_mode, method_name, m_mode,
27636285Sbrian                                                 signature, option, lists[OptionCommand]);
2776059Samurai  return lists[OptionCommand];
2786059Samurai}
27931176Sbrian
28031176Sbrian
28132021Sbrianbool CompilerOracle::has_option_string(methodHandle method, const char* option) {
2826735Samurai  return lists[OptionCommand] != NULL &&
2836059Samurai    ((MethodOptionMatcher*)lists[OptionCommand])->match(method, option);
2846059Samurai}
2856059Samurai
2866735Samurai
2876735Samuraibool CompilerOracle::should_exclude(methodHandle method, bool& quietly) {
2886059Samurai  quietly = true;
2896735Samurai  if (lists[ExcludeCommand] != NULL) {
2906735Samurai    if (lists[ExcludeCommand]->match(method)) {
29136285Sbrian      quietly = _quiet;
29228679Sbrian      return true;
2936735Samurai    }
2946059Samurai  }
29528679Sbrian
2966059Samurai  if (lists[CompileOnlyCommand] != NULL) {
2976059Samurai    return !lists[CompileOnlyCommand]->match(method);
29828679Sbrian  }
2996735Samurai  return false;
30036285Sbrian}
30118885Sjkh
30228679Sbrian
3036735Samuraibool CompilerOracle::should_inline(methodHandle method) {
3046059Samurai  return (check_predicate(InlineCommand, method));
3056059Samurai}
30636285Sbrian
30736285Sbrian
3086059Samuraibool CompilerOracle::should_not_inline(methodHandle method) {
30928679Sbrian  return (check_predicate(DontInlineCommand, method));
31031176Sbrian}
31131176Sbrian
31231176Sbrian
31331176Sbrianbool CompilerOracle::should_print(methodHandle method) {
31431176Sbrian  return (check_predicate(PrintCommand, method));
31531176Sbrian}
31631176Sbrian
31731176Sbrian
31831176Sbrianbool CompilerOracle::should_log(methodHandle method) {
31931176Sbrian  if (!LogCompilation)            return false;
32031176Sbrian  if (lists[LogCommand] == NULL)  return true;  // by default, log all
32131176Sbrian  return (check_predicate(LogCommand, method));
32231176Sbrian}
32331176Sbrian
32431176Sbrian
32531176Sbrianbool CompilerOracle::should_break_at(methodHandle method) {
32631176Sbrian  return check_predicate(BreakCommand, method);
32731176Sbrian}
32831176Sbrian
32931176Sbrian
33036285Sbrianstatic OracleCommand parse_command_name(const char * line, int* bytes_read) {
33136285Sbrian  assert(ARRAY_SIZE(command_names) == OracleCommandCount,
33231176Sbrian         "command_names size mismatch");
33336285Sbrian
33436285Sbrian  *bytes_read = 0;
3356059Samurai  char command[33];
33618885Sjkh  int result = sscanf(line, "%32[a-z]%n", command, bytes_read);
33726516Sbrian  for (uint i = 0; i < ARRAY_SIZE(command_names); i++) {
3386059Samurai    if (strcmp(command, command_names[i]) == 0) {
3396059Samurai      return (OracleCommand)i;
3406059Samurai    }
3416059Samurai  }
3426059Samurai  return UnknownCommand;
3436059Samurai}
34436285Sbrian
3456059Samurai
3466059Samuraistatic void usage() {
3476059Samurai  tty->print_cr("  CompileCommand and the CompilerOracle allows simple control over");
34831598Sbrian  tty->print_cr("  what's allowed to be compiled.  The standard supported directives");
34932021Sbrian  tty->print_cr("  are exclude and compileonly.  The exclude directive stops a method");
35032021Sbrian  tty->print_cr("  from being compiled and compileonly excludes all methods except for");
3516059Samurai  tty->print_cr("  the ones mentioned by compileonly directives.  The basic form of");
3526735Samurai  tty->print_cr("  all commands is a command name followed by the name of the method");
3536059Samurai  tty->print_cr("  in one of two forms: the standard class file format as in");
35436285Sbrian  tty->print_cr("  class/name.methodName or the PrintCompilation format");
35531598Sbrian  tty->print_cr("  class.name::methodName.  The method name can optionally be followed");
35626516Sbrian  tty->print_cr("  by a space then the signature of the method in the class file");
3576059Samurai  tty->print_cr("  format.  Otherwise the directive applies to all methods with the");
3586059Samurai  tty->print_cr("  same name and class regardless of signature.  Leading and trailing");
3596735Samurai  tty->print_cr("  *'s in the class and/or method name allows a small amount of");
3606735Samurai  tty->print_cr("  wildcarding.  ");
3616059Samurai  tty->cr();
3626735Samurai  tty->print_cr("  Examples:");
3636735Samurai  tty->cr();
36436285Sbrian  tty->print_cr("  exclude java/lang/StringBuffer.append");
36528974Sbrian  tty->print_cr("  compileonly java/lang/StringBuffer.toString ()Ljava/lang/String;");
3666735Samurai  tty->print_cr("  exclude java/lang/String*.*");
3676735Samurai  tty->print_cr("  exclude *.toString");
3686059Samurai}
3696059Samurai
3706059Samurai
3716059Samurai// The characters allowed in a class or method name.  All characters > 0x7f
3726059Samurai// are allowed in order to handle obfuscated class files (e.g. Volano)
3736059Samurai#define RANGEBASE "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_<>" \
3746059Samurai        "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" \
3756735Samurai        "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" \
37636285Sbrian        "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" \
37728974Sbrian        "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" \
3786059Samurai        "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" \
3796059Samurai        "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" \
3806059Samurai        "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" \
3816059Samurai        "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
3826059Samurai
38331739Sbrian#define RANGE0 "[*" RANGEBASE "]"
38431739Sbrian#define RANGEDOT "[*" RANGEBASE ".]"
38531739Sbrian#define RANGESLASH "[*" RANGEBASE "/]"
38631739Sbrian
38731739Sbrian
38836285Sbrian// Accept several syntaxes for these patterns
38936285Sbrian//  original syntax
39031739Sbrian//   cmd  java.lang.String foo
39131739Sbrian//  PrintCompilation syntax
39231739Sbrian//   cmd  java.lang.String::foo
39331739Sbrian//  VM syntax
39431739Sbrian//   cmd  java/lang/String[. ]foo
39531739Sbrian//
39631739Sbrian
39736285Sbrianstatic const char* patterns[] = {
39831739Sbrian  "%*[ \t]%255" RANGEDOT    " "     "%255"  RANGE0 "%n",
39931739Sbrian  "%*[ \t]%255" RANGEDOT   "::"     "%255"  RANGE0 "%n",
40031739Sbrian  "%*[ \t]%255" RANGESLASH "%*[ .]" "%255"  RANGE0 "%n",
40131739Sbrian};
40236285Sbrian
40331739Sbrianstatic MethodMatcher::Mode check_mode(char name[], const char*& error_msg) {
40431739Sbrian  int match = MethodMatcher::Exact;
40531739Sbrian  while (name[0] == '*') {
40631739Sbrian    match |= MethodMatcher::Suffix;
40731739Sbrian    strcpy(name, name + 1);
40831739Sbrian  }
40936285Sbrian
41036285Sbrian  if (strcmp(name, "*") == 0) return MethodMatcher::Any;
41131739Sbrian
41236285Sbrian  size_t len = strlen(name);
41331739Sbrian  while (len > 0 && name[len - 1] == '*') {
41436285Sbrian    match |= MethodMatcher::Prefix;
41536285Sbrian    name[--len] = '\0';
41631739Sbrian  }
41731739Sbrian
4186059Samurai  if (strstr(name, "*") != NULL) {
4196059Samurai    error_msg = "  Embedded * not allowed";
4206059Samurai    return MethodMatcher::Unknown;
4216059Samurai  }
4226059Samurai  return (MethodMatcher::Mode)match;
4236059Samurai}
42428679Sbrian
4256059Samuraistatic bool scan_line(const char * line,
42631176Sbrian                      char class_name[],  MethodMatcher::Mode* c_mode,
42731343Sbrian                      char method_name[], MethodMatcher::Mode* m_mode,
42828679Sbrian                      int* bytes_read, const char*& error_msg) {
42931354Sbrian  *bytes_read = 0;
43031176Sbrian  error_msg = NULL;
43131176Sbrian  for (uint i = 0; i < ARRAY_SIZE(patterns); i++) {
43236285Sbrian    if (2 == sscanf(line, patterns[i], class_name, method_name, bytes_read)) {
43331176Sbrian      *c_mode = check_mode(class_name, error_msg);
43431176Sbrian      *m_mode = check_mode(method_name, error_msg);
43531176Sbrian      return *c_mode != MethodMatcher::Unknown && *m_mode != MethodMatcher::Unknown;
4366059Samurai    }
43731690Sbrian  }
43836285Sbrian  return false;
43936285Sbrian}
44036285Sbrian
44131690Sbrian
44236285Sbrian
44331690Sbrianvoid CompilerOracle::parse_from_line(char* line) {
44436285Sbrian  if (line[0] == '\0') return;
44536285Sbrian  if (line[0] == '#')  return;
44636285Sbrian
44736285Sbrian  bool have_colon = (strstr(line, "::") != NULL);
44836285Sbrian  for (char* lp = line; *lp != '\0'; lp++) {
44936285Sbrian    // Allow '.' to separate the class name from the method name.
45036285Sbrian    // This is the preferred spelling of methods:
45136285Sbrian    //      exclude java/lang/String.indexOf(I)I
45236285Sbrian    // Allow ',' for spaces (eases command line quoting).
45336285Sbrian    //      exclude,java/lang/String.indexOf
45436285Sbrian    // For backward compatibility, allow space as separator also.
45536285Sbrian    //      exclude java/lang/String indexOf
45636285Sbrian    //      exclude,java/lang/String,indexOf
45736285Sbrian    // For easy cut-and-paste of method names, allow VM output format
45836285Sbrian    // as produced by methodOopDesc::print_short_name:
45936285Sbrian    //      exclude java.lang.String::indexOf
46036285Sbrian    // For simple implementation convenience here, convert them all to space.
46136285Sbrian    if (have_colon) {
46236285Sbrian      if (*lp == '.')  *lp = '/';   // dots build the package prefix
46336285Sbrian      if (*lp == ':')  *lp = ' ';
46436285Sbrian    }
46536285Sbrian    if (*lp == ',' || *lp == '.')  *lp = ' ';
46636285Sbrian  }
46736285Sbrian
46836285Sbrian  char* original_line = line;
46936285Sbrian  int bytes_read;
47036285Sbrian  OracleCommand command = parse_command_name(line, &bytes_read);
47136285Sbrian  line += bytes_read;
47236285Sbrian
47336285Sbrian  if (command == UnknownCommand) {
47436285Sbrian    tty->print_cr("CompilerOracle: unrecognized line");
47536285Sbrian    tty->print_cr("  \"%s\"", original_line);
47636285Sbrian    return;
47736285Sbrian  }
47836285Sbrian
47936285Sbrian  if (command == QuietCommand) {
48036285Sbrian    _quiet = true;
48136285Sbrian    return;
48236285Sbrian  }
48336285Sbrian
48436285Sbrian  if (command == HelpCommand) {
48536285Sbrian    usage();
48636285Sbrian    return;
48736285Sbrian  }
48836285Sbrian
48936285Sbrian  MethodMatcher::Mode c_match = MethodMatcher::Exact;
49036285Sbrian  MethodMatcher::Mode m_match = MethodMatcher::Exact;
49136285Sbrian  char class_name[256];
49236285Sbrian  char method_name[256];
49336285Sbrian  char sig[1024];
49436285Sbrian  char errorbuf[1024];
49536285Sbrian  const char* error_msg = NULL;
49636285Sbrian  MethodMatcher* match = NULL;
49736285Sbrian
49836285Sbrian  if (scan_line(line, class_name, &c_match, method_name, &m_match, &bytes_read, error_msg)) {
49936285Sbrian    EXCEPTION_MARK;
50036285Sbrian    Symbol* c_name = SymbolTable::new_symbol(class_name, CHECK);
50136285Sbrian    Symbol* m_name = SymbolTable::new_symbol(method_name, CHECK);
50236285Sbrian    Symbol* signature = NULL;
50336285Sbrian
50436285Sbrian    line += bytes_read;
50536285Sbrian    // there might be a signature following the method.
50636285Sbrian    // signatures always begin with ( so match that by hand
50736285Sbrian    if (1 == sscanf(line, "%*[ \t](%254[[);/" RANGEBASE "]%n", sig + 1, &bytes_read)) {
50836285Sbrian      sig[0] = '(';
50936285Sbrian      line += bytes_read;
51036285Sbrian      signature = SymbolTable::new_symbol(sig, CHECK);
51136285Sbrian    }
51236285Sbrian
51336285Sbrian    if (command == OptionCommand) {
51436285Sbrian      // Look for trailing options to support
51536285Sbrian      // ciMethod::has_option("string") to control features in the
51636285Sbrian      // compiler.  Multiple options may follow the method name.
51731690Sbrian      char option[256];
51831690Sbrian      while (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", option, &bytes_read) == 1) {
51931690Sbrian        if (match != NULL && !_quiet) {
52036285Sbrian          // Print out the last match added
52131690Sbrian          tty->print("CompilerOracle: %s ", command_names[command]);
52236285Sbrian          match->print();
52336285Sbrian        }
52436285Sbrian        match = add_option_string(c_name, c_match, m_name, m_match, signature, strdup(option));
52536285Sbrian        line += bytes_read;
52636285Sbrian      }
52736285Sbrian    } else {
52836285Sbrian      bytes_read = 0;
52936285Sbrian      sscanf(line, "%*[ \t]%n", &bytes_read);
53031690Sbrian      if (line[bytes_read] != '\0') {
53136285Sbrian        jio_snprintf(errorbuf, sizeof(errorbuf), "  Unrecognized text after command: %s", line);
53236285Sbrian        error_msg = errorbuf;
53331690Sbrian      } else {
53436285Sbrian        match = add_predicate(command, c_name, c_match, m_name, m_match, signature);
53536285Sbrian      }
53636285Sbrian    }
53736285Sbrian  }
53836285Sbrian
53936285Sbrian  if (match != NULL) {
54036285Sbrian    if (!_quiet) {
54136285Sbrian      tty->print("CompilerOracle: %s ", command_names[command]);
54236285Sbrian      match->print();
54336285Sbrian    }
54436285Sbrian  } else {
54536285Sbrian    tty->print_cr("CompilerOracle: unrecognized line");
54636285Sbrian    tty->print_cr("  \"%s\"", original_line);
54736285Sbrian    if (error_msg != NULL) {
54836285Sbrian      tty->print_cr(error_msg);
54936285Sbrian    }
55036285Sbrian  }
55136285Sbrian}
55236285Sbrian
55336285Sbrianstatic const char* cc_file() {
55436285Sbrian  if (CompileCommandFile == NULL)
55536285Sbrian    return ".hotspot_compiler";
55636285Sbrian  return CompileCommandFile;
55736285Sbrian}
55836285Sbrianbool CompilerOracle::_quiet = false;
55936285Sbrian
56036285Sbrianvoid CompilerOracle::parse_from_file() {
56131690Sbrian  FILE* stream = fopen(cc_file(), "rt");
562  if (stream == NULL) return;
563
564  char token[1024];
565  int  pos = 0;
566  int  c = getc(stream);
567  while(c != EOF) {
568    if (c == '\n') {
569      token[pos++] = '\0';
570      parse_from_line(token);
571      pos = 0;
572    } else {
573      token[pos++] = c;
574    }
575    c = getc(stream);
576  }
577  token[pos++] = '\0';
578  parse_from_line(token);
579
580  fclose(stream);
581}
582
583void CompilerOracle::parse_from_string(const char* str, void (*parse_line)(char*)) {
584  char token[1024];
585  int  pos = 0;
586  const char* sp = str;
587  int  c = *sp++;
588  while (c != '\0') {
589    if (c == '\n') {
590      token[pos++] = '\0';
591      parse_line(token);
592      pos = 0;
593    } else {
594      token[pos++] = c;
595    }
596    c = *sp++;
597  }
598  token[pos++] = '\0';
599  parse_line(token);
600}
601
602void CompilerOracle::append_comment_to_file(const char* message) {
603  fileStream stream(fopen(cc_file(), "at"));
604  stream.print("# ");
605  for (int index = 0; message[index] != '\0'; index++) {
606    stream.put(message[index]);
607    if (message[index] == '\n') stream.print("# ");
608  }
609  stream.cr();
610}
611
612void CompilerOracle::append_exclude_to_file(methodHandle method) {
613  fileStream stream(fopen(cc_file(), "at"));
614  stream.print("exclude ");
615  Klass::cast(method->method_holder())->name()->print_symbol_on(&stream);
616  stream.print(".");
617  method->name()->print_symbol_on(&stream);
618  method->signature()->print_symbol_on(&stream);
619  stream.cr();
620  stream.cr();
621}
622
623
624void compilerOracle_init() {
625  CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line);
626  CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only);
627  CompilerOracle::parse_from_file();
628  if (lists[PrintCommand] != NULL) {
629    if (PrintAssembly) {
630      warning("CompileCommand and/or .hotspot_compiler file contains 'print' commands, but PrintAssembly is also enabled");
631    } else if (FLAG_IS_DEFAULT(DebugNonSafepoints)) {
632      warning("printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output");
633      DebugNonSafepoints = true;
634    }
635  }
636}
637
638
639void CompilerOracle::parse_compile_only(char * line) {
640  int i;
641  char name[1024];
642  const char* className = NULL;
643  const char* methodName = NULL;
644
645  bool have_colon = (strstr(line, "::") != NULL);
646  char method_sep = have_colon ? ':' : '.';
647
648  if (Verbose) {
649    tty->print_cr(line);
650  }
651
652  ResourceMark rm;
653  while (*line != '\0') {
654    MethodMatcher::Mode c_match = MethodMatcher::Exact;
655    MethodMatcher::Mode m_match = MethodMatcher::Exact;
656
657    for (i = 0;
658         i < 1024 && *line != '\0' && *line != method_sep && *line != ',' && !isspace(*line);
659         line++, i++) {
660      name[i] = *line;
661      if (name[i] == '.')  name[i] = '/';  // package prefix uses '/'
662    }
663
664    if (i > 0) {
665      char* newName = NEW_RESOURCE_ARRAY( char, i + 1);
666      if (newName == NULL)
667        return;
668      strncpy(newName, name, i);
669      newName[i] = '\0';
670
671      if (className == NULL) {
672        className = newName;
673        c_match = MethodMatcher::Prefix;
674      } else {
675        methodName = newName;
676      }
677    }
678
679    if (*line == method_sep) {
680      if (className == NULL) {
681        className = "";
682        c_match = MethodMatcher::Any;
683      } else {
684        // foo/bar.blah is an exact match on foo/bar, bar.blah is a suffix match on bar
685        if (strchr(className, '/') != NULL) {
686          c_match = MethodMatcher::Exact;
687        } else {
688          c_match = MethodMatcher::Suffix;
689        }
690      }
691    } else {
692      // got foo or foo/bar
693      if (className == NULL) {
694        ShouldNotReachHere();
695      } else {
696        // got foo or foo/bar
697        if (strchr(className, '/') != NULL) {
698          c_match = MethodMatcher::Prefix;
699        } else if (className[0] == '\0') {
700          c_match = MethodMatcher::Any;
701        } else {
702          c_match = MethodMatcher::Substring;
703        }
704      }
705    }
706
707    // each directive is terminated by , or NUL or . followed by NUL
708    if (*line == ',' || *line == '\0' || (line[0] == '.' && line[1] == '\0')) {
709      if (methodName == NULL) {
710        methodName = "";
711        if (*line != method_sep) {
712          m_match = MethodMatcher::Any;
713        }
714      }
715
716      EXCEPTION_MARK;
717      Symbol* c_name = SymbolTable::new_symbol(className, CHECK);
718      Symbol* m_name = SymbolTable::new_symbol(methodName, CHECK);
719      Symbol* signature = NULL;
720
721      add_predicate(CompileOnlyCommand, c_name, c_match, m_name, m_match, signature);
722      if (PrintVMOptions) {
723        tty->print("CompileOnly: compileonly ");
724        lists[CompileOnlyCommand]->print();
725      }
726
727      className = NULL;
728      methodName = NULL;
729    }
730
731    line = *line == '\0' ? line : line + 1;
732  }
733}
734