Fixed crash on xds-agent or server disconnection (IOT-108)
[src/xds/xds-gdb.git] / main.go
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *
18  * xds-gdb: a wrapper on gdb tool for X(cross) Development System.
19  */
20
21 package main
22
23 import (
24         "bufio"
25         "fmt"
26         "io/ioutil"
27         "os"
28         "os/signal"
29         "os/user"
30         "syscall"
31         "time"
32
33         "strings"
34
35         "path"
36
37         "github.com/Sirupsen/logrus"
38         "github.com/codegangsta/cli"
39         common "github.com/iotbzh/xds-common/golib"
40         "github.com/joho/godotenv"
41 )
42
43 var appAuthors = []cli.Author{
44         cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
45 }
46
47 // AppName name of this application
48 var AppName = "xds-gdb"
49
50 // AppVersion Version of this application
51 // (set by Makefile)
52 var AppVersion = "?.?.?"
53
54 // AppSubVersion is the git tag id added to version string
55 // Should be set by compilation -ldflags "-X main.AppSubVersion=xxx"
56 // (set by Makefile)
57 var AppSubVersion = "unknown-dev"
58
59 // Create logger
60 var log = logrus.New()
61 var logFileInitial = "/tmp/xds-gdb.log"
62
63 // Application details
64 const (
65         appCopyright    = "Copyright (C) 2017 IoT.bzh - Apache-2.0"
66         defaultLogLevel = "warning"
67 )
68
69 // Exit events
70 type exitResult struct {
71         error error
72         code  int
73 }
74
75 // EnvVar - Environment variables used by application
76 type EnvVar struct {
77         Name        string
78         Usage       string
79         Destination *string
80 }
81
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)
87
88         os.Exit(int(code))
89 }
90
91 // main
92 func main() {
93         var agentURL, serverURL string
94         var prjID, rPath, logLevel, logFile, sdkid, confFile, gdbNative string
95         var listProject bool
96         var err error
97
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)
102         if err != nil {
103                 msgErr := fmt.Sprintf("Cannot create log file %s", logFileInitial)
104                 exitError(syscall.EPERM, msgErr)
105         }
106         log.Formatter = &logrus.TextFormatter{}
107         log.Out = fdL
108         log.Level = logrus.DebugLevel
109
110         agentURL = "localhost:8800"
111         logLevel = defaultLogLevel
112
113         // Create a new App instance
114         app := cli.NewApp()
115         app.Name = AppName
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
124
125         app.Flags = []cli.Flag{
126                 cli.BoolFlag{
127                         Name:        "list, ls",
128                         Usage:       "list existing xds projects",
129                         Destination: &listProject,
130                 },
131         }
132
133         appEnvVars := []EnvVar{
134                 EnvVar{
135                         Name:        "XDS_CONFIG",
136                         Usage:       "env config file to source on startup",
137                         Destination: &confFile,
138                 },
139                 EnvVar{
140                         Name:        "XDS_LOGLEVEL",
141                         Usage:       "logging level (supported levels: panic, fatal, error, warn, info, debug)",
142                         Destination: &logLevel,
143                 },
144                 EnvVar{
145                         Name:        "XDS_LOGFILE",
146                         Usage:       "logging file (default: " + logFileInitial + ")",
147                         Destination: &logFile,
148                 },
149                 EnvVar{
150                         Name:        "XDS_NATIVE_GDB",
151                         Usage:       "use native gdb instead of remote XDS server",
152                         Destination: &gdbNative,
153                 },
154                 EnvVar{
155                         Name:        "XDS_PROJECT_ID",
156                         Usage:       "project ID you want to build (mandatory variable)",
157                         Destination: &prjID,
158                 },
159                 EnvVar{
160                         Name:        "XDS_RPATH",
161                         Usage:       "relative path into project",
162                         Destination: &rPath,
163                 },
164                 EnvVar{
165                         Name:        "XDS_SDK_ID",
166                         Usage:       "Cross Sdk ID to use to build project",
167                         Destination: &sdkid,
168                 },
169                 EnvVar{
170                         Name:        "XDS_AGENT_URL",
171                         Usage:       "local XDS agent url",
172                         Destination: &agentURL,
173                 },
174                 EnvVar{
175                         Name:        "XDS_SERVER_URL",
176                         Usage:       "overwrite remote XDS server url (default value set in xds-agent-config.json file)",
177                         Destination: &serverURL,
178                 },
179         }
180
181         // Process gdb arguments
182         log.Debugf("xds-gdb started with args: %v", os.Args)
183         args := make([]string, len(os.Args))
184         args[0] = os.Args[0]
185         gdbArgs := make([]string, len(os.Args))
186
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
191                 switch a {
192                 case "--help", "-h", "--version", "-v", "--list", "-ls":
193                         args[1] = a
194                         goto endloop
195                 case "--":
196                         // Detect skip option (IOW '--') to split arguments
197                         copy(args, os.Args[0:idx+1])
198                         copy(gdbArgs, os.Args[idx+2:])
199                         goto endloop
200                 }
201         }
202 endloop:
203
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
207         clientPty := ""
208         gdbCmdFile := ""
209         for idx, a := range gdbArgs {
210                 switch {
211                 case strings.HasPrefix(a, "--tty="):
212                         clientPty = a[len("--tty="):]
213                         gdbArgs[idx] = ""
214
215                 case a == "--tty":
216                 case strings.HasPrefix(a, "-tty"):
217                         clientPty = gdbArgs[idx+1]
218                         gdbArgs[idx] = ""
219                         gdbArgs[idx+1] = ""
220
221                 case strings.HasPrefix(a, "--command="):
222                         gdbCmdFile = a[len("--command="):]
223
224                 case a == "--command":
225                 case strings.HasPrefix(a, "-x"):
226                         gdbCmdFile = gdbArgs[idx+1]
227                 }
228         }
229
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)
234
235         // Only rise an error when args is not set (IOW when --help or --version is not set)
236         if len(args) == 1 {
237                 if err != nil {
238                         exitError(syscall.ENOENT, err.Error())
239                 }
240         }
241
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
248                 }
249         }
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"
267
268         // only one action
269         app.Action = func(ctx *cli.Context) error {
270                 var err error
271                 curDir, _ := os.Getwd()
272
273                 // Build env variables
274                 env := []string{}
275                 for k, v := range envMap {
276                         env = append(env, k+"="+v)
277                 }
278
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))
283                 }
284                 log.Infof("Switch log level to %s", logLevel)
285
286                 if logFile != logFileInitial {
287                         log.Infof("Switch logging to log file %s", logFile)
288
289                         fdL, err := os.OpenFile(logFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
290                         if err != nil {
291                                 msgErr := fmt.Sprintf("Cannot create log file %s", logFile)
292                                 return cli.NewExitError(msgErr, int(syscall.EPERM))
293                         }
294                         defer fdL.Close()
295                         log.Out = fdL
296                 }
297
298                 // Create cross or native gdb interface
299                 var gdb IGDB
300                 if gdbNative != "" {
301                         gdb = NewGdbNative(log, gdbArgs, env)
302                 } else {
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)
310                 }
311
312                 // Log useful info
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())
317
318                 // Properly report invalid init file error
319                 gdbCommandFileError := ""
320                 for i, a := range gdbArgs {
321                         if a == "-x" {
322                                 gdbCommandFileError = gdbArgs[i+1] + ": No such file or directory."
323                                 break
324                         } else if strings.HasPrefix(a, "--command=") {
325                                 gdbCommandFileError = strings.TrimLeft(a, "--command=") + ": No such file or directory."
326                                 break
327                         }
328                 }
329                 log.Infof("Add detection of error: <%s>", gdbCommandFileError)
330
331                 // Init gdb subprocess management
332                 if code, err := gdb.Init(); err != nil {
333                         return cli.NewExitError(err.Error(), code)
334                 }
335
336                 exitChan := make(chan exitResult, 1)
337
338                 gdb.OnError(func(err error) {
339                         fmt.Println("ERROR: ", err.Error())
340                 })
341
342                 gdb.OnDisconnect(func(err error) {
343                         errMsg := "\nXDS-Agent disconnected"
344                         if err != nil {
345                                 fmt.Printf("%s: %v\n", errMsg, err.Error())
346                         } else {
347                                 fmt.Println(errMsg)
348                         }
349
350                         exitChan <- exitResult{err, int(syscall.ESHUTDOWN)}
351                 })
352
353                 gdb.Read(func(timestamp, stdout, stderr string) {
354                         if stdout != "" {
355                                 fmt.Printf("%s", stdout)
356                                 log.Debugf("Recv OUT: <%s>", stdout)
357                         }
358                         if stderr != "" {
359                                 fmt.Fprintf(os.Stderr, "%s", stderr)
360                                 log.Debugf("Recv ERR: <%s>", stderr)
361                         }
362
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())
369                                 }
370                                 exitChan <- exitResult{err, int(syscall.ENOENT)}
371                         }
372                 })
373
374                 gdb.OnExit(func(code int, err error) {
375                         exitChan <- exitResult{err, code}
376                 })
377
378                 // Handle client tty / pts
379                 if clientPty != "" {
380                         log.Infoln("Client tty detected: %v", clientPty)
381
382                         cpFd, err := os.OpenFile(clientPty, os.O_RDWR, 0)
383                         if err != nil {
384                                 return cli.NewExitError(err.Error(), int(syscall.EPERM))
385                         }
386                         defer cpFd.Close()
387
388                         // client tty stdin
389                         /* XXX TODO - implement stdin to send data to debugged program
390                         go func() {
391                                 reader := bufio.NewReader(cpFd)
392                                 sc := bufio.NewScanner(reader)
393                                 for sc.Scan() {
394                                         data := sc.Text()
395                                         iosk.Emit(xaapiv1.ExecInferiorInEvent, data+"\n")
396                                         log.Debugf("Inferior IN: <%v>", data)
397                                 }
398                                 if sc.Err() != nil {
399                                         log.Warnf("Inferior Stdin scanner exit, close stdin (err=%v)", sc.Err())
400                                 }
401                         }()
402                         */
403
404                         // client tty stdout
405                         gdb.InferiorRead(func(timestamp, stdout, stderr string) {
406                                 if stdout != "" {
407                                         fmt.Fprintf(cpFd, "%s", stdout)
408                                         log.Debugf("Inferior OUT: <%s>", stdout)
409                                 }
410                                 if stderr != "" {
411                                         fmt.Fprintf(cpFd, "%s", stderr)
412                                         log.Debugf("Inferior ERR: <%s>", stderr)
413                                 }
414                         })
415                 }
416
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])
426                                         } else {
427                                                 return cli.NewExitError(
428                                                         fmt.Errorf("Invalid definition in XDS_OVERWRITE_COMMANDS (%s)", def),
429                                                         int(syscall.EINVAL))
430                                         }
431                                 }
432                         }
433                 } else {
434                         overwriteMap["-exec-run"] = "-exec-continue"
435                         overwriteMap["-file-exec-and-symbols"] = "-file-exec-file"
436                 }
437                 log.Debugf("overwriteMap = %v", overwriteMap)
438
439                 // Send stdin though WS
440                 go func() {
441                         paranoia := 600
442                         reader := bufio.NewReader(os.Stdin)
443
444                         // Enable workaround to correctly close connection
445                         // except if XDS_GDBSERVER_EXIT_NOFIX is defined
446                         _, gdbExitNoFix := os.LookupEnv("XDS_GDBSERVER_EXIT_NOFIX")
447
448                         for {
449                                 sc := bufio.NewScanner(reader)
450                                 for sc.Scan() {
451                                         command := sc.Text()
452
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)
458                                                 }
459                                         }
460
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())
466                                                 }
467                                                 time.Sleep(time.Millisecond * 200)
468                                         }
469
470                                         log.Debugf("Send: <%v>", command)
471                                         gdb.Write(command + "\n")
472                                 }
473                                 log.Infof("Stdin scanner exit, close stdin (err=%v)", sc.Err())
474
475                                 // CTRL-D exited scanner, so send it explicitly
476                                 gdb.Write("\x04")
477                                 time.Sleep(time.Millisecond * 100)
478
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)}
484                                 }
485                         }
486                 }()
487
488                 // Handling all Signals
489                 sigs := make(chan os.Signal, 1)
490                 signal.Notify(sigs)
491
492                 go func() {
493                         for {
494                                 sig := <-sigs
495
496                                 if isIgnoredSignal(sig) {
497                                         return
498                                 }
499
500                                 if err := gdb.SendSignal(sig); err != nil {
501                                         log.Errorf("Error while sending signal %v : %s", sig, err.Error())
502                                 }
503                         }
504                 }()
505
506                 // Start gdb
507                 if code, err := gdb.Start(clientPty != ""); err != nil {
508                         return cli.NewExitError(err.Error(), code)
509                 }
510
511                 // Wait exit
512                 select {
513                 case res := <-exitChan:
514                         errStr := ""
515                         if res.code == 0 {
516                                 log.Infoln("Exit successfully")
517                         }
518                         if res.error != nil {
519                                 log.Infoln("Exit with ERROR: ", res.error.Error())
520                                 errStr = res.error.Error()
521                         }
522                         return cli.NewExitError(errStr, res.code)
523                 }
524         }
525
526         app.Run(args)
527 }
528
529 // loadConfigEnvFile
530 func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string, error) {
531         var err error
532         envMap := make(map[string]string)
533
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)
539                 if confFile != "" {
540                         defer os.Remove(confFile)
541                 }
542                 if err != nil {
543                         log.Infof("Extraction from gdbCmdFile failed: %v", err.Error())
544                 }
545         }
546         // 2- search xds-gdb.env file in various locations
547         if confFile == "" {
548                 curDir, _ := os.Getwd()
549                 if u, err := user.Current(); err == nil {
550                         xdsEnvFile := "xds-gdb.env"
551                         for _, d := range []string{
552                                 path.Join(curDir),
553                                 path.Join(curDir, ".."),
554                                 path.Join(curDir, "target"),
555                                 path.Join(u.HomeDir, ".config", "xds"),
556                         } {
557                                 cf := path.Join(d, xdsEnvFile)
558                                 log.Infof("Search config in %s", cf)
559                                 if common.Exists(cf) {
560                                         confFile = cf
561                                         break
562                                 }
563                         }
564                 }
565         }
566
567         if confFile == "" {
568                 log.Infof("NO valid conf file found!")
569                 return envMap, "", nil
570         }
571
572         if !common.Exists(confFile) {
573                 return envMap, confFile, fmt.Errorf("Error no env config file not found")
574         }
575         if err = godotenv.Load(confFile); err != nil {
576                 return envMap, confFile, fmt.Errorf("Error loading env config file " + confFile)
577         }
578         if envMap, err = godotenv.Read(confFile); err != nil {
579                 return envMap, confFile, fmt.Errorf("Error reading env config file " + confFile)
580         }
581
582         return envMap, confFile, nil
583 }
584
585 /*
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
589   are supported:
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
593 */
594 func extractEnvFromCmdFile(cmdFile string) (string, error) {
595         if !common.Exists(cmdFile) {
596                 return "", nil
597         }
598         cFd, err := os.Open(cmdFile)
599         if err != nil {
600                 return "", fmt.Errorf("Cannot open %s : %s", cmdFile, err.Error())
601         }
602         defer cFd.Close()
603
604         var lines []string
605         scanner := bufio.NewScanner(cFd)
606         for scanner.Scan() {
607                 lines = append(lines, scanner.Text())
608         }
609         if err = scanner.Err(); err != nil {
610                 return "", fmt.Errorf("Cannot parse %s : %s", cmdFile, err.Error())
611         }
612
613         envFile, err := ioutil.TempFile("", "xds-gdb_env.ini")
614         if err != nil {
615                 return "", fmt.Errorf("Error while creating temporary env file: %s", err.Error())
616         }
617         envFileName := envFile.Name()
618         defer envFile.Close()
619
620         envFound := false
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)
625                         if len(env) == 2 {
626                                 envFound = true
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())
629                                 }
630                         } else {
631                                 log.Warnf("Error while decoding line %s", ln)
632                         }
633                 }
634         }
635
636         if !envFound {
637                 ff := envFileName
638                 defer os.Remove(ff)
639                 envFileName = ""
640
641         }
642
643         return envFileName, nil
644 }