From cb739fd3b2d5b3ce633b9c92520ac78d7aab3492 Mon Sep 17 00:00:00 2001 From: Jason Zavaglia Date: Tue, 5 Sep 2017 14:59:38 +1000 Subject: [PATCH] extend --profile cmd line option to allow interface to be specified Closes #691 --- config.go | 29 +++++++++++++++++++++++------ dcrd.go | 5 ++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/config.go b/config.go index 4d69466956..b3ef7dccfb 100644 --- a/config.go +++ b/config.go @@ -126,7 +126,7 @@ type config struct { SimNet bool `long:"simnet" description:"Use the simulation test network"` DisableCheckpoints bool `long:"nocheckpoints" description:"Disable built-in checkpoints. Don't do this unless you know what you're doing."` DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"` - Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"` + Profile string `long:"profile" description:"Enable HTTP profiling on given [addr:]port -- NOTE port must be between 1024 and 65536"` CPUProfile string `long:"cpuprofile" description:"Write CPU profile to the specified file"` MemProfile string `long:"memprofile" description:"Write mem profile to the specified file"` DumpBlockchain string `long:"dumpblockchain" description:"Write blockchain as a flat file of blocks for use with addblock, to the specified filename"` @@ -636,12 +636,29 @@ func loadConfig() (*config, []string, error) { return nil, nil, err } - // Validate profile port number + // Validate format of profile, can be an address:port, or just a port. if cfg.Profile != "" { - profilePort, err := strconv.Atoi(cfg.Profile) - if err != nil || profilePort < 1024 || profilePort > 65535 { - str := "%s: The profile port must be between 1024 and 65535" - err := fmt.Errorf(str, funcName) + // if profile is just a number, then add a default host of "127.0.0.1" such that Profile is a valid tcp address + if _, err := strconv.Atoi(cfg.Profile); err == nil { + cfg.Profile = net.JoinHostPort("127.0.0.1", cfg.Profile) + } + + // check the Profile is a valid address + _, portStr, err := net.SplitHostPort(cfg.Profile) + + if err != nil { + str := "%s: profile: %s" + err := fmt.Errorf(str, funcName, err) + fmt.Fprintln(os.Stderr, err) + fmt.Fprintln(os.Stderr, usageMessage) + return nil, nil, err + } + + // finally, check the port is in range + port, _ := strconv.Atoi(portStr) + if port < 1024 || port > 65535 { + str := "%s: profile: address %s: port must be between 1024 and 65535" + err := fmt.Errorf(str, funcName, cfg.Profile) fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, usageMessage) return nil, nil, err diff --git a/dcrd.go b/dcrd.go index 6134ab01c5..af2e4e222b 100644 --- a/dcrd.go +++ b/dcrd.go @@ -7,7 +7,6 @@ package main import ( "fmt" - "net" "net/http" _ "net/http/pprof" "os" @@ -58,9 +57,9 @@ func dcrdMain(serverChan chan<- *server) error { // Enable http profiling server if requested. if cfg.Profile != "" { go func() { - listenAddr := net.JoinHostPort("", cfg.Profile) + listenAddr := cfg.Profile dcrdLog.Infof("Creating profiling server "+ - "listening on %s", listenAddr) + "listening on %s", cfg.Profile) profileRedirect := http.RedirectHandler("/debug/pprof", http.StatusSeeOther) http.Handle("/", profileRedirect)