Update connection env vars XDS_AGENT_URL / XDS_SERVER_URL.
[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:8000"
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                         fmt.Println("Disconnection: ", err.Error())
344                         exitChan <- exitResult{err, int(syscall.ESHUTDOWN)}
345                 })
346
347                 gdb.Read(func(timestamp, stdout, stderr string) {
348                         if stdout != "" {
349                                 fmt.Printf("%s", stdout)
350                                 log.Debugf("Recv OUT: <%s>", stdout)
351                         }
352                         if stderr != "" {
353                                 fmt.Fprintf(os.Stderr, "%s", stderr)
354                                 log.Debugf("Recv ERR: <%s>", stderr)
355                         }
356
357                         // Correctly report error about init file
358                         if gdbCommandFileError != "" && strings.Contains(stdout, gdbCommandFileError) {
359                                 fmt.Fprintf(os.Stderr, "ERROR: "+gdbCommandFileError)
360                                 log.Errorf("ERROR: " + gdbCommandFileError)
361                                 if err := gdb.SendSignal(syscall.SIGTERM); err != nil {
362                                         log.Errorf("Error while sending signal: %s", err.Error())
363                                 }
364                                 exitChan <- exitResult{err, int(syscall.ENOENT)}
365                         }
366                 })
367
368                 gdb.OnExit(func(code int, err error) {
369                         exitChan <- exitResult{err, code}
370                 })
371
372                 // Handle client tty / pts
373                 if clientPty != "" {
374                         log.Infoln("Client tty detected: %v", clientPty)
375
376                         cpFd, err := os.OpenFile(clientPty, os.O_RDWR, 0)
377                         if err != nil {
378                                 return cli.NewExitError(err.Error(), int(syscall.EPERM))
379                         }
380                         defer cpFd.Close()
381
382                         // client tty stdin
383                         /* XXX TODO - implement stdin to send data to debugged program
384                         go func() {
385                                 reader := bufio.NewReader(cpFd)
386                                 sc := bufio.NewScanner(reader)
387                                 for sc.Scan() {
388                                         data := sc.Text()
389                                         iosk.Emit(xaapiv1.ExecInferiorInEvent, data+"\n")
390                                         log.Debugf("Inferior IN: <%v>", data)
391                                 }
392                                 if sc.Err() != nil {
393                                         log.Warnf("Inferior Stdin scanner exit, close stdin (err=%v)", sc.Err())
394                                 }
395                         }()
396                         */
397
398                         // client tty stdout
399                         gdb.InferiorRead(func(timestamp, stdout, stderr string) {
400                                 if stdout != "" {
401                                         fmt.Fprintf(cpFd, "%s", stdout)
402                                         log.Debugf("Inferior OUT: <%s>", stdout)
403                                 }
404                                 if stderr != "" {
405                                         fmt.Fprintf(cpFd, "%s", stderr)
406                                         log.Debugf("Inferior ERR: <%s>", stderr)
407                                 }
408                         })
409                 }
410
411                 // Allow to overwrite some gdb commands
412                 var overwriteMap = make(map[string]string)
413                 if overEnv, exist := os.LookupEnv("XDS_OVERWRITE_COMMANDS"); exist {
414                         overEnvS := strings.TrimSpace(overEnv)
415                         if len(overEnvS) > 0 {
416                                 // Extract overwrite commands from env variable
417                                 for _, def := range strings.Split(overEnvS, ",") {
418                                         if kv := strings.Split(def, ":"); len(kv) == 2 {
419                                                 overwriteMap[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
420                                         } else {
421                                                 return cli.NewExitError(
422                                                         fmt.Errorf("Invalid definition in XDS_OVERWRITE_COMMANDS (%s)", def),
423                                                         int(syscall.EINVAL))
424                                         }
425                                 }
426                         }
427                 } else {
428                         overwriteMap["-exec-run"] = "-exec-continue"
429                         overwriteMap["-file-exec-and-symbols"] = "-file-exec-file"
430                 }
431                 log.Debugf("overwriteMap = %v", overwriteMap)
432
433                 // Send stdin though WS
434                 go func() {
435                         paranoia := 600
436                         reader := bufio.NewReader(os.Stdin)
437
438                         // Enable workaround to correctly close connection
439                         // except if XDS_GDBSERVER_EXIT_NOFIX is defined
440                         _, gdbExitNoFix := os.LookupEnv("XDS_GDBSERVER_EXIT_NOFIX")
441
442                         for {
443                                 sc := bufio.NewScanner(reader)
444                                 for sc.Scan() {
445                                         command := sc.Text()
446
447                                         // overwrite some commands
448                                         for key, value := range overwriteMap {
449                                                 if strings.Contains(command, key) {
450                                                         command = strings.Replace(command, key, value, 1)
451                                                         log.Debugf("OVERWRITE %s -> %s", key, value)
452                                                 }
453                                         }
454
455                                         // Send SIGINT to stop debugged process execution before sending -gdb-exit command
456                                         if !gdbExitNoFix && strings.Contains(command, "-gdb-exit") {
457                                                 log.Infof("Detection of -gdb-exit, exiting...")
458                                                 if err := gdb.SendSignal(syscall.SIGINT); err != nil {
459                                                         log.Errorf("Error while sending signal SIGINT : %s", err.Error())
460                                                 }
461                                                 time.Sleep(time.Millisecond * 200)
462                                         }
463
464                                         gdb.Write(command + "\n")
465                                         log.Debugf("Send: <%v>", command)
466                                 }
467                                 log.Infof("Stdin scanner exit, close stdin (err=%v)", sc.Err())
468
469                                 // CTRL-D exited scanner, so send it explicitly
470                                 gdb.Write("\x04")
471                                 time.Sleep(time.Millisecond * 100)
472
473                                 if paranoia--; paranoia <= 0 {
474                                         msg := "Abnormal loop detected on stdin"
475                                         log.Errorf("Abnormal loop detected on stdin")
476                                         gdb.SendSignal(syscall.SIGTERM)
477                                         exitChan <- exitResult{fmt.Errorf(msg), int(syscall.ELOOP)}
478                                 }
479                         }
480                 }()
481
482                 // Handling all Signals
483                 sigs := make(chan os.Signal, 1)
484                 signal.Notify(sigs)
485
486                 go func() {
487                         for {
488                                 sig := <-sigs
489
490                                 if isIgnoredSignal(sig) {
491                                         return
492                                 }
493
494                                 if err := gdb.SendSignal(sig); err != nil {
495                                         log.Errorf("Error while sending signal %v : %s", sig, err.Error())
496                                 }
497                         }
498                 }()
499
500                 // Start gdb
501                 if code, err := gdb.Start(clientPty != ""); err != nil {
502                         return cli.NewExitError(err.Error(), code)
503                 }
504
505                 // Wait exit
506                 select {
507                 case res := <-exitChan:
508                         errStr := ""
509                         if res.code == 0 {
510                                 log.Infoln("Exit successfully")
511                         }
512                         if res.error != nil {
513                                 log.Infoln("Exit with ERROR: ", res.error.Error())
514                                 errStr = res.error.Error()
515                         }
516                         return cli.NewExitError(errStr, res.code)
517                 }
518         }
519
520         app.Run(args)
521 }
522
523 // loadConfigEnvFile
524 func loadConfigEnvFile(confFile, gdbCmdFile string) (map[string]string, string, error) {
525         var err error
526         envMap := make(map[string]string)
527
528         // 1- if no confFile set, use setting from gdb command file is option
529         //    --command/-x is set
530         if confFile == "" && gdbCmdFile != "" {
531                 log.Infof("Try extract config from gdbCmdFile: %s", gdbCmdFile)
532                 confFile, err = extractEnvFromCmdFile(gdbCmdFile)
533                 if confFile != "" {
534                         defer os.Remove(confFile)
535                 }
536                 if err != nil {
537                         log.Infof("Extraction from gdbCmdFile failed: %v", err.Error())
538                 }
539         }
540         // 2- search xds-gdb.env file in various locations
541         if confFile == "" {
542                 curDir, _ := os.Getwd()
543                 if u, err := user.Current(); err == nil {
544                         xdsEnvFile := "xds-gdb.env"
545                         for _, d := range []string{
546                                 path.Join(curDir),
547                                 path.Join(curDir, ".."),
548                                 path.Join(curDir, "target"),
549                                 path.Join(u.HomeDir, ".config", "xds"),
550                         } {
551                                 cf := path.Join(d, xdsEnvFile)
552                                 log.Infof("Search config in %s", cf)
553                                 if common.Exists(cf) {
554                                         confFile = cf
555                                         break
556                                 }
557                         }
558                 }
559         }
560
561         if confFile == "" {
562                 log.Infof("NO valid conf file found!")
563                 return envMap, "", nil
564         }
565
566         if !common.Exists(confFile) {
567                 return envMap, confFile, fmt.Errorf("Error no env config file not found")
568         }
569         if err = godotenv.Load(confFile); err != nil {
570                 return envMap, confFile, fmt.Errorf("Error loading env config file " + confFile)
571         }
572         if envMap, err = godotenv.Read(confFile); err != nil {
573                 return envMap, confFile, fmt.Errorf("Error reading env config file " + confFile)
574         }
575
576         return envMap, confFile, nil
577 }
578
579 /*
580  extractEnvFromCmdFile: extract xds-gdb env variable from gdb command file
581   All commented lines (#) in gdb command file that start with ':XDS-ENV:' prefix
582   will be considered as XDS env commands. For example the 3 syntaxes below
583   are supported:
584   # :XDS-ENV: XDS_PROJECT_ID=4021617e-ced0-11e7-acd2-3c970e49ad9b
585   #:XDS-ENV:XDS_SDK_ID=06c0e95a-e215-3a5a-b373-f677c0dabd3b
586   # :XDS-ENV:  export XDS_AGENT_URL=localhost:8800
587 */
588 func extractEnvFromCmdFile(cmdFile string) (string, error) {
589         if !common.Exists(cmdFile) {
590                 return "", nil
591         }
592         cFd, err := os.Open(cmdFile)
593         if err != nil {
594                 return "", fmt.Errorf("Cannot open %s : %s", cmdFile, err.Error())
595         }
596         defer cFd.Close()
597
598         var lines []string
599         scanner := bufio.NewScanner(cFd)
600         for scanner.Scan() {
601                 lines = append(lines, scanner.Text())
602         }
603         if err = scanner.Err(); err != nil {
604                 return "", fmt.Errorf("Cannot parse %s : %s", cmdFile, err.Error())
605         }
606
607         envFile, err := ioutil.TempFile("", "xds-gdb_env.ini")
608         if err != nil {
609                 return "", fmt.Errorf("Error while creating temporary env file: %s", err.Error())
610         }
611         envFileName := envFile.Name()
612         defer envFile.Close()
613
614         envFound := false
615         for _, ln := range lines {
616                 ln = strings.TrimSpace(ln)
617                 if strings.HasPrefix(ln, "#") && strings.Contains(ln, ":XDS-ENV:") {
618                         env := strings.SplitAfterN(ln, ":XDS-ENV:", 2)
619                         if len(env) == 2 {
620                                 envFound = true
621                                 if _, err := envFile.WriteString(strings.TrimSpace(env[1]) + "\n"); err != nil {
622                                         return "", fmt.Errorf("Error write into temporary env file: %s", err.Error())
623                                 }
624                         } else {
625                                 log.Warnf("Error while decoding line %s", ln)
626                         }
627                 }
628         }
629
630         if !envFound {
631                 ff := envFileName
632                 defer os.Remove(ff)
633                 envFileName = ""
634
635         }
636
637         return envFileName, nil
638 }