Skip to content

Commit

Permalink
extend --profile cmd line option to allow interface to be specified
Browse files Browse the repository at this point in the history
Closes #691
  • Loading branch information
jpz committed Sep 6, 2017
1 parent ba83738 commit cb739fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
29 changes: 23 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions dcrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main

import (
"fmt"
"net"
"net/http"
_ "net/http/pprof"
"os"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cb739fd

Please sign in to comment.