Set 'all' build rule as default like other XDS packages.
[src/xds/xds-gdb.git] / main.go
diff --git a/main.go b/main.go
index 495a644..87de43c 100644 (file)
--- a/main.go
+++ b/main.go
@@ -123,7 +123,7 @@ func main() {
                },
                EnvVar{
                        Name:        "XDS_LOGFILE",
-                       Usage:       "logging file",
+                       Usage:       "logging file (default: " + logFileInitial + ")",
                        Destination: &logFile,
                },
                EnvVar{
@@ -164,7 +164,7 @@ func main() {
        for idx, a := range os.Args[1:] {
                // Specific case to print help or version of xds-gdb
                switch a {
-               case "--help", "-h", "--version", "-v":
+               case "--help", "-h", "--version", "-v", "--list", "-ls":
                        args[1] = a
                        goto endloop
                case "--":
@@ -205,6 +205,7 @@ endloop:
        // Source config env file
        // (we cannot use confFile var because env variables setting is just after)
        envMap, confFile, err := loadConfigEnvFile(os.Getenv("XDS_CONFIG"), gdbCmdFile)
+       log.Infof("Load env config: envMap=%v, confFile=%v, err=%v", envMap, confFile, err)
 
        // Only rise an error when args is not set (IOW when --help or --version is not set)
        if len(args) == 1 {
@@ -244,6 +245,12 @@ endloop:
                var err error
                curDir, _ := os.Getwd()
 
+               // Build env variables
+               env := []string{}
+               for k, v := range envMap {
+                       env = append(env, k+"="+v)
+               }
+
                // Now set logger level and log file to correct/env var settings
                if log.Level, err = logrus.ParseLevel(logLevel); err != nil {
                        msg := fmt.Sprintf("Invalid log level : \"%v\"\n", logLevel)
@@ -263,12 +270,6 @@ endloop:
                        log.Out = fdL
                }
 
-               // Build env variables
-               env := []string{}
-               for k, v := range envMap {
-                       env = append(env, k+"="+v)
-               }
-
                // Create cross or native gdb interface
                var gdb IGDB
                if gdbNative != "" {
@@ -344,7 +345,7 @@ endloop:
 
                // Handle client tty / pts
                if clientPty != "" {
-                       log.Infoln("Client tty detected: %v\n", clientPty)
+                       log.Infoln("Client tty detected: %v", clientPty)
 
                        cpFd, err := os.OpenFile(clientPty, os.O_RDWR, 0)
                        if err != nil {
@@ -408,6 +409,10 @@ endloop:
                        paranoia := 600
                        reader := bufio.NewReader(os.Stdin)
 
+                       // Enable workaround to correctly close connection
+                       // except if XDS_GDBSERVER_EXIT_NOFIX is defined
+                       _, gdbExitNoFix := os.LookupEnv("XDS_GDBSERVER_EXIT_NOFIX")
+
                        for {
                                sc := bufio.NewScanner(reader)
                                for sc.Scan() {
@@ -420,6 +425,16 @@ endloop:
                                                        log.Debugf("OVERWRITE %s -> %s", key, value)
                                                }
                                        }
+
+                                       // Send SIGINT to stop debugged process execution before sending -gdb-exit command
+                                       if !gdbExitNoFix && strings.Contains(command, "-gdb-exit") {
+                                               log.Infof("Detection of -gdb-exit, exiting...")
+                                               if err := gdb.SendSignal(syscall.SIGINT); err != nil {
+                                                       log.Errorf("Error while sending signal SIGINT : %s", err.Error())
+                                               }
+                                               time.Sleep(time.Millisecond * 200)
+                                       }
+
                                        gdb.Write(command + "\n")
                                        log.Debugf("Send: <%v>", command)
                                }
@@ -445,8 +460,15 @@ endloop:
                go func() {
                        for {
                                sig := <-sigs
+
+                               // FIXME: skip Window Changed signal for now
+                               if sig == syscall.SIGWINCH {
+                                       log.Debugf("SKIP signal Window Changed")
+                                       return
+                               }
+
                                if err := gdb.SendSignal(sig); err != nil {
-                                       log.Errorf("Error while sending signal: %s", err.Error())
+                                       log.Errorf("Error while sending signal %v : %s", sig, err.Error())
                                }
                        }
                }()
@@ -488,7 +510,7 @@ func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string,
                        defer os.Remove(confFile)
                }
                if err != nil {
-                       return envMap, confFile, fmt.Errorf(err.Error())
+                       log.Infof("Extraction from gdbCmdFile failed: %v", err.Error())
                }
        }
        // 2- search xds-gdb.env file in various locations
@@ -498,13 +520,14 @@ func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string,
                        xdsEnvFile := "xds-gdb.env"
                        for _, d := range []string{
                                path.Join(curDir),
-                               path.Join(curDir, "..", ".."),
-                               path.Join(curDir, "../../target"),
-                               path.Join(u.HomeDir, ".xds"),
+                               path.Join(curDir, ".."),
+                               path.Join(curDir, "target"),
+                               path.Join(u.HomeDir, ".config", "xds"),
                        } {
-                               confFile = path.Join(d, xdsEnvFile)
-                               log.Infof("Search config in %s", confFile)
-                               if common.Exists(confFile) {
+                               cf := path.Join(d, xdsEnvFile)
+                               log.Infof("Search config in %s", cf)
+                               if common.Exists(cf) {
+                                       confFile = cf
                                        break
                                }
                        }
@@ -516,7 +539,6 @@ func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string,
                return envMap, "", nil
        }
 
-       log.Infof("Use conf file: %s", confFile)
        if !common.Exists(confFile) {
                return envMap, confFile, fmt.Errorf("Error no env config file not found")
        }
@@ -526,6 +548,7 @@ func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string,
        if envMap, err = godotenv.Read(confFile); err != nil {
                return envMap, confFile, fmt.Errorf("Error reading env config file " + confFile)
        }
+
        return envMap, confFile, nil
 }