github.com/ice-blockchain/go/src@v0.0.0-20240403114104-1564d284e521/net/rpc/debug.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rpc 6 7 /* 8 Some HTML presented at http://machine:port/debug/rpc 9 Lists services, their methods, and some statistics, still rudimentary. 10 */ 11 12 import ( 13 "fmt" 14 "html/template" 15 "net/http" 16 "sort" 17 ) 18 19 const debugText = `<html> 20 <body> 21 <title>Services</title> 22 {{range .}} 23 <hr> 24 Service {{.Name}} 25 <hr> 26 <table> 27 <th align=center>Method</th><th align=center>Calls</th> 28 {{range .Method}} 29 <tr> 30 <td align=left font=fixed>{{.Name}}({{.Type.ArgType}}, {{.Type.ReplyType}}) error</td> 31 <td align=center>{{.Type.NumCalls}}</td> 32 </tr> 33 {{end}} 34 </table> 35 {{end}} 36 </body> 37 </html>` 38 39 var debug = template.Must(template.New("RPC debug").Parse(debugText)) 40 41 // If set, print log statements for internal and I/O errors. 42 var debugLog = false 43 44 type debugMethod struct { 45 Type *methodType 46 Name string 47 } 48 49 type methodArray []debugMethod 50 51 type debugService struct { 52 Service *service 53 Name string 54 Method methodArray 55 } 56 57 type serviceArray []debugService 58 59 func (s serviceArray) Len() int { return len(s) } 60 func (s serviceArray) Less(i, j int) bool { return s[i].Name < s[j].Name } 61 func (s serviceArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 62 63 func (m methodArray) Len() int { return len(m) } 64 func (m methodArray) Less(i, j int) bool { return m[i].Name < m[j].Name } 65 func (m methodArray) Swap(i, j int) { m[i], m[j] = m[j], m[i] } 66 67 type debugHTTP struct { 68 *Server 69 } 70 71 // Runs at /debug/rpc 72 func (server debugHTTP) ServeHTTP(w http.ResponseWriter, req *http.Request) { 73 // Build a sorted version of the data. 74 var services serviceArray 75 server.serviceMap.Range(func(snamei, svci any) bool { 76 svc := svci.(*service) 77 ds := debugService{svc, snamei.(string), make(methodArray, 0, len(svc.method))} 78 for mname, method := range svc.method { 79 ds.Method = append(ds.Method, debugMethod{method, mname}) 80 } 81 sort.Sort(ds.Method) 82 services = append(services, ds) 83 return true 84 }) 85 sort.Sort(services) 86 err := debug.Execute(w, services) 87 if err != nil { 88 fmt.Fprintln(w, "rpc: error executing template:", err.Error()) 89 } 90 }