2 * Copyright (C) 2017 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 * xds-gdb: a wrapper on gdb tool for X(cross) Development System.
37 "github.com/Sirupsen/logrus"
38 "github.com/codegangsta/cli"
39 common "github.com/iotbzh/xds-common/golib"
40 "github.com/joho/godotenv"
43 var appAuthors = []cli.Author{
44 cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
47 // AppName name of this application
48 var AppName = "xds-gdb"
50 // AppVersion Version of this application
52 var AppVersion = "?.?.?"
54 // AppSubVersion is the git tag id added to version string
55 // Should be set by compilation -ldflags "-X main.AppSubVersion=xxx"
57 var AppSubVersion = "unknown-dev"
60 var log = logrus.New()
61 var logFileInitial = "/tmp/xds-gdb.log"
63 // Application details
65 appCopyright = "Copyright (C) 2017 IoT.bzh - Apache-2.0"
66 defaultLogLevel = "warning"
70 type exitResult struct {
75 // EnvVar - Environment variables used by application
82 // exitError terminates this program with the specified error
83 func exitError(code syscall.Errno, f string, a ...interface{}) {
84 err := fmt.Sprintf(f, a...)
85 fmt.Fprintf(os.Stderr, err+"\n")
86 log.Debugf("Exit: code=%v, err=%s", code, err)
93 var agentURL, serverURL string
94 var prjID, rPath, logLevel, logFile, sdkid, confFile, gdbNative string
98 // Init Logger and set temporary file and level for the 1st part
99 // IOW while XDS_LOGLEVEL and XDS_LOGFILE options are not parsed
100 logFile = logFileInitial
101 fdL, err := os.OpenFile(logFileInitial, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
103 msgErr := fmt.Sprintf("Cannot create log file %s", logFileInitial)
104 exitError(syscall.EPERM, msgErr)
106 log.Formatter = &logrus.TextFormatter{}
108 log.Level = logrus.DebugLevel
110 agentURL = "localhost:8800"
111 logLevel = defaultLogLevel
113 // Create a new App instance
116 app.Usage = "wrapper on gdb for X(cross) Development System."
117 app.Version = AppVersion + " (" + AppSubVersion + ")"
118 app.Authors = appAuthors
119 app.Copyright = appCopyright
120 app.Metadata = make(map[string]interface{})
121 app.Metadata["version"] = AppVersion
122 app.Metadata["git-tag"] = AppSubVersion
123 app.Metadata["logger"] = log
125 app.Flags = []cli.Flag{
128 Usage: "list existing xds projects",
129 Destination: &listProject,
133 appEnvVars := []EnvVar{
136 Usage: "env config file to source on startup",
137 Destination: &confFile,
140 Name: "XDS_LOGLEVEL",
141 Usage: "logging level (supported levels: panic, fatal, error, warn, info, debug)",
142 Destination: &logLevel,
146 Usage: "logging file (default: " + logFileInitial + ")",
147 Destination: &logFile,
150 Name: "XDS_NATIVE_GDB",
151 Usage: "use native gdb instead of remote XDS server",
152 Destination: &gdbNative,
155 Name: "XDS_PROJECT_ID",
156 Usage: "project ID you want to build (mandatory variable)",
161 Usage: "relative path into project",
166 Usage: "Cross Sdk ID to use to build project",
170 Name: "XDS_AGENT_URL",
171 Usage: "local XDS agent url",
172 Destination: &agentURL,
175 Name: "XDS_SERVER_URL",
176 Usage: "overwrite remote XDS server url (default value set in xds-agent-config.json file)",
177 Destination: &serverURL,
181 // Process gdb arguments
182 log.Debugf("xds-gdb started with args: %v", os.Args)
183 args := make([]string, len(os.Args))
185 gdbArgs := make([]string, len(os.Args))
187 // Split xds-xxx options from gdb options
188 copy(gdbArgs, os.Args[1:])
189 for idx, a := range os.Args[1:] {
190 // Specific case to print help or version of xds-gdb
192 case "--help", "-h", "--version", "-v", "--list", "-ls":
196 // Detect skip option (IOW '--') to split arguments
197 copy(args, os.Args[0:idx+1])
198 copy(gdbArgs, os.Args[idx+2:])
204 // Parse gdb arguments to detect:
205 // --tty option: used for inferior/ tty of debugged program
206 // -x/--command option: XDS env vars may be set within gdb command file
209 for idx, a := range gdbArgs {
211 case strings.HasPrefix(a, "--tty="):
212 clientPty = a[len("--tty="):]
216 case strings.HasPrefix(a, "-tty"):
217 clientPty = gdbArgs[idx+1]
221 case strings.HasPrefix(a, "--command="):
222 gdbCmdFile = a[len("--command="):]
224 case a == "--command":
225 case strings.HasPrefix(a, "-x"):
226 gdbCmdFile = gdbArgs[idx+1]
230 // Source config env file
231 // (we cannot use confFile var because env variables setting is just after)
232 envMap, confFile, err := loadConfigEnvFile(os.Getenv("XDS_CONFIG"), gdbCmdFile)
233 log.Infof("Load env config: envMap=%v, confFile=%v, err=%v", envMap, confFile, err)
235 // Only rise an error when args is not set (IOW when --help or --version is not set)
238 exitError(syscall.ENOENT, err.Error())
242 // Managed env vars and create help
243 dynDesc := "\nENVIRONMENT VARIABLES:"
244 for _, ev := range appEnvVars {
245 dynDesc += fmt.Sprintf("\n %s \t\t %s", ev.Name, ev.Usage)
246 if evVal, evExist := os.LookupEnv(ev.Name); evExist && ev.Destination != nil {
247 *ev.Destination = evVal
250 app.Description = "gdb wrapper for X(cross) Development System\n"
251 app.Description += "\n"
252 app.Description += " Two debugging models are supported:\n"
253 app.Description += " - xds remote debugging requiring an XDS server and allowing cross debug\n"
254 app.Description += " - native debugging\n"
255 app.Description += " By default xds remote debug is used and you need to define XDS_NATIVE_GDB to\n"
256 app.Description += " use native gdb debug mode instead.\n"
257 app.Description += "\n"
258 app.Description += " xds-gdb configuration (see variables list below) can be set using:\n"
259 app.Description += " - a config file (XDS_CONFIG)\n"
260 app.Description += " - or environment variables\n"
261 app.Description += " - or by setting variables within gdb ini file (commented line including :XDS-ENV: tag)\n"
262 app.Description += " Example of gdb ini file where we define project and sdk ID:\n"
263 app.Description += " # :XDS-ENV: XDS_PROJECT_ID=IW7B4EE-DBY4Z74_myProject\n"
264 app.Description += " # :XDS-ENV: XDS_SDK_ID=poky-agl_aarch64_3.99.1+snapshot\n"
265 app.Description += "\n"
266 app.Description += dynDesc + "\n"
269 app.Action = func(ctx *cli.Context) error {
271 curDir, _ := os.Getwd()
273 // Build env variables
275 for k, v := range envMap {
276 env = append(env, k+"="+v)
279 // Now set logger level and log file to correct/env var settings
280 if log.Level, err = logrus.ParseLevel(logLevel); err != nil {
281 msg := fmt.Sprintf("Invalid log level : \"%v\"\n", logLevel)
282 return cli.NewExitError(msg, int(syscall.EINVAL))
284 log.Infof("Switch log level to %s", logLevel)
286 if logFile != logFileInitial {
287 log.Infof("Switch logging to log file %s", logFile)
289 fdL, err := os.OpenFile(logFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
291 msgErr := fmt.Sprintf("Cannot create log file %s", logFile)
292 return cli.NewExitError(msgErr, int(syscall.EPERM))
298 // Create cross or native gdb interface
301 gdb = NewGdbNative(log, gdbArgs, env)
303 gdb = NewGdbXds(log, gdbArgs, env)
304 gdb.SetConfig("agentURL", agentURL)
305 gdb.SetConfig("serverURL", serverURL)
306 gdb.SetConfig("prjID", prjID)
307 gdb.SetConfig("sdkID", sdkid)
308 gdb.SetConfig("rPath", rPath)
309 gdb.SetConfig("listProject", listProject)
313 log.Infof("Original arguments: %v", os.Args)
314 log.Infof("Current directory : %v", curDir)
315 log.Infof("Use confFile : '%s'", confFile)
316 log.Infof("Execute : /exec %v %v", gdb.Cmd(), gdb.Args())
318 // Properly report invalid init file error
319 gdbCommandFileError := ""
320 for i, a := range gdbArgs {
322 gdbCommandFileError = gdbArgs[i+1] + ": No such file or directory."
324 } else if strings.HasPrefix(a, "--command=") {
325 gdbCommandFileError = strings.TrimLeft(a, "--command=") + ": No such file or directory."
329 log.Infof("Add detection of error: <%s>", gdbCommandFileError)
331 // Init gdb subprocess management
332 if code, err := gdb.Init(); err != nil {
333 return cli.NewExitError(err.Error(), code)
336 exitChan := make(chan exitResult, 1)
338 gdb.OnError(func(err error) {
339 fmt.Println("ERROR: ", err.Error())
342 gdb.OnDisconnect(func(err error) {
343 errMsg := "\nXDS-Agent disconnected"
345 fmt.Printf("%s: %v\n", errMsg, err.Error())
350 exitChan <- exitResult{err, int(syscall.ESHUTDOWN)}
353 gdb.Read(func(timestamp, stdout, stderr string) {
355 fmt.Printf("%s", stdout)
356 log.Debugf("Recv OUT: <%s>", stdout)
359 fmt.Fprintf(os.Stderr, "%s", stderr)
360 log.Debugf("Recv ERR: <%s>", stderr)
363 // Correctly report error about init file
364 if gdbCommandFileError != "" && strings.Contains(stdout, gdbCommandFileError) {
365 fmt.Fprintf(os.Stderr, "ERROR: "+gdbCommandFileError)
366 log.Errorf("ERROR: " + gdbCommandFileError)
367 if err := gdb.SendSignal(syscall.SIGTERM); err != nil {
368 log.Errorf("Error while sending signal: %s", err.Error())
370 exitChan <- exitResult{err, int(syscall.ENOENT)}
374 gdb.OnExit(func(code int, err error) {
375 exitChan <- exitResult{err, code}
378 // Handle client tty / pts
380 log.Infoln("Client tty detected: %v", clientPty)
382 cpFd, err := os.OpenFile(clientPty, os.O_RDWR, 0)
384 return cli.NewExitError(err.Error(), int(syscall.EPERM))
389 /* XXX TODO - implement stdin to send data to debugged program
391 reader := bufio.NewReader(cpFd)
392 sc := bufio.NewScanner(reader)
395 iosk.Emit(xaapiv1.ExecInferiorInEvent, data+"\n")
396 log.Debugf("Inferior IN: <%v>", data)
399 log.Warnf("Inferior Stdin scanner exit, close stdin (err=%v)", sc.Err())
405 gdb.InferiorRead(func(timestamp, stdout, stderr string) {
407 fmt.Fprintf(cpFd, "%s", stdout)
408 log.Debugf("Inferior OUT: <%s>", stdout)
411 fmt.Fprintf(cpFd, "%s", stderr)
412 log.Debugf("Inferior ERR: <%s>", stderr)
417 // Allow to overwrite some gdb commands
418 var overwriteMap = make(map[string]string)
419 if overEnv, exist := os.LookupEnv("XDS_OVERWRITE_COMMANDS"); exist {
420 overEnvS := strings.TrimSpace(overEnv)
421 if len(overEnvS) > 0 {
422 // Extract overwrite commands from env variable
423 for _, def := range strings.Split(overEnvS, ",") {
424 if kv := strings.Split(def, ":"); len(kv) == 2 {
425 overwriteMap[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
427 return cli.NewExitError(
428 fmt.Errorf("Invalid definition in XDS_OVERWRITE_COMMANDS (%s)", def),
434 overwriteMap["-exec-run"] = "-exec-continue"
435 overwriteMap["-file-exec-and-symbols"] = "-file-exec-file"
437 log.Debugf("overwriteMap = %v", overwriteMap)
439 // Send stdin though WS
442 reader := bufio.NewReader(os.Stdin)
444 // Enable workaround to correctly close connection
445 // except if XDS_GDBSERVER_EXIT_NOFIX is defined
446 _, gdbExitNoFix := os.LookupEnv("XDS_GDBSERVER_EXIT_NOFIX")
449 sc := bufio.NewScanner(reader)
453 // overwrite some commands
454 for key, value := range overwriteMap {
455 if strings.Contains(command, key) {
456 command = strings.Replace(command, key, value, 1)
457 log.Debugf("OVERWRITE %s -> %s", key, value)
461 // Send SIGINT to stop debugged process execution before sending -gdb-exit command
462 if !gdbExitNoFix && strings.Contains(command, "-gdb-exit") {
463 log.Infof("Detection of -gdb-exit, exiting...")
464 if err := gdb.SendSignal(syscall.SIGINT); err != nil {
465 log.Errorf("Error while sending signal SIGINT : %s", err.Error())
467 time.Sleep(time.Millisecond * 200)
470 log.Debugf("Send: <%v>", command)
471 gdb.Write(command + "\n")
473 log.Infof("Stdin scanner exit, close stdin (err=%v)", sc.Err())
475 // CTRL-D exited scanner, so send it explicitly
477 time.Sleep(time.Millisecond * 100)
479 if paranoia--; paranoia <= 0 {
480 msg := "Abnormal loop detected on stdin"
481 log.Errorf("Abnormal loop detected on stdin")
482 gdb.SendSignal(syscall.SIGTERM)
483 exitChan <- exitResult{fmt.Errorf(msg), int(syscall.ELOOP)}
488 // Handling all Signals
489 sigs := make(chan os.Signal, 1)
496 if isIgnoredSignal(sig) {
500 if err := gdb.SendSignal(sig); err != nil {
501 log.Errorf("Error while sending signal %v : %s", sig, err.Error())
507 if code, err := gdb.Start(clientPty != ""); err != nil {
508 return cli.NewExitError(err.Error(), code)
513 case res := <-exitChan:
516 log.Infoln("Exit successfully")
518 if res.error != nil {
519 log.Infoln("Exit with ERROR: ", res.error.Error())
520 errStr = res.error.Error()
522 return cli.NewExitError(errStr, res.code)
530 func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string, error) {
532 envMap := make(map[string]string)
534 // 1- if no confFile set, use setting from gdb command file is option
535 // --command/-x is set
536 if confFile == "" && gdbCmdFile != "" {
537 log.Infof("Try extract config from gdbCmdFile: %s", gdbCmdFile)
538 confFile, err = extractEnvFromCmdFile(gdbCmdFile)
540 defer os.Remove(confFile)
543 log.Infof("Extraction from gdbCmdFile failed: %v", err.Error())
546 // 2- search xds-gdb.env file in various locations
548 curDir, _ := os.Getwd()
549 if u, err := user.Current(); err == nil {
550 xdsEnvFile := "xds-gdb.env"
551 for _, d := range []string{
553 path.Join(curDir, ".."),
554 path.Join(curDir, "target"),
555 path.Join(u.HomeDir, ".config", "xds"),
557 cf := path.Join(d, xdsEnvFile)
558 log.Infof("Search config in %s", cf)
559 if common.Exists(cf) {
568 log.Infof("NO valid conf file found!")
569 return envMap, "", nil
572 if !common.Exists(confFile) {
573 return envMap, confFile, fmt.Errorf("Error no env config file not found")
575 if err = godotenv.Load(confFile); err != nil {
576 return envMap, confFile, fmt.Errorf("Error loading env config file " + confFile)
578 if envMap, err = godotenv.Read(confFile); err != nil {
579 return envMap, confFile, fmt.Errorf("Error reading env config file " + confFile)
582 return envMap, confFile, nil
586 extractEnvFromCmdFile: extract xds-gdb env variable from gdb command file
587 All commented lines (#) in gdb command file that start with ':XDS-ENV:' prefix
588 will be considered as XDS env commands. For example the 3 syntaxes below
590 # :XDS-ENV: XDS_PROJECT_ID=4021617e-ced0-11e7-acd2-3c970e49ad9b
591 #:XDS-ENV:XDS_SDK_ID=06c0e95a-e215-3a5a-b373-f677c0dabd3b
592 # :XDS-ENV: export XDS_AGENT_URL=localhost:8800
594 func extractEnvFromCmdFile(cmdFile string) (string, error) {
595 if !common.Exists(cmdFile) {
598 cFd, err := os.Open(cmdFile)
600 return "", fmt.Errorf("Cannot open %s : %s", cmdFile, err.Error())
605 scanner := bufio.NewScanner(cFd)
607 lines = append(lines, scanner.Text())
609 if err = scanner.Err(); err != nil {
610 return "", fmt.Errorf("Cannot parse %s : %s", cmdFile, err.Error())
613 envFile, err := ioutil.TempFile("", "xds-gdb_env.ini")
615 return "", fmt.Errorf("Error while creating temporary env file: %s", err.Error())
617 envFileName := envFile.Name()
618 defer envFile.Close()
621 for _, ln := range lines {
622 ln = strings.TrimSpace(ln)
623 if strings.HasPrefix(ln, "#") && strings.Contains(ln, ":XDS-ENV:") {
624 env := strings.SplitAfterN(ln, ":XDS-ENV:", 2)
627 if _, err := envFile.WriteString(strings.TrimSpace(env[1]) + "\n"); err != nil {
628 return "", fmt.Errorf("Error write into temporary env file: %s", err.Error())
631 log.Warnf("Error while decoding line %s", ln)
643 return envFileName, nil