Improved projects+sdks list output
[src/xds/xds-gdb.git] / main.go
diff --git a/main.go b/main.go
index 64bbcb9..3df9e1a 100644 (file)
--- a/main.go
+++ b/main.go
@@ -1,4 +1,23 @@
-// xds-gdb: a wrapper on gdb tool for X(cross) Development System.
+/*
+ * Copyright (C) 2017 "IoT.bzh"
+ * Author Sebastien Douheret <sebastien@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ *
+ * xds-gdb: a wrapper on gdb tool for X(cross) Development System.
+ */
+
 package main
 
 import (
@@ -43,7 +62,7 @@ var logFileInitial = "/tmp/xds-gdb.log"
 
 // Application details
 const (
-       appCopyright    = "Apache-2.0"
+       appCopyright    = "Copyright (C) 2017 IoT.bzh - Apache-2.0"
        defaultLogLevel = "warning"
 )
 
@@ -71,7 +90,8 @@ func exitError(code syscall.Errno, f string, a ...interface{}) {
 
 // main
 func main() {
-       var uri, prjID, rPath, logLevel, logFile, sdkid, confFile, gdbNative string
+       var agentURL, serverURL string
+       var prjID, rPath, logLevel, logFile, sdkid, confFile, gdbNative string
        var listProject bool
        var err error
 
@@ -87,7 +107,7 @@ func main() {
        log.Out = fdL
        log.Level = logrus.DebugLevel
 
-       uri = "localhost:8000"
+       agentURL = "localhost:8800"
        logLevel = defaultLogLevel
 
        // Create a new App instance
@@ -123,7 +143,7 @@ func main() {
                },
                EnvVar{
                        Name:        "XDS_LOGFILE",
-                       Usage:       "logging file",
+                       Usage:       "logging file (default: " + logFileInitial + ")",
                        Destination: &logFile,
                },
                EnvVar{
@@ -146,10 +166,15 @@ func main() {
                        Usage:       "Cross Sdk ID to use to build project",
                        Destination: &sdkid,
                },
+               EnvVar{
+                       Name:        "XDS_AGENT_URL",
+                       Usage:       "local XDS agent url",
+                       Destination: &agentURL,
+               },
                EnvVar{
                        Name:        "XDS_SERVER_URL",
-                       Usage:       "remote XDS server url",
-                       Destination: &uri,
+                       Usage:       "overwrite remote XDS server url (default value set in xds-agent-config.json file)",
+                       Destination: &serverURL,
                },
        }
 
@@ -276,7 +301,8 @@ endloop:
                        gdb = NewGdbNative(log, gdbArgs, env)
                } else {
                        gdb = NewGdbXds(log, gdbArgs, env)
-                       gdb.SetConfig("uri", uri)
+                       gdb.SetConfig("agentURL", agentURL)
+                       gdb.SetConfig("serverURL", serverURL)
                        gdb.SetConfig("prjID", prjID)
                        gdb.SetConfig("sdkID", sdkid)
                        gdb.SetConfig("rPath", rPath)
@@ -345,7 +371,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 {
@@ -360,7 +386,7 @@ endloop:
                                sc := bufio.NewScanner(reader)
                                for sc.Scan() {
                                        data := sc.Text()
-                                       iosk.Emit(apiv1.ExecInferiorInEvent, data+"\n")
+                                       iosk.Emit(xaapiv1.ExecInferiorInEvent, data+"\n")
                                        log.Debugf("Inferior IN: <%v>", data)
                                }
                                if sc.Err() != nil {
@@ -409,6 +435,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() {
@@ -421,6 +451,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)
                                }
@@ -446,8 +486,13 @@ endloop:
                go func() {
                        for {
                                sig := <-sigs
+
+                               if isIgnoredSignal(sig) {
+                                       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())
                                }
                        }
                }()
@@ -499,13 +544,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
                                }
                        }
@@ -535,9 +581,9 @@ func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string,
   All commented lines (#) in gdb command file that start with ':XDS-ENV:' prefix
   will be considered as XDS env commands. For example the 3 syntaxes below
   are supported:
-  # :XDS-ENV: XDS_PROJECT_ID=IW7B4EE-DBY4Z74_myProject
-  #:XDS-ENV:XDS_SDK_ID=poky-agl_aarch64_3.99.1+snapshot
-  # :XDS-ENV:  export XDS_SERVER_URL=localhost:8800
+  # :XDS-ENV: XDS_PROJECT_ID=4021617e-ced0-11e7-acd2-3c970e49ad9b
+  #:XDS-ENV:XDS_SDK_ID=06c0e95a-e215-3a5a-b373-f677c0dabd3b
+  # :XDS-ENV:  export XDS_AGENT_URL=localhost:8800
 */
 func extractEnvFromCmdFile(cmdFile string) (string, error) {
        if !common.Exists(cmdFile) {