Merge pull request #3353 from esdrubal/st-format
[mono.git] / mcs / tools / mono-symbolicate / symbolicate.cs
1 using System;
2 using System.IO;
3 using System.Text;
4 using System.Linq;
5 using System.Collections.Generic;
6 using System.Globalization;
7 using Mono.Options;
8
9 namespace Mono
10 {
11         public class Symbolicate
12         {
13                 class Command {
14                         public readonly int MinArgCount;
15                         public readonly int MaxArgCount;
16                         public readonly Action<List<string>> Action;
17
18                         public Command (Action<List<string>> action, int minArgCount = 0, int maxArgCount = int.MaxValue)
19                         {
20                                 Action = action;
21                                 MinArgCount = minArgCount;
22                                 MaxArgCount = maxArgCount;
23                         }
24                 }
25
26                 static Logger logger;
27
28                 public static int Main (String[] args)
29                 {
30                         var showHelp = false;
31                         List<string> extra = null;
32
33                         Command cmd = null;
34
35                         var logLevel = Logger.Level.Warning;
36
37                         var options = new OptionSet {
38                                 { "h|help", "Show this help", v => showHelp = true },
39                                 { "q", "Quiet, warnings are not displayed", v => logLevel = Logger.Level.Error },
40                                 { "v", "Verbose, log debug messages", v => logLevel = Logger.Level.Debug },
41                         };
42
43                         try {
44                                 extra = options.Parse (args);
45                         } catch (OptionException e) {
46                                 Console.WriteLine ("Option error: {0}", e.Message);
47                                 showHelp = true;
48                         }
49
50                         if (extra.Count > 0 && extra[0] == "store-symbols")
51                                 cmd = new Command (StoreSymbolsAction, 2);
52
53                         if (cmd != null) {
54                                 extra.RemoveAt (0);
55                         } else {
56                                 cmd = new Command (SymbolicateAction, 2, 2);
57                         }
58
59                         if (showHelp || extra == null || extra.Count < cmd.MinArgCount || extra.Count > cmd.MaxArgCount) {
60                                 Console.Error.WriteLine ("Usage: symbolicate [options] <msym dir> <input file>");
61                                 Console.Error.WriteLine ("       symbolicate [options] store-symbols <msym dir> [<dir>]+");
62                                 Console.WriteLine ();
63                                 Console.WriteLine ("Available options:");
64                                 options.WriteOptionDescriptions (Console.Out);
65                                 return 1;
66                         }
67
68                         logger = new Logger (logLevel, msg => Console.Error.WriteLine (msg));
69
70                         cmd.Action (extra);
71
72                         return 0;
73                 }
74
75                 private static void SymbolicateAction (List<string> args)
76                 {
77                         var msymDir = args [0];
78                         var inputFile = args [1];
79
80                         var symbolManager = new SymbolManager (msymDir, logger);
81
82                         using (StreamReader r = new StreamReader (inputFile)) {
83                                 for (var line = r.ReadLine (); line != null; line = r.ReadLine ()) {
84                                         StackFrameData sfData;
85                                         if (StackFrameData.TryParse (line, out sfData) &&
86                                                 symbolManager.TryResolveLocation (sfData)) {
87                                                 Console.WriteLine (sfData.ToString ());
88                                                 continue;
89                                         }
90
91                                         Console.WriteLine (line);
92                                 }
93                         }
94                 }
95
96                 private static void StoreSymbolsAction (List<string> args)
97                 {
98                         var msymDir = args[0];
99                         var lookupDirs = args.Skip (1).ToArray ();
100
101                         var symbolManager = new SymbolManager (msymDir, logger);
102
103                         symbolManager.StoreSymbols (lookupDirs);
104                 }
105         }
106 }