Skip to content

Commit

Permalink
Serialize addresses as strings (#1692)
Browse files Browse the repository at this point in the history
* Serialize addresses as strings

* Update CHANGELOG.md
  • Loading branch information
mattjohnsonpint committed Jun 7, 2022
1 parent 0b1f8df commit 7c48687
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Improve timestamp precision of transactions and spans ([#1680](https://github.com/getsentry/sentry-dotnet/pull/1680))
- Flatten AggregateException ([#1672](https://github.com/getsentry/sentry-dotnet/pull/1672))
- NOTE: This can affect grouping. You can keep the original behavior by setting the option `KeepAggregateException` to `true`.
- Serialize stack frame addresses as strings. ([#1692](https://github.com/getsentry/sentry-dotnet/pull/1692))

### Features

Expand Down
8 changes: 4 additions & 4 deletions src/Sentry/Exceptions/SentryStackFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
writer.WriteBooleanIfNotNull("in_app", InApp);
writer.WriteStringIfNotWhiteSpace("package", Package);
writer.WriteStringIfNotWhiteSpace("platform", Platform);
writer.WriteNumberIfNotNull("image_addr", ImageAddress.NullIfDefault());
writer.WriteNumberIfNotNull("symbol_addr", SymbolAddress);
writer.WriteStringIfNotWhiteSpace("image_addr", ImageAddress.NullIfDefault()?.ToHexString());
writer.WriteStringIfNotWhiteSpace("symbol_addr", SymbolAddress?.ToHexString());
writer.WriteStringIfNotWhiteSpace("instruction_addr", InstructionAddress);
writer.WriteNumberIfNotNull("instruction_offset", InstructionOffset);
writer.WriteStringIfNotWhiteSpace("addr_mode", AddressMode);
Expand Down Expand Up @@ -212,8 +212,8 @@ public static SentryStackFrame FromJson(JsonElement json)
var inApp = json.GetPropertyOrNull("in_app")?.GetBoolean();
var package = json.GetPropertyOrNull("package")?.GetString();
var platform = json.GetPropertyOrNull("platform")?.GetString();
var imageAddress = json.GetPropertyOrNull("image_addr")?.GetInt64() ?? 0;
var symbolAddress = json.GetPropertyOrNull("symbol_addr")?.GetInt64();
var imageAddress = json.GetPropertyOrNull("image_addr")?.GetAddressAsLong() ?? 0;
var symbolAddress = json.GetPropertyOrNull("symbol_addr")?.GetAddressAsLong();
var instructionAddress = json.GetPropertyOrNull("instruction_addr")?.GetString();
var instructionOffset = json.GetPropertyOrNull("instruction_offset")?.GetInt64();
var addressMode = json.GetPropertyOrNull("addr_mode")?.GetString();
Expand Down
30 changes: 30 additions & 0 deletions src/Sentry/Internal/Extensions/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,36 @@ public static void Deconstruct(this JsonProperty jsonProperty, out string name,
return double.Parse(json.ToString()!);
}

public static long? GetAddressAsLong(this JsonElement json)
{
// If the address is in json as a number, we can just use it.
if (json.ValueKind == JsonValueKind.Number)
{
return json.GetInt64();
}

// Otherwise it will be a string, but we need to convert it to a number.
var s = json.GetString();
if (s == null)
{
return null;
}

// It should be in hex format, such as "0x7fff5bf346c0"
#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER
var substring = s[2..];
#else
var substring = s.Substring(2);
#endif
if (s.StartsWith("0x") &&
long.TryParse(substring, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var result))
{
return result;
}

throw new FormatException();
}

public static string GetStringOrThrow(this JsonElement json) =>
json.GetString() ?? throw new InvalidOperationException("JSON string is null.");

Expand Down
4 changes: 4 additions & 0 deletions src/Sentry/Internal/Extensions/MiscExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Sentry.Internal.Extensions
{
Expand All @@ -11,5 +12,8 @@ internal static class MiscExtensions
!EqualityComparer<T>.Default.Equals(value, default)
? value
: null;

public static string ToHexString(this long l) =>
"0x" + l.ToString("x", CultureInfo.InvariantCulture);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public void SerializeObject_AllPropertiesSetToNonDefault_SerializesValidObject()
"\"in_app\":true," +
"\"package\":\"Package\"," +
"\"platform\":\"Platform\"," +
"\"image_addr\":3," +
"\"symbol_addr\":4," +
"\"image_addr\":\"0x3\"," +
"\"symbol_addr\":\"0x4\"," +
"\"instruction_addr\":\"0xffffffff\"," +
"\"instruction_offset\":5," +
"\"addr_mode\":\"rel:0\"" +
Expand Down

0 comments on commit 7c48687

Please sign in to comment.