Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
6d40ac0
Profiling, first steps
grendello Oct 18, 2022
a7e9dbb
[WIP, PROF] MSBuild tasks to drive the profiling session
grendello Nov 3, 2022
8aca937
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 4, 2022
f155b08
[WIP] Add wrap.sh, start a python runner, support newer Android versions
grendello Nov 4, 2022
451c4d9
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 8, 2022
176b0cf
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 9, 2022
b125b3b
[WIP] Beginnings of debugging support
grendello Nov 9, 2022
0377015
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 10, 2022
7760be1
[WIP] Prepare device and host for debugging
grendello Nov 10, 2022
8e328c0
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 14, 2022
69185ac
[WIP] Library symbolication + some progress towards running lldb
grendello Nov 14, 2022
c7ed034
[WIP] Debug server runner progress
grendello Nov 14, 2022
99a07cd
[WIP] Well... it won't work this way
grendello Nov 15, 2022
e403f4f
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 16, 2022
a362b9e
[WIP] Working on new approach to running the debug server
grendello Nov 16, 2022
83fd5d8
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 17, 2022
6923ffa
[WIP] 4h spent looking for a missing /, doh
grendello Nov 17, 2022
38ba04f
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 18, 2022
02f72bf
[WIP] Different direction
grendello Nov 21, 2022
065faaf
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 22, 2022
583cc39
[WIP] Code migration to standalone app
grendello Nov 22, 2022
662142e
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 23, 2022
b5346a8
[WIP] Further progress
grendello Nov 23, 2022
a8ce675
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 24, 2022
0dbaca4
[WIP] Compatibility between Android version is a mess...
grendello Nov 24, 2022
1e8284b
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 25, 2022
29ee96f
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 25, 2022
a06e281
[WIP] ld config and directory listing parsing for devices without ldd
grendello Nov 25, 2022
b095fdf
[WIP] Fetching libraries from devices without ldd
grendello Nov 28, 2022
ae9fe5d
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 30, 2022
a4b923a
Merge branch 'main' into native-profiling-and-debugging
grendello Nov 30, 2022
2361e12
[WIP] Prep app mostly done, onto the script phase
grendello Nov 30, 2022
20c7f41
Merge branch 'main' into native-profiling-and-debugging
grendello Dec 1, 2022
ea106f2
Merge branch 'main' into native-profiling-and-debugging
grendello Dec 1, 2022
033954a
[WIP] Install the console app in tools; script progress
grendello Dec 1, 2022
5a013dc
[WIP] Change of direction
grendello Dec 2, 2022
9f3cf01
Merge branch 'main' into native-profiling-and-debugging
grendello Dec 5, 2022
88c4e40
[WIP] AXML parser
grendello Dec 5, 2022
ba0aaaa
Merge branch 'main' into native-profiling-and-debugging
grendello Dec 6, 2022
c7a0478
[WIP] AXML parsing continued
grendello Dec 6, 2022
db93826
Merge branch 'main' into native-profiling-and-debugging
grendello Dec 7, 2022
dfcddc3
[WIP] AXML parser works
grendello Dec 7, 2022
82d90f4
[WIP] App building and APK discovery
grendello Dec 8, 2022
a62ef71
[WIP] Debug session, beginnings
grendello Dec 9, 2022
4ea0641
Merge branch 'main' into native-profiling-and-debugging
grendello Jan 3, 2023
8b6b0b3
[WIP] Update MSBuild.StructuredLogger version
grendello Jan 3, 2023
fc3bb07
Merge branch 'main' into native-profiling-and-debugging
grendello Jan 4, 2023
230c132
[WIP] Getting back in the saddle
grendello Jan 4, 2023
9de48e2
Merge branch 'main' into native-profiling-and-debugging
grendello Jan 17, 2023
e146d06
A handful of changes, lots of testing on various versions of Android
grendello Jan 17, 2023
4ebbeb4
Merge branch 'main' into native-profiling-and-debugging
grendello Jan 18, 2023
371d3bb
Support for background processes + TPL, attempt #3
grendello Jan 18, 2023
61ce724
New tool runner, based on the new process runner
grendello Jan 19, 2023
b9fa158
Merge branch 'main' into native-profiling-and-debugging
grendello Jan 20, 2023
3ae0d9c
Merge branch 'main' into native-profiling-and-debugging
grendello Jan 23, 2023
3fca451
Merge branch 'main' into native-profiling-and-debugging
grendello Jan 30, 2023
82a52d2
Merge branch 'main' into native-profiling-and-debugging
grendello Apr 6, 2023
e97c680
Some little stuff
grendello Apr 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Xamarin.Android.sln
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Java.Runtime.Environment",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "create-android-api", "build-tools\create-android-api\create-android-api.csproj", "{BA4D889D-066B-4C2C-A973-09E319CBC396}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "debug-session-prep", "tools\debug-session-prep\debug-session-prep.csproj", "{087C42C4-6B45-4020-AB39-52515265082E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "xadebug", "tools\xadebug\xadebug.csproj", "{F4D105FA-D394-437A-B3BA-0EC56C6734C3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xamarin.Android.Tools.Aidl-Tests", "tests\Xamarin.Android.Tools.Aidl-Tests\Xamarin.Android.Tools.Aidl-Tests.csproj", "{A39B6D7C-6616-40D6-8AE4-C6CEE93D2708}"
EndProject
Global
Expand Down Expand Up @@ -300,12 +304,10 @@ Global
{B8105878-D423-4159-A3E7-028298281EC6}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{B8105878-D423-4159-A3E7-028298281EC6}.Release|AnyCPU.ActiveCfg = Release|Any CPU
{B8105878-D423-4159-A3E7-028298281EC6}.Release|AnyCPU.Build.0 = Release|Any CPU

{43564FB3-0F79-4FF4-A2B0-B1637072FF01}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU
{43564FB3-0F79-4FF4-A2B0-B1637072FF01}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{43564FB3-0F79-4FF4-A2B0-B1637072FF01}.Release|AnyCPU.ActiveCfg = Release|Any CPU
{43564FB3-0F79-4FF4-A2B0-B1637072FF01}.Release|AnyCPU.Build.0 = Release|Any CPU

{3DE17662-DCD6-4F49-AF06-D39AACC8649A}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU
{3DE17662-DCD6-4F49-AF06-D39AACC8649A}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{3DE17662-DCD6-4F49-AF06-D39AACC8649A}.Release|AnyCPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -418,6 +420,10 @@ Global
{D8E14B43-E929-4C18-9FA6-2C3DC47EFC17}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{D8E14B43-E929-4C18-9FA6-2C3DC47EFC17}.Release|AnyCPU.ActiveCfg = Release|Any CPU
{D8E14B43-E929-4C18-9FA6-2C3DC47EFC17}.Release|AnyCPU.Build.0 = Release|Any CPU
{087C42C4-6B45-4020-AB39-52515265082E}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU
{087C42C4-6B45-4020-AB39-52515265082E}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{087C42C4-6B45-4020-AB39-52515265082E}.Release|AnyCPU.ActiveCfg = Release|Any CPU
{087C42C4-6B45-4020-AB39-52515265082E}.Release|AnyCPU.Build.0 = Release|Any CPU
{C0E44558-FEE3-4DD3-986A-3F46DD1BF41B}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU
{C0E44558-FEE3-4DD3-986A-3F46DD1BF41B}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{C0E44558-FEE3-4DD3-986A-3F46DD1BF41B}.Release|AnyCPU.ActiveCfg = Release|Any CPU
Expand All @@ -426,6 +432,10 @@ Global
{BA4D889D-066B-4C2C-A973-09E319CBC396}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{BA4D889D-066B-4C2C-A973-09E319CBC396}.Release|AnyCPU.ActiveCfg = Release|Any CPU
{BA4D889D-066B-4C2C-A973-09E319CBC396}.Release|AnyCPU.Build.0 = Release|Any CPU
{F4D105FA-D394-437A-B3BA-0EC56C6734C3}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU
{F4D105FA-D394-437A-B3BA-0EC56C6734C3}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{F4D105FA-D394-437A-B3BA-0EC56C6734C3}.Release|AnyCPU.ActiveCfg = Release|Any CPU
{F4D105FA-D394-437A-B3BA-0EC56C6734C3}.Release|AnyCPU.Build.0 = Release|Any CPU
{A39B6D7C-6616-40D6-8AE4-C6CEE93D2708}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU
{A39B6D7C-6616-40D6-8AE4-C6CEE93D2708}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{A39B6D7C-6616-40D6-8AE4-C6CEE93D2708}.Release|AnyCPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -499,8 +509,10 @@ Global
{DA50FC92-7FE7-48B5-BDB6-CDA57B37BB51} = {864062D3-A415-4A6F-9324-5820237BA058}
{4EFCED6E-9A6B-453A-94E4-CE4B736EC684} = {864062D3-A415-4A6F-9324-5820237BA058}
{D8E14B43-E929-4C18-9FA6-2C3DC47EFC17} = {864062D3-A415-4A6F-9324-5820237BA058}
{087C42C4-6B45-4020-AB39-52515265082E} = {864062D3-A415-4A6F-9324-5820237BA058}
{C0E44558-FEE3-4DD3-986A-3F46DD1BF41B} = {04E3E11E-B47D-4599-8AFC-50515A95E715}
{BA4D889D-066B-4C2C-A973-09E319CBC396} = {E351F97D-EA4F-4E7F-AAA0-8EBB1F2A4A62}
{F4D105FA-D394-437A-B3BA-0EC56C6734C3} = {864062D3-A415-4A6F-9324-5820237BA058}
{A39B6D7C-6616-40D6-8AE4-C6CEE93D2708} = {CAB438D8-B0F5-4AF0-BEBD-9E2ADBD7B483}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
4 changes: 4 additions & 0 deletions build-tools/create-packs/Microsoft.Android.Sdk.proj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ core workload SDK packs imported by WorkloadManifest.targets.
<_PackageFiles Include="$(MicrosoftAndroidSdkOutDir)class-parse.dll" PackagePath="tools" />
<_PackageFiles Include="$(MicrosoftAndroidSdkOutDir)class-parse.pdb" PackagePath="tools" />
<_PackageFiles Include="$(MicrosoftAndroidSdkOutDir)class-parse.runtimeconfig.json" PackagePath="tools" />
<_PackageFiles Include="$(MicrosoftAndroidSdkOutDir)debug-session-prep.deps.json" PackagePath="tools" />
<_PackageFiles Include="$(MicrosoftAndroidSdkOutDir)debug-session-prep.dll" PackagePath="tools" />
<_PackageFiles Include="$(MicrosoftAndroidSdkOutDir)debug-session-prep.pdb" PackagePath="tools" />
<_PackageFiles Include="$(MicrosoftAndroidSdkOutDir)debug-session-prep.runtimeconfig.json" PackagePath="tools" />
<_PackageFiles Include="$(MicrosoftAndroidSdkOutDir)generator.dll" PackagePath="tools" />
<_PackageFiles Include="$(MicrosoftAndroidSdkOutDir)generator.pdb" PackagePath="tools" />
<_PackageFiles Include="$(MicrosoftAndroidSdkOutDir)generator.runtimeconfig.json" PackagePath="tools" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="Xamarin.Android.Tasks.DebugNativeCode" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.GetAndroidActivityName" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.ProfileNativeCode" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.SetupNativeCodeProfiling" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />

<PropertyGroup>
<_AndroidEnableNativeCodeProfiling>False</_AndroidEnableNativeCodeProfiling>
<_AndroidEnableNativeDebugging>False</_AndroidEnableNativeDebugging>

<ProfileNativeCodeDependsOnTargets>
_ResolveMonoAndroidSdks;
_SetupNativeCodeProfiling;
Build;
Install;
</ProfileNativeCodeDependsOnTargets>

<DebugNativeCodeDependsOnTargets>
_ResolveMonoAndroidSdks;
_SetupNativeCodeDebugging;
Build;
Install;
</DebugNativeCodeDependsOnTargets>
</PropertyGroup>

<Target Name="_SetupNativeCodeProfiling">
<PropertyGroup>
<_AndroidEnableNativeCodeProfiling>True</_AndroidEnableNativeCodeProfiling>
<_AndroidAotStripLibraries>False</_AndroidAotStripLibraries>
<_AndroidStripNativeLibraries>False</_AndroidStripNativeLibraries>

<!-- TODO: for some reason this makes build produce an unsigned apk. Figure out why -->
<AndroidPackageFormat>apk</AndroidPackageFormat> <!-- using aab breaks installation of archives with wrap.sh -->
</PropertyGroup>

<SetupNativeCodeProfiling
TargetDeviceName="$(AdbTarget)"
AdbPath="$(AdbToolPath)adb"
AndroidNdkPath="$(_AndroidNdkDirectory)">
<Output TaskParameter="DeviceIsEmulator" PropertyName="_AndroidDeviceIsEmulator" />
<Output TaskParameter="DevicePrimaryABI" PropertyName="_AndroidDevicePrimaryABI" />
<Output TaskParameter="DeviceSdkVersion" PropertyName="_AndroidDeviceSdkVersion" />
<Output TaskParameter="DeviceSupportedAbis" PropertyName="_AndroidDeviceSupportedAbis" />
<Output TaskParameter="NdkPythonDirectory" PropertyName="_AndroidNdkPythonDirectory" />
<Output TaskParameter="SimplePerfDirectory" PropertyName="_AndroidSimplePerfDirectory" />
</SetupNativeCodeProfiling>
</Target>

<Target Name="ProfileNativeCode"
DependsOnTargets="$(ProfileNativeCodeDependsOnTargets)">
<ProfileNativeCode
DeviceSdkVersion="$(_AndroidDeviceSdkVersion)"
DeviceIsEmulator="$(_AndroidDeviceIsEmulator)"
DeviceSupportedAbis="$(_AndroidDeviceSupportedAbis)"
DevicePrimaryABI="$(_AndroidDevicePrimaryABI)"
SimplePerfDirectory="$(_AndroidSimplePerfDirectory)"
NdkPythonDirectory="$(_AndroidNdkPythonDirectory)" />
</Target>

<Target Name="_SetupNativeCodeDebugging">
<PropertyGroup>
<_AndroidEnableNativeDebugging>True</_AndroidEnableNativeDebugging>
<_AndroidAotStripLibraries>False</_AndroidAotStripLibraries>
<_AndroidStripNativeLibraries>False</_AndroidStripNativeLibraries>
</PropertyGroup>
</Target>

<Target Name="DebugNativeCode"
DependsOnTargets="$(DebugNativeCodeDependsOnTargets)">
<GetAndroidActivityName
Condition=" '$(AndroidLaunchActivity)' == '' "
ManifestFile="$(IntermediateOutputPath)android\AndroidManifest.xml">
<Output TaskParameter="ActivityName" PropertyName="AndroidLaunchActivity" />
</GetAndroidActivityName>

<DebugNativeCode
TargetDeviceName="$(AdbTarget)"
AdbPath="$(AdbToolPath)adb"
AndroidNdkPath="$(_AndroidNdkDirectory)"
PackageName="$(_AndroidPackage)"
SupportedAbis="@(_BuildTargetAbis)"
ActivityName="$(AndroidStartActivityName)"
MainActivityName="$(AndroidLaunchActivity)"
IntermediateOutputDir="$(IntermediateOutputPath)"
NativeLibraries="@(FrameworkNativeLibrary);@(_ApplicationSharedLibrary)"/>
</Target>
</Project>
9 changes: 9 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Resources/debug-app.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# How to read vars from a file
#
# $vars = Get-Content -Raw ./settings.txt | ConvertFrom-StringData
#
# Access to them:
#
# $vars.one
# write-host "$($vars.one)"
154 changes: 154 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Resources/debug-app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/bin/bash
MY_DIR="$(cd $(dirname $0);pwd)"

ADB_PATH="@ADB_PATH@"
APP_LIBS_DIR="@APP_LIBS_DIR@"
CONFIG_SCRIPT_NAME="@CONFIG_SCRIPT_NAME@"
DEBUG_SESSION_PREP_PATH="@DEBUG_SESSION_PREP_PATH@"
DEFAULT_ACTIVITY_NAME="@ACTIVITY_NAME@"
LLDB_SCRIPT_NAME="@LLDB_SCRIPT_NAME@"
NDK_DIR="@NDK_DIR@"
OUTPUT_DIR="@OUTPUT_DIR@"
PACKAGE_NAME="@PACKAGE_NAME@"
SUPPORTED_ABIS_ARRAY=(@SUPPORTED_ABIS@)
ADB_DEVICE=""

function die()
{
echo "$@"
exit 1
}

function die_if_failed()
{
if [ $? -ne 0 ]; then
die "$@"
fi
}

function run_adb_no_echo()
{
local args=""

if [ -n "${ADB_DEVICE}" ]; then
args="-s ${ADB_DEVICE}"
fi

adb ${args} "$@"
}

function run_adb()
{
echo Running: adb ${args} "$@"
run_adb_no_echo "$@"
}

function run_adb_echo_info()
{
local log_file="$1"
shift

echo Running: adb ${args} "$@"
echo Logging to: "${log_file}"
echo
}

function run_adb_with_log()
{
local log_file="$1"
shift

run_adb_echo_info "${log_file}" "$@"
run_adb_no_echo "$@" > "${log_file}" 2>&1
}

function run_adb_with_log_bg()
{
local log_file="$1"
shift

run_adb_echo_info "${log_file}" "$@"
run_adb_no_echo "$@" > "${log_file}" 2>&1 &
}

#TODO: APP_LIBS_DIR needs to be appended the abi-specific subdir
#TODO: make NDK_DIR overridable via a parameter
#TOOD: add a parameter to specify the Android device to target
#TODO: detect whether we have dotnet in $PATH and whether it's a compatible version
#TODO: add an option to make XA wait for debugger to connect
#TODO: add a parameter to specify activity to start

ACTIVITY_NAME="${DEFAULT_ACTIVITY_NAME}"

SUPPORTED_ABIS_ARG=""
for sa in "${SUPPORTED_ABIS_ARRAY[@]}"; do
if [ -z "${SUPPORTED_ABIS_ARG}" ]; then
SUPPORTED_ABIS_ARG="${sa}"
else
SUPPORTED_ABIS_ARG="${SUPPORTED_ABIS_ARG},${sa}"
fi
done

dotnet "${DEBUG_SESSION_PREP_PATH}" \
-s "${SUPPORTED_ABIS_ARG}" \
-p "${PACKAGE_NAME}" \
-n "${NDK_DIR}" \
-o "${OUTPUT_DIR}" \
-l "${APP_LIBS_DIR}" \
-c "${CONFIG_SCRIPT_NAME}" \
-g "${LLDB_SCRIPT_NAME}"

die_if_failed Debug preparation app failed

CONFIG_SCRIPT_PATH="${OUTPUT_DIR}/${CONFIG_SCRIPT_NAME}"
if [ ! -f "${CONFIG_SCRIPT_PATH}" ]; then
die Config script ${CONFIG_SCRIPT_PATH} not found
fi

source "${CONFIG_SCRIPT_PATH}"

# Determine cross section of supported and device ABIs
ALLOWED_ABIS=()

for dabi in "${DEVICE_AVAILABLE_ABIS[@]}"; do
for sabi in "${SUPPORTED_ABIS_ARRAY[@]}"; do
if [ "${dabi}" == "${sabi}" ]; then
ALLOWED_ABIS+="${dabi}"
fi
done
done

if [ ${#ALLOWED_ABIS[@]} -le 0 ]; then
die Application does not support any ABIs available on device
fi

ADB_DEBUG_SERVER_LOG="${OUTPUT_DIR}/lldb-debug-server.log"
echo Starting debug server on device
echo stdout and stderr will be redirected to: ${ADB_DEBUG_SERVER_LOG}

LLDB_SERVER_PLATFORM_LOG="${DEVICE_LLDB_DIR}/log/platform.log"
LLDB_SERVER_STDOUT_LOG="${DEVICE_LLDB_DIR}/log/platform-stdout.log"
LLDB_SERVER_GDB_LOG="${DEVICE_LLDB_DIR}/log/gdb-server.log"

run_adb shell run-as ${PACKAGE_NAME} kill -9 "\"\`pidof ${DEVICE_DEBUG_SERVER_LAUNCHER}\`\""
run_adb_with_log_bg "${ADB_DEBUG_SERVER_LOG}" shell run-as ${PACKAGE_NAME} ${DEVICE_DEBUG_SERVER_LAUNCHER} ${DEVICE_LLDB_DIR} ${SOCKET_SCHEME} ${SOCKET_DIR} ${SOCKET_NAME} "\"lldb process:gdb-remote packets\""

LAUNCH_SPEC=${PACKAGE_NAME}/${ACTIVITY_NAME}
run_adb shell am start -S -W ${LAUNCH_SPEC}
die_if_failed Failed to start ${LAUNCH_SPEC}

APP_PID=$(run_adb_no_echo shell pidof ${PACKAGE_NAME})
die_if_failed Failed to get ${PACKAGE_NAME} PID on device

LLDB_SCRIPT_PATH="${OUTPUT_DIR}/${LLDB_SCRIPT_NAME}"
echo App PID: ${APP_PID}
echo "attach ${APP_PID}" >> "${LLDB_SCRIPT_PATH}"

#TODO: start the app if not running
#TODO: pass app pid to lldb, with -p or --attach-pid
export TERMINFO=/usr/share/terminfo
"${LLDB_PATH}" --source "${LLDB_SCRIPT_PATH}"

run_adb_with_log "${OUTPUT_DIR}/lldb-platform.log" shell run-as ${PACKAGE_NAME} cat ${LLDB_SERVER_PLATFORM_LOG}
run_adb_with_log "${OUTPUT_DIR}/lldb-platform-stdout.log" shell run-as ${PACKAGE_NAME} cat ${LLDB_SERVER_STDOUT_LOG}
run_adb_with_log "${OUTPUT_DIR}/lldb-gdb-server.log" shell run-as ${PACKAGE_NAME} cat ${LLDB_SERVER_GDB_LOG}
31 changes: 29 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public class BuildApk : AndroidTask

public bool UseAssemblyStore { get; set; }

public bool NativeCodeProfilingEnabled { get; set; }

public string DeviceSdkVersion { get; set; }

[Required]
public string ProjectFullPath { get; set; }

Expand Down Expand Up @@ -192,6 +196,7 @@ void ExecuteWithAbi (string [] supportedAbis, string apkInputPath, string apkOut
}

AddRuntimeLibraries (apk, supportedAbis);
AddProfilingScripts (apk, supportedAbis);
apk.Flush();
AddNativeLibraries (files, supportedAbis);
AddAdditionalNativeLibraries (files, supportedAbis);
Expand Down Expand Up @@ -621,9 +626,11 @@ CompressionMethod GetCompressionMethod (string fileName)
return CompressionMethod.Default;
}

string GetArchiveAbiLibPath (string abi, string fileName) => $"lib/{abi}/{fileName}";

void AddNativeLibraryToArchive (ZipArchiveEx apk, string abi, string filesystemPath, string inArchiveFileName)
{
string archivePath = $"lib/{abi}/{inArchiveFileName}";
string archivePath = GetArchiveAbiLibPath (abi, inArchiveFileName);
existingEntries.Remove (archivePath);
CompressionMethod compressionMethod = GetCompressionMethod (archivePath);
if (apk.SkipExistingFile (filesystemPath, archivePath, compressionMethod)) {
Expand All @@ -634,6 +641,26 @@ void AddNativeLibraryToArchive (ZipArchiveEx apk, string abi, string filesystemP
apk.Archive.AddEntry (archivePath, File.OpenRead (filesystemPath), compressionMethod);
}

void AddProfilingScripts (ZipArchiveEx apk, string [] supportedAbis)
{
if (!NativeCodeProfilingEnabled || !Int32.TryParse (DeviceSdkVersion, out int sdkVersion) || sdkVersion < 26) {
// wrap.sh is available on Android O (API 26) or newer
return;
}

string wrapScript = "#!/system/bin/sh\n$@\n";
byte[] wrapScriptBytes = new UTF8Encoding (false).GetBytes (wrapScript);
EntryPermissions wrapScriptPermissions =
EntryPermissions.WorldRead | EntryPermissions.WorldExecute |
EntryPermissions.GroupRead | EntryPermissions.GroupExecute |
EntryPermissions.OwnerRead | EntryPermissions.OwnerWrite | EntryPermissions.OwnerExecute;

foreach (var abi in supportedAbis) {
string path = GetArchiveAbiLibPath (abi, "wrap.sh");
apk.Archive.AddEntry (wrapScriptBytes, path, wrapScriptPermissions, CompressionMethod.Default);
}
}

void AddRuntimeLibraries (ZipArchiveEx apk, string [] supportedAbis)
{
foreach (var abi in supportedAbis) {
Expand Down Expand Up @@ -807,7 +834,7 @@ private void AddAdditionalNativeLibraries (ArchiveFileList files, string [] supp
void AddNativeLibrary (ArchiveFileList files, string path, string abi, string archiveFileName)
{
string fileName = string.IsNullOrEmpty (archiveFileName) ? Path.GetFileName (path) : archiveFileName;
var item = (filePath: path, archivePath: $"lib/{abi}/{fileName}");
var item = (filePath: path, archivePath: GetArchiveAbiLibPath (abi, fileName));
if (files.Any (x => x.archivePath == item.archivePath)) {
Log.LogCodedWarning ("XA4301", path, 0, Properties.Resources.XA4301, item.archivePath);
return;
Expand Down
Loading