View Javadoc

1   /**
2    * Copyright (2008) Schibsted Søk AS
3    * This file is part of Sesat Commons.
4    *
5    *   Sesat Commons is free software: you can redistribute it and/or modify
6    *   it under the terms of the GNU Lesser General Public License as published by
7    *   the Free Software Foundation, either version 3 of the License, or
8    *   (at your option) any later version.
9    *
10   *   Sesat Commons is distributed in the hope that it will be useful,
11   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *   GNU Lesser General Public License for more details.
14   *
15   *   You should have received a copy of the GNU Lesser General Public License
16   *   along with Sesat Commons.  If not, see <http://www.gnu.org/licenses/>.
17   *
18   */
19  
20  package no.sesat;
21  
22  import java.lang.instrument.*;
23  
24  import no.sesat.Interpreter;
25  
26  public class Agent {
27      public static void premain(String agentArgs, final Instrumentation inst) {
28          Interpreter.addFunction("loaded-classes", new Interpreter.Function() {
29              public String execute(final Interpreter.Context ctx) {
30                  String res = "Loaded:\n";
31                  for (Class<?> c : inst.getAllLoadedClasses()) {
32                      if (ctx.length() == 1 && !c.getName().matches(ctx.getArgument(0))) {
33                          continue;
34                      }
35                      long size = inst.getObjectSize(c);
36                      res += c.getName() + " - " + size + "\n" + c.getClassLoader() + "\n\n";
37                  }
38                  return res;
39              }
40  
41              public String describe() {
42                  return "List loaded classes. 'loaded-classes [regexp]'";
43              }
44          });
45      }
46  }