Skip to content

Commit

Permalink
[sdk] Add NativeRuntime.buildId to dart:developer.
Browse files Browse the repository at this point in the history
TEST=vm/dart/build_id

Issue: #51941
CoreLibraryReviewExempt: Native runtime only API
Change-Id: Ib3757480f0eab6d147385a87adf657f4f709ec4e
Cq-Include-Trybots: luci.dart.try:vm-aot-dwarf-linux-product-x64-try,vm-aot-linux-debug-simarm_x64-try,vm-aot-linux-debug-x64-try,vm-aot-linux-release-x64-try,vm-aot-mac-product-arm64-try,vm-aot-mac-release-arm64-try,vm-aot-mac-release-x64-try,vm-aot-linux-product-x64-try,vm-aot-win-release-x64-try,vm-aot-win-product-x64-try,vm-aot-win-debug-x64c-try,vm-aot-android-release-arm_x64-try,vm-fuchsia-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/307122
Reviewed-by: Slava Egorov <[email protected]>
Reviewed-by: Martin Kustermann <[email protected]>
Commit-Queue: Tess Strickland <[email protected]>
Reviewed-by: Lasse Nielsen <[email protected]>
  • Loading branch information
sstrickl authored and Commit Queue committed Jun 22, 2023
1 parent 1c3011c commit 97a09b9
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 0 deletions.
20 changes: 20 additions & 0 deletions runtime/lib/developer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "vm/object_store.h"
#include "vm/service.h"
#include "vm/service_isolate.h"
#include "vm/zone_text_buffer.h"

namespace dart {

Expand Down Expand Up @@ -179,6 +180,25 @@ DEFINE_NATIVE_ENTRY(Developer_reachability_barrier, 0, 0) {
#endif
}

DEFINE_NATIVE_ENTRY(Developer_NativeRuntime_buildId, 0, 0) {
#if defined(DART_PRECOMPILED_RUNTIME)
IsolateGroup* isolate_group = thread->isolate_group();
ASSERT(isolate_group != nullptr);
if (const uint8_t* instructions =
isolate_group->source()->snapshot_instructions) {
const auto& build_id = OS::GetAppBuildId(instructions);
if (build_id.data != nullptr) {
ZoneTextBuffer buffer(zone);
for (intptr_t i = 0; i < build_id.len; i++) {
buffer.Printf("%2.2x", build_id.data[i]);
}
return String::New(buffer.buffer());
}
}
#endif
return String::null();
}

DEFINE_NATIVE_ENTRY(Developer_NativeRuntime_writeHeapSnapshotToFile, 0, 1) {
#if defined(DART_ENABLE_HEAP_SNAPSHOT_WRITER)
const String& filename =
Expand Down
20 changes: 20 additions & 0 deletions runtime/tests/vm/dart/build_id_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:developer';

import 'package:expect/expect.dart';

import 'use_flag_test_helper.dart';

void main() {
final buildId = NativeRuntime.buildId;
if (isAOTRuntime) {
Expect.isNotNull(buildId);
Expect.isTrue(buildId!.isNotEmpty, 'Build ID is an empty string');
} else {
Expect.isNull(buildId); // Should be null in JIT mode.
}
print(buildId);
}
1 change: 1 addition & 0 deletions runtime/vm/bootstrap_natives.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace dart {
V(Developer_log, 8) \
V(Developer_postEvent, 2) \
V(Developer_webServerControl, 3) \
V(Developer_NativeRuntime_buildId, 0) \
V(Developer_NativeRuntime_writeHeapSnapshotToFile, 1) \
V(Developer_reachability_barrier, 0) \
V(Double_getIsNegative, 1) \
Expand Down
3 changes: 3 additions & 0 deletions sdk/lib/_internal/js_dev_runtime/patch/developer_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ UserTag getCurrentTag() => _currentTag;

@patch
abstract final class NativeRuntime {
@patch
static String? get buildId => null;

@patch
static void writeHeapSnapshotToFile(String filepath) =>
throw UnsupportedError(
Expand Down
3 changes: 3 additions & 0 deletions sdk/lib/_internal/js_runtime/lib/developer_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ UserTag getCurrentTag() => _currentTag;

@patch
abstract final class NativeRuntime {
@patch
static String? get buildId => null;

@patch
static void writeHeapSnapshotToFile(String filepath) =>
throw UnsupportedError(
Expand Down
4 changes: 4 additions & 0 deletions sdk/lib/_internal/vm/lib/developer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ external String _getIsolateIDFromSendPort(SendPort sendPort);

@patch
abstract final class NativeRuntime {
@patch
@pragma("vm:external-name", "Developer_NativeRuntime_buildId")
external static String? get buildId;

@patch
@pragma("vm:external-name", "Developer_NativeRuntime_writeHeapSnapshotToFile")
external static void writeHeapSnapshotToFile(String filepath);
Expand Down
3 changes: 3 additions & 0 deletions sdk/lib/_internal/wasm/lib/developer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ void _reportTaskEvent(

@patch
abstract final class NativeRuntime {
@patch
static String? get buildId => null;

@patch
static void writeHeapSnapshotToFile(String filepath) =>
throw UnsupportedError(
Expand Down
12 changes: 12 additions & 0 deletions sdk/lib/developer/developer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ external int get reachabilityBarrier;

/// Functionality available on the native runtime.
abstract final class NativeRuntime {
/// The build ID for the running application.
///
/// The build ID of an application is a string containing a hexadecimal
/// representation of an arbitrarily sized sequence of bytes. This string
/// can be used to match a specific ahead-of-time compiled version of an
/// application, for example, to determine which debugging artifacts emitted
/// during compilation should be used to translate crash and error reports.
///
/// The build ID is only available for ahead-of-time compiled programs. If a
/// build ID is not available, the value is `null`.
external static String? get buildId;

/// Writes a snapshot of the heap to [filepath].
///
/// The [filepath] should be a native file path that can be opened for writing.
Expand Down

0 comments on commit 97a09b9

Please sign in to comment.