Skip to content

Commit

Permalink
cmd/dlv: delete trace directory when we delete the built executable (g…
Browse files Browse the repository at this point in the history
…o-delve#856)

Mozilla RR will create a trace directory that can be reused with the
replay verb, however if we delete the executable file the trace
directory will become useless, so delete that too before exit.

Users that wish to reuse a recording should build the executable
themselves and then use either dlv exec or rr record to do the
recording.
  • Loading branch information
aarzilli authored and derekparker committed Jul 26, 2017
1 parent e0ac447 commit d7a92b5
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion cmd/dlv/cmds/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func testCmd(cmd *cobra.Command, args []string) {
defer os.Remove("./" + testdebugname)
processArgs := append([]string{"./" + testdebugname}, targetArgs...)

return execute(0, processArgs, conf, "", executingOther)
return execute(0, processArgs, conf, "", executingGeneratedTest)
}()
os.Exit(status)
}
Expand Down Expand Up @@ -433,6 +433,7 @@ type executeKind int
const (
executingExistingFile = executeKind(iota)
executingGeneratedFile
executingGeneratedTest
executingOther
)

Expand Down Expand Up @@ -507,6 +508,13 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile
} else {
// Create and start a terminal
client := rpc2.NewClient(listener.Addr().String())
if client.Recorded() && (kind == executingGeneratedFile || kind == executingGeneratedTest) {
// When using the rr backend remove the trace directory if we built the
// executable
if tracedir, err := client.TraceDirectory(); err == nil {
defer SafeRemoveAll(tracedir)
}
}
term := terminal.New(client, conf)
term.InitFile = InitFile
status, err = term.Run()
Expand Down Expand Up @@ -601,3 +609,28 @@ func splitQuotedFields(in string) []string {

return r
}

// SafeRemoveAll removes dir and its contents but only as long as dir does
// not contain directories.
func SafeRemoveAll(dir string) {
dh, err := os.Open(dir)
if err != nil {
return
}
defer dh.Close()
fis, err := dh.Readdir(-1)
if err != nil {
return
}
for _, fi := range fis {
if fi.IsDir() {
return
}
}
for _, fi := range fis {
if err := os.Remove(filepath.Join(dir, fi.Name())); err != nil {
return
}
}
os.Remove(dir)
}

0 comments on commit d7a92b5

Please sign in to comment.