This repository has been archived by the owner on Mar 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
/
House.scala
129 lines (105 loc) · 4.4 KB
/
House.scala
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
/*
* Copyright 2013 zhongl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.zhongl.housemd.house
import com.sun.tools.attach.VirtualMachine
import com.github.zhongl.yascli.{PrintOut, Command, Application}
import jline.NoInterruptUnixTerminal
import com.github.zhongl.housemd._
import command._
import misc.Utils._
import duck.Telephone
import management.ManagementFactory
import java.io.{FileInputStream, FileWriter, BufferedWriter, File}
import java.util.jar.{Attributes, JarInputStream}
/**
* @author <a href="mailto:[email protected]">zhongl<a>
*/
object House extends Command("housemd", "a runtime diagnosis tool of JVM.", PrintOut(System.out)) with Application {
implicit private val string2Port = {
value: String =>
val p = value.toInt
if (p > 1024 && p < 65536) p else throw new IllegalArgumentException(", it should be between 1025 and 65535")
}
implicit private val string2File = {
value: String =>
val file = new File(value)
if (file.exists() && file.isFile) file else throw new IllegalArgumentException(", it should be an existed file")
}
private val port = option[Int]("-p" :: "--port" :: Nil, "set console local socket server port number.", 54321)
private val pid = parameter[String]("pid", "id of process to be diagnosing.")
private val printVersion = flag("-v" :: "--version" :: Nil, "show version.")
private lazy val agentJarFile = sourceOf(Manifest.classType(getClass))
private lazy val agentOptions = agentJarFile ::
classNameOf[Telephone] ::
port() ::
classNameOf[Trace] ::
classNameOf[Loaded] ::
classNameOf[Env] ::
classNameOf[Inspect] ::
classNameOf[Prop] ::
classNameOf[Resources] ::
Nil
private lazy val errorDetailFile = "/tmp/housemd.err." + pid()
private lazy val errorDetailWriter = new BufferedWriter(new FileWriter(errorDetailFile))
def run() {
if (printVersion()) {
println("v" + version)
return
}
if (ManagementFactory.getOperatingSystemMXBean.getName.toLowerCase.contains("window")) {
throw new IllegalStateException("Sorry, Windows is not supported now.")
}
try {
val terminal = new NoInterruptUnixTerminal()
terminal.init()
val sout = terminal.wrapOutIfNeeded(System.out)
val sin = terminal.wrapInIfNeeded(System.in)
val vm = VirtualMachine.attach(pid())
val mobilephone = new Mobilephone(port(), {
case PickUp => info("connection established on " + port())
case ListenTo(earphone) => earphone(sout)
case SpeakTo(microphone) => microphone(sin)
case BreakOff(reason) => error("connection breaked causeby"); error(reason)
case HangUp => terminal.restore(); silentClose(errorDetailWriter); info("bye")
})
info("Welcome to HouseMD " + version)
vm.loadAgent(agentJarFile, agentOptions mkString " ")
vm.detach()
mobilephone.start()
} catch {
case e: Throwable => error(e); silentClose(errorDetailWriter)
}
}
override def help = "version: " + version + '\n' + super.help
private lazy val version = {
val stream = new JarInputStream(new FileInputStream(agentJarFile))
try {
val attributes = stream.getManifest.getMainAttributes
attributes.getValue(Attributes.Name.SIGNATURE_VERSION)
} finally {
silentClose(stream)
}
}
override def error(a: Any) {
super.error(a)
a match {
case throwable: Throwable =>
super.error("You can get more details in " + errorDetailFile)
throwable.getStackTrace foreach { s => errorDetailWriter.write(s + "\n") }
case _ =>
}
}
}