Exemplo n.º 1
0
        public JObject Format_0_0_3(string fileName, JObject payload, string hash)
        {
            var event_id    = new JProperty("event_id", Guid.NewGuid().ToString("n"));
            var timestamp   = new JProperty("timestamp", DateTime.UtcNow.ToString("s", System.Globalization.CultureInfo.InvariantCulture));
            var exc_objs    = new List <JObject> ();
            var thread_objs = new List <JObject> ();

            var stackTraces = payload["threads"] as JArray;

            var path = Path.GetDirectoryName(fileName);              // Best differentiator in-tree for files is where they are run from

            foreach (var st in stackTraces)
            {
                var thread_id = st["native_thread_id"]?.ToString();

                var unmanaged_frames = new List <JObject>();
                var managed_frames   = new List <JObject>();
                var exceptions       = new List <JObject>();

                var thread_name = st["thread_name"]?.ToString();
                if (thread_name == null || thread_name?.Length == 0)
                {
                    thread_name = "Unnamed thread";
                }

                var payload_unmanaged_frames = st["unmanaged_frames"] as JArray;
                for (int fr = 0; payload_unmanaged_frames != null && fr < payload_unmanaged_frames.Count; fr++)
                {
                    var frame          = payload_unmanaged_frames [fr] as JObject;
                    var native_address = frame["native_address"];
                    var unmanaged_name = frame["unmanaged_name"] != null ? frame["unmanaged_name"].ToString() : "";

                    var fn_filename = new JProperty("filename", "");
                    var function    = new JProperty("function", unmanaged_name);
                    var module      = new JProperty("module", "mono-sgen");
                    var vars        = new JProperty("vars", new JObject(new JProperty("native_address", native_address)));
                    var blob        = new JObject(fn_filename, function, module, vars);

                    unmanaged_frames.Add(blob);
                }

                var payload_managed_frames = st["managed_frames"] as JArray;
                for (int fr = 0; payload_managed_frames != null && fr < payload_managed_frames.Count; fr++)
                {
                    var frame = payload_managed_frames [fr] as JObject;
                    if (frame["is_managed"] != null && frame["is_managed"].ToString().ToUpper() == "TRUE")
                    {
                        var guid_val   = frame["guid"].ToString();
                        var token_val  = Convert.ToUInt32(frame["token"].ToString(), 16);
                        var offset_val = Convert.ToUInt32(frame["il_offset"].ToString(), 16);

                        var output_frame = codebase.Find(guid_val, token_val, offset_val);
                        if (output_frame == null)
                        {
                            continue;
                        }

                        var guid      = new JProperty("guid", guid_val);
                        var token     = new JProperty("token", token_val);
                        var il_offset = new JProperty("il_offset", offset_val);

                        output_frame.Add(new JProperty("vars", new JObject(guid, token, il_offset)));

                        managed_frames.Add(output_frame);
                    }
                    else
                    {
                        var native_address = frame["native_address"];
                        var unmanaged_name = frame["unmanaged_name"] != null ? frame["unmanaged_name"].ToString() : "";

                        var fn_filename = new JProperty("filename", "mono-sgen");
                        var function    = new JProperty("function", unmanaged_name);
                        var module      = new JProperty("module", "");
                        var vars        = new JProperty("vars", frame);
                        var blob        = new JObject(fn_filename, function, module, vars);

                        managed_frames.Add(blob);
                    }
                }

                var payload_exceptions = st["exceptions"] as JArray;
                for (int exc = 0; payload_exceptions != null && exc < payload_exceptions.Count; exc++)
                {
                    var exception_obj            = payload_exceptions [exc] as JObject;
                    var exc_managed_frames       = new List <JObject>();
                    var payload_exception_frames = exception_obj ["managed_frames"] as JArray;
                    for (int fr = 0; payload_exception_frames != null && fr < payload_exception_frames.Count; fr++)
                    {
                        var frame = payload_exception_frames [fr] as JObject;
                        if (frame["is_managed"] != null && frame["is_managed"].ToString().ToUpper() == "TRUE")
                        {
                            var guid_val   = frame["guid"].ToString();
                            var token_val  = Convert.ToUInt32(frame["token"].ToString(), 16);
                            var offset_val = Convert.ToUInt32(frame["il_offset"].ToString(), 16);

                            var output_frame = codebase.Find(guid_val, token_val, offset_val);
                            if (output_frame == null)
                            {
                                continue;
                            }

                            var guid      = new JProperty("guid", guid_val);
                            var token     = new JProperty("token", token_val);
                            var il_offset = new JProperty("il_offset", offset_val);

                            output_frame.Add(new JProperty("vars", new JObject(guid, token, il_offset)));

                            exc_managed_frames.Add(output_frame);
                        }
                        else
                        {
                            var native_address = frame["native_address"];
                            var unmanaged_name = frame["unmanaged_name"] != null ? frame["unmanaged_name"].ToString() : "";

                            var fn_filename = new JProperty("filename", "mono-sgen");
                            var function    = new JProperty("function", unmanaged_name);
                            var module      = new JProperty("module", "");
                            var vars        = new JProperty("vars", frame);
                            var blob        = new JObject(fn_filename, function, module, vars);

                            exc_managed_frames.Add(blob);
                        }
                    }

                    var type       = new JProperty("type", exception_obj ["type"].ToString());
                    var managed_st = new JProperty("frames", new JArray(exc_managed_frames.ToArray()));
                    var exc_blob   = new JObject(type, managed_st);

                    exceptions.Add(exc_blob);
                }

                if (unmanaged_frames.Count > 0)
                {
                    var unmanaged_st = new JObject(new JProperty("frames", new JArray(unmanaged_frames.ToArray())));
                    var id           = String.Format("{0}_unmanaged", st ["native_thread_id"]);
                    var active       = new JProperty("active", "true");

                    if (st["crashed"]?.ToString().ToUpper() == "TRUE")
                    {
                        var unmanaged_thread = new JObject(active, new JProperty("crashed", "true"), new JProperty("name", String.Format("{0} unmanaged", thread_name)), new JProperty("id", id));
                        var unmanaged_exc    = new JObject(new JProperty("module", String.Format("{0}_managed_frames", thread_id)),
                                                           new JProperty("type", path),
                                                           new JProperty("value", ""),
                                                           new JProperty("stacktrace", unmanaged_st), new JProperty("thread_id", id));

                        thread_objs.Add(unmanaged_thread);
                        exc_objs.Add(unmanaged_exc);
                    }
                    else
                    {
                        var unmanaged_thread = new JObject(active, new JProperty("name", String.Format("{0} Unmanaged", thread_name)),
                                                           new JProperty("id", id), new JProperty("stacktrace", unmanaged_st));
                        thread_objs.Add(unmanaged_thread);
                    }
                }

                if (managed_frames.Count > 0)
                {
                    var managed_st = new JObject(new JProperty("frames", new JArray(managed_frames.ToArray())));
                    // If we are the crashing thread, set the exception object to the
                    // managed stacktrace and the thread object to the managed thread
                    //
                    // If we aren't, add the thread + st to
                    var id     = String.Format("{0}_managed", st["native_thread_id"]);
                    var active = new JProperty("active", "true");

                    if (unmanaged_frames.Count == 0 && st["crashed"]?.ToString().ToUpper() == "TRUE")
                    {
                        var managed_thread = new JObject(active, new JProperty("crashed", "true"), new JProperty("name", String.Format("{0} managed", thread_name)), new JProperty("id", id));
                        var managed_exc    = new JObject(new JProperty("module", String.Format("{0}_managed_frames", thread_id)),
                                                         new JProperty("type", path),
                                                         new JProperty("value", ""),
                                                         new JProperty("stacktrace", managed_st), new JProperty("thread_id", id));

                        thread_objs.Add(managed_thread);
                        exc_objs.Add(managed_exc);
                    }
                    else
                    {
                        var managed_thread = new JObject(active, new JProperty("name", String.Format("{0} managed", thread_name)),
                                                         new JProperty("id", id), new JProperty("stacktrace", managed_st));
                        thread_objs.Add(managed_thread);
                    }
                }

                bool first_exception = true;
                foreach (var exc_input in exceptions)
                {
                    // The 0.0.3 managed exception only has one thread, so we want it to be active and set crashed to true.
                    var active = new JProperty("active", first_exception ? "true" : "false");
                    first_exception = false;

                    var name = "Caught Managed Exception";

                    var exception_thread = new JObject(active, new JProperty("crashed", "true"), new JProperty("name", name), new JProperty("id", name), new JProperty("exception", exc_input));
                    var exception_exc    = new JObject(new JProperty("module", name),
                                                       new JProperty("type", path),
                                                       new JProperty("value", ""),
                                                       new JProperty("stacktrace", exc_input ["frames"]));

                    thread_objs.Add(exception_thread);
                    exc_objs.Add(exception_exc);
                }
            }

            var exception = new JProperty("exception", new JObject(new JProperty("values", new JArray(exc_objs.ToArray()))));
            var threads   = new JProperty("threads", new JObject(new JProperty("values", new JArray(thread_objs.ToArray()))));
            // Bake in the whole blob
            var embedded    = new JProperty("extra", payload);
            var fingerprint = new JProperty("fingerprint", new JArray(new JValue(hash)));

            var sentry_message = new JObject(timestamp, event_id, exception, embedded, threads, fingerprint);

            return(sentry_message);
        }
Exemplo n.º 2
0
        public void Upload(string filePath, string os_tag, Dsn url)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception(String.Format("Json file not found {0}", filePath));
            }

            var dump    = File.ReadAllText(filePath);
            var message = new SentryMessage(dump);
            // var blob = new SentryEvent(message);
            var payload = JObject.Parse(dump);

            // Try to extract a test name
            var fileName  = Path.GetFileName(filePath);
            var groups    = Regex.Match(fileName, @"mono_crash.(\d+).(\d+).json").Groups;
            var hash      = groups[1].Value;
            var increment = groups[2].Value;

            // var version_string = Regex.Match(payload["configuration"]["version"].ToString(), @"").Groups;

            var event_id     = new JProperty("event_id", Guid.NewGuid().ToString("n"));
            var timestamp    = new JProperty("timestamp", DateTime.UtcNow.ToString("s", System.Globalization.CultureInfo.InvariantCulture));
            var exc_objs     = new List <JObject> ();
            var failure_type = "Unhandled Managed Exception";

            var stackTraces = payload["threads"] as JArray;

            if (stackTraces.Count > 1)
            {
                failure_type = "Unhandled Unmanaged Exception";
            }

            var culprit = new JProperty("culprit", failure_type);


            for (int i = 0; i < stackTraces.Count; i++)
            {
                var thread_id          = stackTraces[i]["native_thread_id"].ToString();
                var managed_frame_name = "";

                var unmanaged_frames = new List <JObject>();
                var managed_frames   = new List <JObject>();

                var payload_unmanaged_frames = stackTraces[i]["managed_frames"] as JArray;
                for (int fr = 0; payload_unmanaged_frames != null && fr < payload_unmanaged_frames.Count; fr++)
                {
                    var fn_filename = new JProperty("filename", "unknown");
                    var module      = new JProperty("module", "mono-sgen");
                    var function    = new JProperty("function", "");

                    unmanaged_frames.Add(new JObject(fn_filename, function, module));
                }

                var payload_managed_frames = stackTraces[i]["managed_frames"] as JArray;
                for (int fr = 0; payload_managed_frames != null && fr < payload_managed_frames.Count; fr++)
                {
                    var frame = payload_managed_frames [fr] as JObject;
                    if (frame["is_managed"] != null && frame["is_managed"].ToString() == "true")
                    {
                        var guid_val   = frame["guid"].ToString();
                        var token_val  = Convert.ToUInt32(frame["token"].ToString(), 16);
                        var offset_val = Convert.ToUInt32(frame["il_offset"].ToString(), 16);

                        var output_frame = codebase.Find(guid_val, token_val, offset_val);
                        if (output_frame == null)
                        {
                            continue;
                        }

                        var guid      = new JProperty("guid", guid_val);
                        var token     = new JProperty("token", token_val);
                        var il_offset = new JProperty("il_offset", offset_val);

                        output_frame.Add(new JProperty("vars", new JObject(guid, token, il_offset)));

                        managed_frames.Add(output_frame);
                    }
                    else
                    {
                        var native_address = frame["native_address"].ToString();
                        var unmanaged_name = frame["unmanaged_name"].ToString();

                        var fn_filename = new JProperty("filename", "native");
                        var function    = new JProperty("function", string.Format("Offset {0}", unmanaged_name));
                        var module      = new JProperty("module", unmanaged_name);
                        var vars        = new JProperty("vars", frame);

                        managed_frames.Add(new JObject(fn_filename, function, module));
                    }
                }

                if (managed_frames.Count > 0)
                {
                    var managed_st = new JObject(new JProperty("frames", new JArray(managed_frames.ToArray())));

                    exc_objs.Add(new JObject(
                                     new JProperty("module", String.Format("{0}_managed_frames", thread_id)),
                                     new JProperty("type", failure_type),
                                     new JProperty("value", managed_frame_name),
                                     new JProperty("stacktrace", managed_st)));
                }

                if (unmanaged_frames.Count > 0)
                {
                    var unmanaged_st = new JObject(new JProperty("frames", new JArray(unmanaged_frames.ToArray())));

                    exc_objs.Add(new JObject(
                                     new JProperty("module", String.Format("{0}_unmanaged_frames", thread_id)),
                                     new JProperty("type", failure_type),
                                     new JProperty("value", managed_frame_name),
                                     new JProperty("stacktrace", unmanaged_st)));
                }
            }

            var exception = new JProperty("exception", new JArray(exc_objs.ToArray()));
            // Bake in the whole blob
            var embedded       = new JProperty("extra", payload);
            var sentry_message = new JObject(timestamp, event_id, culprit, exception, embedded);

            // sent to url via post?
            Console.WriteLine(sentry_message);

            var request = (HttpWebRequest)WebRequest.Create(url.SentryUri);

            request.Method      = "POST";
            request.ContentType = "application/json";
            request.UserAgent   = PacketBuilder.UserAgent;

            var header = PacketBuilder.CreateAuthenticationHeader(url);

            request.Headers ["X-Sentry-Auth"] = header;

            byte[] byteArray = Encoding.UTF8.GetBytes(sentry_message.ToString());
            request.ContentLength = byteArray.Length;

            Stream dataStream = request.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            Console.WriteLine("break");

            try {
                WebResponse response = request.GetResponse();
                // Display the status.
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);

                StreamReader reader = new StreamReader(response.GetResponseStream());
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                Console.WriteLine(responseFromServer);
                // Clean up the streams.
            } catch (WebException ex) {
                Console.WriteLine("{0}", ex.Response.Headers [""]);
            }
        }