暫無描述

commands.go 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package driver
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "os"
  20. "os/exec"
  21. "runtime"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "time"
  26. "github.com/google/pprof/internal/plugin"
  27. "github.com/google/pprof/internal/report"
  28. "github.com/google/pprof/third_party/svg"
  29. )
  30. // commands describes the commands accepted by pprof.
  31. type commands map[string]*command
  32. // command describes the actions for a pprof command. Includes a
  33. // function for command-line completion, the report format to use
  34. // during report generation, any postprocessing functions, and whether
  35. // the command expects a regexp parameter (typically a function name).
  36. type command struct {
  37. format int // report format to generate
  38. postProcess PostProcessor // postprocessing to run on report
  39. visualizer PostProcessor // display output using some callback
  40. hasParam bool // collect a parameter from the CLI
  41. description string // single-line description text saying what the command does
  42. usage string // multi-line help text saying how the command is used
  43. }
  44. // help returns a help string for a command.
  45. func (c *command) help(name string) string {
  46. message := c.description + "\n"
  47. if c.usage != "" {
  48. message += " Usage:\n"
  49. lines := strings.Split(c.usage, "\n")
  50. for _, line := range lines {
  51. message += fmt.Sprintf(" %s\n", line)
  52. }
  53. }
  54. return message + "\n"
  55. }
  56. // AddCommand adds an additional command to the set of commands
  57. // accepted by pprof. This enables extensions to add new commands for
  58. // specialized visualization formats. If the command specified already
  59. // exists, it is overwritten.
  60. func AddCommand(cmd string, format int, post PostProcessor, desc, usage string) {
  61. pprofCommands[cmd] = &command{format, post, nil, false, desc, usage}
  62. }
  63. // SetVariableDefault sets the default value for a pprof
  64. // variable. This enables extensions to set their own defaults.
  65. func SetVariableDefault(variable, value string) {
  66. if v := pprofVariables[variable]; v != nil {
  67. v.value = value
  68. }
  69. }
  70. // PostProcessor is a function that applies post-processing to the report output
  71. type PostProcessor func(input io.Reader, output io.Writer, ui plugin.UI) error
  72. // interactiveMode is true if pprof is running on interactive mode, reading
  73. // commands from its shell.
  74. var interactiveMode = false
  75. // pprofCommands are the report generation commands recognized by pprof.
  76. var pprofCommands = commands{
  77. // Commands that require no post-processing.
  78. "comments": {report.Comments, nil, nil, false, "Output all profile comments", ""},
  79. "disasm": {report.Dis, nil, nil, true, "Output assembly listings annotated with samples", listHelp("disasm", true)},
  80. "dot": {report.Dot, nil, nil, false, "Outputs a graph in DOT format", reportHelp("dot", false, true)},
  81. "list": {report.List, nil, nil, true, "Output annotated source for functions matching regexp", listHelp("list", false)},
  82. "peek": {report.Tree, nil, nil, true, "Output callers/callees of functions matching regexp", "peek func_regex\nDisplay callers and callees of functions matching func_regex."},
  83. "raw": {report.Raw, nil, nil, false, "Outputs a text representation of the raw profile", ""},
  84. "tags": {report.Tags, nil, nil, false, "Outputs all tags in the profile", "tags [tag_regex]* [-ignore_regex]* [>file]\nList tags with key:value matching tag_regex and exclude ignore_regex."},
  85. "text": {report.Text, nil, nil, false, "Outputs top entries in text form", reportHelp("text", true, true)},
  86. "top": {report.Text, nil, nil, false, "Outputs top entries in text form", reportHelp("top", true, true)},
  87. "traces": {report.Traces, nil, nil, false, "Outputs all profile samples in text form", ""},
  88. "tree": {report.Tree, nil, nil, false, "Outputs a text rendering of call graph", reportHelp("tree", true, true)},
  89. // Save binary formats to a file
  90. "callgrind": {report.Callgrind, nil, awayFromTTY("callgraph.out"), false, "Outputs a graph in callgrind format", reportHelp("callgrind", false, true)},
  91. "proto": {report.Proto, nil, awayFromTTY("pb.gz"), false, "Outputs the profile in compressed protobuf format", ""},
  92. "topproto": {report.TopProto, nil, awayFromTTY("pb.gz"), false, "Outputs top entries in compressed protobuf format", ""},
  93. // Generate report in DOT format and postprocess with dot
  94. "gif": {report.Dot, invokeDot("gif"), awayFromTTY("gif"), false, "Outputs a graph image in GIF format", reportHelp("gif", false, true)},
  95. "pdf": {report.Dot, invokeDot("pdf"), awayFromTTY("pdf"), false, "Outputs a graph in PDF format", reportHelp("pdf", false, true)},
  96. "png": {report.Dot, invokeDot("png"), awayFromTTY("png"), false, "Outputs a graph image in PNG format", reportHelp("png", false, true)},
  97. "ps": {report.Dot, invokeDot("ps"), awayFromTTY("ps"), false, "Outputs a graph in PS format", reportHelp("ps", false, true)},
  98. // Save SVG output into a file
  99. "svg": {report.Dot, massageDotSVG(), awayFromTTY("svg"), false, "Outputs a graph in SVG format", reportHelp("svg", false, true)},
  100. // Visualize postprocessed dot output
  101. "eog": {report.Dot, invokeDot("svg"), invokeVisualizer("svg", []string{"eog"}), false, "Visualize graph through eog", reportHelp("eog", false, false)},
  102. "evince": {report.Dot, invokeDot("pdf"), invokeVisualizer("pdf", []string{"evince"}), false, "Visualize graph through evince", reportHelp("evince", false, false)},
  103. "gv": {report.Dot, invokeDot("ps"), invokeVisualizer("ps", []string{"gv --noantialias"}), false, "Visualize graph through gv", reportHelp("gv", false, false)},
  104. "web": {report.Dot, massageDotSVG(), invokeVisualizer("svg", browsers()), false, "Visualize graph through web browser", reportHelp("web", false, false)},
  105. // Visualize callgrind output
  106. "kcachegrind": {report.Callgrind, nil, invokeVisualizer("grind", kcachegrind), false, "Visualize report in KCachegrind", reportHelp("kcachegrind", false, false)},
  107. // Visualize HTML directly generated by report.
  108. "weblist": {report.WebList, nil, invokeVisualizer("html", browsers()), true, "Display annotated source in a web browser", listHelp("weblist", false)},
  109. }
  110. // pprofVariables are the configuration parameters that affect the
  111. // reported generated by pprof.
  112. var pprofVariables = variables{
  113. // Filename for file-based output formats, stdout by default.
  114. "output": &variable{stringKind, "", "", helpText("Output filename for file-based outputs")},
  115. // Comparisons.
  116. "drop_negative": &variable{boolKind, "f", "", helpText(
  117. "Ignore negative differences",
  118. "Do not show any locations with values <0.")},
  119. // Comparisons.
  120. "positive_percentages": &variable{boolKind, "f", "", helpText(
  121. "Ignore negative samples when computing percentages",
  122. " Do not count negative samples when computing the total value",
  123. " of the profile, used to compute percentages. If set, and the -base",
  124. " option is used, percentages reported will be computed against the",
  125. " main profile, ignoring the base profile.")},
  126. // Graph handling options.
  127. "call_tree": &variable{boolKind, "f", "", helpText(
  128. "Create a context-sensitive call tree",
  129. "Treat locations reached through different paths as separate.")},
  130. // Display options.
  131. "relative_percentages": &variable{boolKind, "f", "", helpText(
  132. "Show percentages relative to focused subgraph",
  133. "If unset, percentages are relative to full graph before focusing",
  134. "to facilitate comparison with original graph.")},
  135. "unit": &variable{stringKind, "minimum", "", helpText(
  136. "Measurement units to display",
  137. "Scale the sample values to this unit.",
  138. " For time-based profiles, use seconds, milliseconds, nanoseconds, etc.",
  139. " For memory profiles, use megabytes, kilobytes, bytes, etc.",
  140. " auto will scale each value independently to the most natural unit.")},
  141. "compact_labels": &variable{boolKind, "f", "", "Show minimal headers"},
  142. "source_path": &variable{stringKind, "", "", "Search path for source files"},
  143. // Filtering options
  144. "nodecount": &variable{intKind, "-1", "", helpText(
  145. "Max number of nodes to show",
  146. "Uses heuristics to limit the number of locations to be displayed.",
  147. "On graphs, dotted edges represent paths through nodes that have been removed.")},
  148. "nodefraction": &variable{floatKind, "0.005", "", "Hide nodes below <f>*total"},
  149. "edgefraction": &variable{floatKind, "0.001", "", "Hide edges below <f>*total"},
  150. "trim": &variable{boolKind, "t", "", helpText(
  151. "Honor nodefraction/edgefraction/nodecount defaults",
  152. "Set to false to get the full profile, without any trimming.")},
  153. "focus": &variable{stringKind, "", "", helpText(
  154. "Restricts to samples going through a node matching regexp",
  155. "Discard samples that do not include a node matching this regexp.",
  156. "Matching includes the function name, filename or object name.")},
  157. "ignore": &variable{stringKind, "", "", helpText(
  158. "Skips paths going through any nodes matching regexp",
  159. "If set, discard samples that include a node matching this regexp.",
  160. "Matching includes the function name, filename or object name.")},
  161. "prune_from": &variable{stringKind, "", "", helpText(
  162. "Drops any functions below the matched frame.",
  163. "If set, any frames matching the specified regexp and any frames",
  164. "below it will be dropped from each sample.")},
  165. "hide": &variable{stringKind, "", "", helpText(
  166. "Skips nodes matching regexp",
  167. "Discard nodes that match this location.",
  168. "Other nodes from samples that include this location will be shown.",
  169. "Matching includes the function name, filename or object name.")},
  170. "show": &variable{stringKind, "", "", helpText(
  171. "Only show nodes matching regexp",
  172. "If set, only show nodes that match this location.",
  173. "Matching includes the function name, filename or object name.")},
  174. "tagfocus": &variable{stringKind, "", "", helpText(
  175. "Restrict to samples with tags in range or matched by regexp",
  176. "Discard samples that do not include a node with a tag matching this regexp.")},
  177. "tagignore": &variable{stringKind, "", "", helpText(
  178. "Discard samples with tags in range or matched by regexp",
  179. "Discard samples that do include a node with a tag matching this regexp.")},
  180. "tagshow": &variable{stringKind, "", "", helpText(
  181. "Only consider tags matching this regexp",
  182. "Discard tags that do not match this regexp")},
  183. "taghide": &variable{stringKind, "", "", helpText(
  184. "Skip tags matching this regexp",
  185. "Discard tags that match this regexp")},
  186. // Heap profile options
  187. "divide_by": &variable{floatKind, "1", "", helpText(
  188. "Ratio to divide all samples before visualization",
  189. "Divide all samples values by a constant, eg the number of processors or jobs.")},
  190. "mean": &variable{boolKind, "f", "", helpText(
  191. "Average sample value over first value (count)",
  192. "For memory profiles, report average memory per allocation.",
  193. "For time-based profiles, report average time per event.")},
  194. "sample_index": &variable{stringKind, "", "", helpText(
  195. "Sample value to report (0-based index or name)",
  196. "Profiles contain multiple values per sample.",
  197. "Use sample_index=i to select the ith value (starting at 0).")},
  198. // Data sorting criteria
  199. "flat": &variable{boolKind, "t", "cumulative", helpText("Sort entries based on own weight")},
  200. "cum": &variable{boolKind, "f", "cumulative", helpText("Sort entries based on cumulative weight")},
  201. // Output granularity
  202. "functions": &variable{boolKind, "t", "granularity", helpText(
  203. "Aggregate at the function level.",
  204. "Takes into account the filename/lineno where the function was defined.")},
  205. "files": &variable{boolKind, "f", "granularity", "Aggregate at the file level."},
  206. "lines": &variable{boolKind, "f", "granularity", "Aggregate at the source code line level."},
  207. "addresses": &variable{boolKind, "f", "granularity", helpText(
  208. "Aggregate at the function level.",
  209. "Includes functions' addresses in the output.")},
  210. "noinlines": &variable{boolKind, "f", "granularity", helpText(
  211. "Aggregate at the function level.",
  212. "Attributes inlined functions to their first out-of-line caller.")},
  213. "addressnoinlines": &variable{boolKind, "f", "granularity", helpText(
  214. "Aggregate at the function level, including functions' addresses in the output.",
  215. "Attributes inlined functions to their first out-of-line caller.")},
  216. }
  217. func helpText(s ...string) string {
  218. return strings.Join(s, "\n") + "\n"
  219. }
  220. // usage returns a string describing the pprof commands and variables.
  221. // if commandLine is set, the output reflect cli usage.
  222. func usage(commandLine bool) string {
  223. var prefix string
  224. if commandLine {
  225. prefix = "-"
  226. }
  227. fmtHelp := func(c, d string) string {
  228. return fmt.Sprintf(" %-16s %s", c, strings.SplitN(d, "\n", 2)[0])
  229. }
  230. var commands []string
  231. for name, cmd := range pprofCommands {
  232. commands = append(commands, fmtHelp(prefix+name, cmd.description))
  233. }
  234. sort.Strings(commands)
  235. var help string
  236. if commandLine {
  237. help = " Output formats (select at most one):\n"
  238. } else {
  239. help = " Commands:\n"
  240. commands = append(commands, fmtHelp("o/options", "List options and their current values"))
  241. commands = append(commands, fmtHelp("quit/exit/^D", "Exit pprof"))
  242. }
  243. help = help + strings.Join(commands, "\n") + "\n\n" +
  244. " Options:\n"
  245. // Print help for variables after sorting them.
  246. // Collect radio variables by their group name to print them together.
  247. radioOptions := make(map[string][]string)
  248. var variables []string
  249. for name, vr := range pprofVariables {
  250. if vr.group != "" {
  251. radioOptions[vr.group] = append(radioOptions[vr.group], name)
  252. continue
  253. }
  254. variables = append(variables, fmtHelp(prefix+name, vr.help))
  255. }
  256. sort.Strings(variables)
  257. help = help + strings.Join(variables, "\n") + "\n\n" +
  258. " Option groups (only set one per group):\n"
  259. var radioStrings []string
  260. for radio, ops := range radioOptions {
  261. sort.Strings(ops)
  262. s := []string{fmtHelp(radio, "")}
  263. for _, op := range ops {
  264. s = append(s, " "+fmtHelp(prefix+op, pprofVariables[op].help))
  265. }
  266. radioStrings = append(radioStrings, strings.Join(s, "\n"))
  267. }
  268. sort.Strings(radioStrings)
  269. return help + strings.Join(radioStrings, "\n")
  270. }
  271. func reportHelp(c string, cum, redirect bool) string {
  272. h := []string{
  273. c + " [n] [focus_regex]* [-ignore_regex]*",
  274. "Include up to n samples",
  275. "Include samples matching focus_regex, and exclude ignore_regex.",
  276. }
  277. if cum {
  278. h[0] += " [-cum]"
  279. h = append(h, "-cum sorts the output by cumulative weight")
  280. }
  281. if redirect {
  282. h[0] += " >f"
  283. h = append(h, "Optionally save the report on the file f")
  284. }
  285. return strings.Join(h, "\n")
  286. }
  287. func listHelp(c string, redirect bool) string {
  288. h := []string{
  289. c + "<func_regex|address> [-focus_regex]* [-ignore_regex]*",
  290. "Include functions matching func_regex, or including the address specified.",
  291. "Include samples matching focus_regex, and exclude ignore_regex.",
  292. }
  293. if redirect {
  294. h[0] += " >f"
  295. h = append(h, "Optionally save the report on the file f")
  296. }
  297. return strings.Join(h, "\n")
  298. }
  299. // browsers returns a list of commands to attempt for web visualization.
  300. func browsers() []string {
  301. cmds := []string{"chrome", "google-chrome", "firefox"}
  302. switch runtime.GOOS {
  303. case "darwin":
  304. return append(cmds, "/usr/bin/open")
  305. case "windows":
  306. return append(cmds, "cmd /c start")
  307. default:
  308. userBrowser := os.Getenv("BROWSER")
  309. if userBrowser != "" {
  310. cmds = append([]string{userBrowser, "sensible-browser"}, cmds...)
  311. } else {
  312. cmds = append([]string{"sensible-browser"}, cmds...)
  313. }
  314. return append(cmds, "xdg-open")
  315. }
  316. }
  317. var kcachegrind = []string{"kcachegrind"}
  318. // awayFromTTY saves the output in a file if it would otherwise go to
  319. // the terminal screen. This is used to avoid dumping binary data on
  320. // the screen.
  321. func awayFromTTY(format string) PostProcessor {
  322. return func(input io.Reader, output io.Writer, ui plugin.UI) error {
  323. if output == os.Stdout && (ui.IsTerminal() || interactiveMode) {
  324. tempFile, err := newTempFile("", "profile", "."+format)
  325. if err != nil {
  326. return err
  327. }
  328. ui.PrintErr("Generating report in ", tempFile.Name())
  329. output = tempFile
  330. }
  331. _, err := io.Copy(output, input)
  332. return err
  333. }
  334. }
  335. func invokeDot(format string) PostProcessor {
  336. return func(input io.Reader, output io.Writer, ui plugin.UI) error {
  337. cmd := exec.Command("dot", "-T"+format)
  338. cmd.Stdin, cmd.Stdout, cmd.Stderr = input, output, os.Stderr
  339. if err := cmd.Run(); err != nil {
  340. return fmt.Errorf("Failed to execute dot. Is Graphviz installed? Error: %v", err)
  341. }
  342. return nil
  343. }
  344. }
  345. // massageDotSVG invokes the dot tool to generate an SVG image and alters
  346. // the image to have panning capabilities when viewed in a browser.
  347. func massageDotSVG() PostProcessor {
  348. generateSVG := invokeDot("svg")
  349. return func(input io.Reader, output io.Writer, ui plugin.UI) error {
  350. baseSVG := new(bytes.Buffer)
  351. if err := generateSVG(input, baseSVG, ui); err != nil {
  352. return err
  353. }
  354. _, err := output.Write([]byte(svg.Massage(baseSVG.String())))
  355. return err
  356. }
  357. }
  358. func invokeVisualizer(suffix string, visualizers []string) PostProcessor {
  359. return func(input io.Reader, output io.Writer, ui plugin.UI) error {
  360. tempFile, err := newTempFile(os.TempDir(), "pprof", "."+suffix)
  361. if err != nil {
  362. return err
  363. }
  364. deferDeleteTempFile(tempFile.Name())
  365. if _, err := io.Copy(tempFile, input); err != nil {
  366. return err
  367. }
  368. tempFile.Close()
  369. // Try visualizers until one is successful
  370. for _, v := range visualizers {
  371. // Separate command and arguments for exec.Command.
  372. args := strings.Split(v, " ")
  373. if len(args) == 0 {
  374. continue
  375. }
  376. viewer := exec.Command(args[0], append(args[1:], tempFile.Name())...)
  377. viewer.Stderr = os.Stderr
  378. if err = viewer.Start(); err == nil {
  379. // Wait for a second so that the visualizer has a chance to
  380. // open the input file. This needs to be done even if we're
  381. // waiting for the visualizer as it can be just a wrapper that
  382. // spawns a browser tab and returns right away.
  383. defer func(t <-chan time.Time) {
  384. <-t
  385. }(time.After(time.Second))
  386. // On interactive mode, let the visualizer run in the background
  387. // so other commands can be issued.
  388. if !interactiveMode {
  389. return viewer.Wait()
  390. }
  391. return nil
  392. }
  393. }
  394. return err
  395. }
  396. }
  397. // variables describe the configuration parameters recognized by pprof.
  398. type variables map[string]*variable
  399. // variable is a single configuration parameter.
  400. type variable struct {
  401. kind int // How to interpret the value, must be one of the enums below.
  402. value string // Effective value. Only values appropriate for the Kind should be set.
  403. group string // boolKind variables with the same Group != "" cannot be set simultaneously.
  404. help string // Text describing the variable, in multiple lines separated by newline.
  405. }
  406. const (
  407. // variable.kind must be one of these variables.
  408. boolKind = iota
  409. intKind
  410. floatKind
  411. stringKind
  412. )
  413. // set updates the value of a variable, checking that the value is
  414. // suitable for the variable Kind.
  415. func (vars variables) set(name, value string) error {
  416. v := vars[name]
  417. if v == nil {
  418. return fmt.Errorf("no variable %s", name)
  419. }
  420. var err error
  421. switch v.kind {
  422. case boolKind:
  423. var b bool
  424. if b, err = stringToBool(value); err == nil {
  425. if v.group != "" && !b {
  426. err = fmt.Errorf("%q can only be set to true", name)
  427. }
  428. }
  429. case intKind:
  430. _, err = strconv.Atoi(value)
  431. case floatKind:
  432. _, err = strconv.ParseFloat(value, 64)
  433. case stringKind:
  434. // Remove quotes, particularly useful for empty values.
  435. if len(value) > 1 && strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`) {
  436. value = value[1 : len(value)-1]
  437. }
  438. }
  439. if err != nil {
  440. return err
  441. }
  442. vars[name].value = value
  443. if group := vars[name].group; group != "" {
  444. for vname, vvar := range vars {
  445. if vvar.group == group && vname != name {
  446. vvar.value = "f"
  447. }
  448. }
  449. }
  450. return err
  451. }
  452. // boolValue returns the value of a boolean variable.
  453. func (v *variable) boolValue() bool {
  454. b, err := stringToBool(v.value)
  455. if err != nil {
  456. panic("unexpected value " + v.value + " for bool ")
  457. }
  458. return b
  459. }
  460. // intValue returns the value of an intKind variable.
  461. func (v *variable) intValue() int {
  462. i, err := strconv.Atoi(v.value)
  463. if err != nil {
  464. panic("unexpected value " + v.value + " for int ")
  465. }
  466. return i
  467. }
  468. // floatValue returns the value of a Float variable.
  469. func (v *variable) floatValue() float64 {
  470. f, err := strconv.ParseFloat(v.value, 64)
  471. if err != nil {
  472. panic("unexpected value " + v.value + " for float ")
  473. }
  474. return f
  475. }
  476. // stringValue returns a canonical representation for a variable.
  477. func (v *variable) stringValue() string {
  478. switch v.kind {
  479. case boolKind:
  480. return fmt.Sprint(v.boolValue())
  481. case intKind:
  482. return fmt.Sprint(v.intValue())
  483. case floatKind:
  484. return fmt.Sprint(v.floatValue())
  485. }
  486. return v.value
  487. }
  488. func stringToBool(s string) (bool, error) {
  489. switch strings.ToLower(s) {
  490. case "true", "t", "yes", "y", "1", "":
  491. return true, nil
  492. case "false", "f", "no", "n", "0":
  493. return false, nil
  494. default:
  495. return false, fmt.Errorf(`illegal value "%s" for bool variable`, s)
  496. }
  497. }
  498. // makeCopy returns a duplicate of a set of shell variables.
  499. func (vars variables) makeCopy() variables {
  500. varscopy := make(variables, len(vars))
  501. for n, v := range vars {
  502. vcopy := *v
  503. varscopy[n] = &vcopy
  504. }
  505. return varscopy
  506. }