From d7a92b58ced3a01ab9b7c2d0fea0fc3a4dbf2603 Mon Sep 17 00:00:00 2001 From: Alessandro Arzilli Date: Wed, 26 Jul 2017 20:53:43 +0200 Subject: [PATCH] cmd/dlv: delete trace directory when we delete the built executable (#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. --- cmd/dlv/cmds/commands.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/cmd/dlv/cmds/commands.go b/cmd/dlv/cmds/commands.go index e94e6a31..3352ff30 100644 --- a/cmd/dlv/cmds/commands.go +++ b/cmd/dlv/cmds/commands.go @@ -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) } @@ -433,6 +433,7 @@ type executeKind int const ( executingExistingFile = executeKind(iota) executingGeneratedFile + executingGeneratedTest executingOther ) @@ -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() @@ -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) +}