2015-03-01 11:47:54 -07:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-09-16 07:23:58 -06:00
|
|
|
// This program can be used as go_ios_$GOARCH_exec by the Go tool.
|
2015-03-01 11:47:54 -07:00
|
|
|
// It executes binaries on an iOS device using the XCode toolchain
|
|
|
|
// and the ios-deploy program: https://github.com/phonegap/ios-deploy
|
2015-03-06 07:45:24 -07:00
|
|
|
//
|
|
|
|
// This script supports an extra flag, -lldb, that pauses execution
|
|
|
|
// just before the main program begins and allows the user to control
|
|
|
|
// the remote lldb session. This flag is appended to the end of the
|
|
|
|
// script's arguments and is not passed through to the underlying
|
|
|
|
// binary.
|
2015-04-13 12:31:41 -06:00
|
|
|
//
|
|
|
|
// This script requires that three environment variables be set:
|
2022-02-03 12:12:08 -07:00
|
|
|
//
|
|
|
|
// GOIOS_DEV_ID: The codesigning developer id or certificate identifier
|
|
|
|
// GOIOS_APP_ID: The provisioning app id prefix. Must support wildcard app ids.
|
|
|
|
// GOIOS_TEAM_ID: The team id that owns the app id prefix.
|
|
|
|
//
|
2015-04-13 12:31:41 -06:00
|
|
|
// $GOROOT/misc/ios contains a script, detect.go, that attempts to autodetect these.
|
2015-03-01 11:47:54 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-15 10:39:14 -06:00
|
|
|
"encoding/xml"
|
2015-03-01 11:47:54 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"go/build"
|
2015-03-06 07:45:24 -07:00
|
|
|
"io"
|
2015-03-01 11:47:54 -07:00
|
|
|
"log"
|
2018-04-15 10:39:14 -06:00
|
|
|
"net"
|
2015-03-01 11:47:54 -07:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
misc/ios: forward SIGQUIT to the iOS program
When running tests that fails to complete within the test timeout,
the go tool sends the test program a SIGQUIT signal to print
backtraces. However, for tests running with an exec wrapper, the
resulting backtraces will come from the exec wrapper process and
not the test program.
Change the iOS exec wrapper to forward SIGQUIT signals to the lldb
python driver and change the driver to forward the signals to the
running test on the device.
Before:
$ GOARCH=arm64 go test forever_test.go
lldb: running program
SIGQUIT: quit
PC=0x10816fe m=0 sigcode=0
goroutine 54 [syscall]:
syscall.Syscall6(0x7, 0x16ab, 0xc000033dfc, 0x0, 0xc000116f30, 0x0, 0x0, 0xc000116f30, 0x0, 0x1328820)
/Users/elias/go-tip/src/syscall/asm_darwin_amd64.s:41 +0x5 fp=0xc000033d48 sp=0xc000033d40 pc=0x10816d5
syscall.wait4(0x16ab, 0xc000033dfc, 0x0, 0xc000116f30, 0x90, 0x1200e00, 0x1)
/Users/elias/go-tip/src/syscall/zsyscall_darwin_amd64.go:34 +0x7b fp=0xc000033dc0 sp=0xc000033d48 pc=0x107e4eb
syscall.Wait4(0x16ab, 0xc000033e4c, 0x0, 0xc000116f30, 0xc0000fd518, 0x0, 0x0)
/Users/elias/go-tip/src/syscall/syscall_bsd.go:129 +0x51 fp=0xc000033e10 sp=0xc000033dc0 pc=0x107b7b1
os.(*Process).wait(0xc00008d440, 0x1095e2e, 0xc0000fd518, 0x0)
/Users/elias/go-tip/src/os/exec_unix.go:38 +0x7b fp=0xc000033e80 sp=0xc000033e10 pc=0x109af2b
os.(*Process).Wait(0xc00008d440, 0xc000033fb0, 0x10, 0x11d1f00)
/Users/elias/go-tip/src/os/exec.go:125 +0x2b fp=0xc000033eb0 sp=0xc000033e80 pc=0x109a47b
os/exec.(*Cmd).Wait(0xc0000b1ce0, 0xc000033f90, 0x11394df)
/Users/elias/go-tip/src/os/exec/exec.go:463 +0x5b fp=0xc000033f28 sp=0xc000033eb0 pc=0x1136f0b
main.startDebugBridge.func1(0xc0000b1ce0, 0xc0000b8ae0, 0xc0000e2a80)
/Users/elias/go-tip/misc/ios/go_darwin_arm_exec.go:314 +0x40 fp=0xc000033fc8 sp=0xc000033f28 pc=0x11a1980
runtime.goexit()
/Users/elias/go-tip/src/runtime/asm_amd64.s:1360 +0x1 fp=0xc000033fd0 sp=0xc000033fc8 pc=0x10565a1
created by main.startDebugBridge
/Users/elias/go-tip/misc/ios/go_darwin_arm_exec.go:313 +0x15f
...
After:
$ GOARCH=arm64 go test forever_test.go
lldb: running program
=== RUN TestForever
SIGQUIT: quit
PC=0x100144e24 m=0 sigcode=0
...
goroutine 19 [select (no cases)]:
command-line-arguments.TestForever(0x1300b60f0)
/Users/elias/go-tip/src/forever_test.go:6 +0x18
testing.tRunner(0x1300b60f0, 0x100211aa0)
/Users/elias/go-tip/src/testing/testing.go:795 +0xa8
created by testing.(*T).Run
/Users/elias/go-tip/src/testing/testing.go:840 +0x22c
...
Change-Id: I6b3cf1662d07a43ade0530842733b0944bee1ace
Reviewed-on: https://go-review.googlesource.com/112676
Run-TryBot: Elias Naur <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-05-11 09:46:00 -06:00
|
|
|
"os/signal"
|
2015-03-01 11:47:54 -07:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2020-09-16 07:23:58 -06:00
|
|
|
"strconv"
|
2015-03-01 11:47:54 -07:00
|
|
|
"strings"
|
2016-03-24 09:03:07 -06:00
|
|
|
"syscall"
|
2015-03-01 11:47:54 -07:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const debug = false
|
|
|
|
|
2015-03-30 06:36:37 -06:00
|
|
|
var tmpdir string
|
|
|
|
|
2015-04-13 12:31:41 -06:00
|
|
|
var (
|
2017-02-01 01:37:47 -07:00
|
|
|
devID string
|
|
|
|
appID string
|
|
|
|
teamID string
|
|
|
|
bundleID string
|
2017-08-20 10:57:18 -06:00
|
|
|
deviceID string
|
2015-04-13 12:31:41 -06:00
|
|
|
)
|
|
|
|
|
2016-03-25 08:40:44 -06:00
|
|
|
// lock is a file lock to serialize iOS runs. It is global to avoid the
|
|
|
|
// garbage collector finalizing it, closing the file and releasing the
|
|
|
|
// lock prematurely.
|
|
|
|
var lock *os.File
|
|
|
|
|
2015-03-01 11:47:54 -07:00
|
|
|
func main() {
|
|
|
|
log.SetFlags(0)
|
2020-10-05 09:51:54 -06:00
|
|
|
log.SetPrefix("go_ios_exec: ")
|
2015-03-01 11:47:54 -07:00
|
|
|
if debug {
|
|
|
|
log.Println(strings.Join(os.Args, " "))
|
|
|
|
}
|
|
|
|
if len(os.Args) < 2 {
|
2020-10-05 09:51:54 -06:00
|
|
|
log.Fatal("usage: go_ios_exec a.out")
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
|
|
|
|
2017-02-01 01:37:47 -07:00
|
|
|
// For compatibility with the old builders, use a fallback bundle ID
|
|
|
|
bundleID = "golang.gotest"
|
|
|
|
|
2018-05-02 11:48:04 -06:00
|
|
|
exitCode, err := runMain()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%v\n", err)
|
|
|
|
}
|
|
|
|
os.Exit(exitCode)
|
2018-04-15 10:39:14 -06:00
|
|
|
}
|
|
|
|
|
2018-05-02 11:48:04 -06:00
|
|
|
func runMain() (int, error) {
|
2015-03-30 06:36:37 -06:00
|
|
|
var err error
|
2021-04-03 02:10:47 -06:00
|
|
|
tmpdir, err = os.MkdirTemp("", "go_ios_exec_")
|
2015-03-30 06:36:37 -06:00
|
|
|
if err != nil {
|
2018-05-02 11:48:04 -06:00
|
|
|
return 1, err
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
if !debug {
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
|
2018-04-12 12:33:47 -06:00
|
|
|
appdir := filepath.Join(tmpdir, "gotest.app")
|
|
|
|
os.RemoveAll(appdir)
|
|
|
|
|
|
|
|
if err := assembleApp(appdir, os.Args[1]); err != nil {
|
2018-05-02 11:48:04 -06:00
|
|
|
return 1, err
|
2018-04-12 12:33:47 -06:00
|
|
|
}
|
|
|
|
|
2016-03-24 09:03:07 -06:00
|
|
|
// This wrapper uses complicated machinery to run iOS binaries. It
|
|
|
|
// works, but only when running one binary at a time.
|
|
|
|
// Use a file lock to make sure only one wrapper is running at a time.
|
|
|
|
//
|
|
|
|
// The lock file is never deleted, to avoid concurrent locks on distinct
|
|
|
|
// files with the same path.
|
2020-10-05 09:51:54 -06:00
|
|
|
lockName := filepath.Join(os.TempDir(), "go_ios_exec-"+deviceID+".lock")
|
2016-03-25 08:40:44 -06:00
|
|
|
lock, err = os.OpenFile(lockName, os.O_CREATE|os.O_RDONLY, 0666)
|
2016-03-24 09:03:07 -06:00
|
|
|
if err != nil {
|
2018-05-02 11:48:04 -06:00
|
|
|
return 1, err
|
2016-03-24 09:03:07 -06:00
|
|
|
}
|
|
|
|
if err := syscall.Flock(int(lock.Fd()), syscall.LOCK_EX); err != nil {
|
2018-05-02 11:48:04 -06:00
|
|
|
return 1, err
|
2016-03-24 09:03:07 -06:00
|
|
|
}
|
2018-04-12 12:33:47 -06:00
|
|
|
|
2020-09-16 07:23:58 -06:00
|
|
|
if goarch := os.Getenv("GOARCH"); goarch == "arm64" {
|
|
|
|
err = runOnDevice(appdir)
|
|
|
|
} else {
|
|
|
|
err = runOnSimulator(appdir)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// If the lldb driver completed with an exit code, use that.
|
|
|
|
if err, ok := err.(*exec.ExitError); ok {
|
|
|
|
if ws, ok := err.Sys().(interface{ ExitStatus() int }); ok {
|
|
|
|
return ws.ExitStatus(), nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-07 05:05:27 -06:00
|
|
|
return 1, err
|
|
|
|
}
|
2020-09-16 07:23:58 -06:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runOnSimulator(appdir string) error {
|
|
|
|
if err := installSimulator(appdir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-07 05:05:27 -06:00
|
|
|
|
2020-09-16 07:23:58 -06:00
|
|
|
return runSimulator(appdir, bundleID, os.Args[2:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func runOnDevice(appdir string) error {
|
|
|
|
// e.g. B393DDEB490947F5A463FD074299B6C0AXXXXXXX
|
|
|
|
devID = getenv("GOIOS_DEV_ID")
|
|
|
|
|
|
|
|
// e.g. Z8B3JBXXXX.org.golang.sample, Z8B3JBXXXX prefix is available at
|
|
|
|
// https://developer.apple.com/membercenter/index.action#accountSummary as Team ID.
|
|
|
|
appID = getenv("GOIOS_APP_ID")
|
|
|
|
|
|
|
|
// e.g. Z8B3JBXXXX, available at
|
|
|
|
// https://developer.apple.com/membercenter/index.action#accountSummary as Team ID.
|
|
|
|
teamID = getenv("GOIOS_TEAM_ID")
|
|
|
|
|
|
|
|
// Device IDs as listed with ios-deploy -c.
|
|
|
|
deviceID = os.Getenv("GOIOS_DEVICE_ID")
|
|
|
|
|
2021-09-22 08:46:32 -06:00
|
|
|
if _, id, ok := strings.Cut(appID, "."); ok {
|
|
|
|
bundleID = id
|
2020-09-16 07:23:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := signApp(appdir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := uninstallDevice(bundleID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := installDevice(appdir); err != nil {
|
|
|
|
return err
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
|
|
|
|
if err := mountDevImage(); err != nil {
|
2020-09-16 07:23:58 -06:00
|
|
|
return err
|
2018-04-15 10:39:14 -06:00
|
|
|
}
|
|
|
|
|
2018-05-08 14:59:35 -06:00
|
|
|
// Kill any hanging debug bridges that might take up port 3222.
|
|
|
|
exec.Command("killall", "idevicedebugserverproxy").Run()
|
|
|
|
|
2018-04-15 10:39:14 -06:00
|
|
|
closer, err := startDebugBridge()
|
2015-03-30 06:36:37 -06:00
|
|
|
if err != nil {
|
2020-09-16 07:23:58 -06:00
|
|
|
return err
|
2018-04-15 10:39:14 -06:00
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
2020-09-16 07:23:58 -06:00
|
|
|
return runDevice(appdir, bundleID, os.Args[2:])
|
2015-03-30 06:36:37 -06:00
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
|
2015-04-13 12:31:41 -06:00
|
|
|
func getenv(envvar string) string {
|
|
|
|
s := os.Getenv(envvar)
|
|
|
|
if s == "" {
|
2015-11-02 15:37:31 -07:00
|
|
|
log.Fatalf("%s not set\nrun $GOROOT/misc/ios/detect.go to attempt to autodetect", envvar)
|
2015-04-13 12:31:41 -06:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-04-12 12:33:47 -06:00
|
|
|
func assembleApp(appdir, bin string) error {
|
2015-03-01 11:47:54 -07:00
|
|
|
if err := os.MkdirAll(appdir, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cp(filepath.Join(appdir, "gotest"), bin); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
misc/ios,cmd/go, runtime/cgo: fix iOS test harness (again)
The iOS test harness was recently changed in response to lldb bugs
to replace breakpoints with the SIGUSR2 signal (CL 34926), and to
pass the current directory in the test binary arguments (CL 35152).
Both the signal sending and working directory setup is done from
the go test driver.
However, the new method doesn't work with tests where a C program is
the test driver instead of go test: the current working directory
will not be changed and SIGUSR2 is not raised.
Instead of copying that logic into any C test program, rework the
test harness (again) to move the setup logic to the early runtime
cgo setup code. That way, the harness will run even in the library
build modes.
Then, use the app Info.plist file to pass the working
directory, removing the need to alter the arguments after running.
Finally, use the SIGINT signal instead of SIGUSR2 to avoid
manipulating the signal masks or handlers.
Fixes the testcarchive tests on iOS.
With this CL, both darwin/arm and darwin/arm64 passes all.bash.
This CL replaces CL 34926, CL 35152 as well as the fixup CL
35123 and CL 35255. They are reverted in CLs earlier in the
relation chain.
Change-Id: I8485c7db1404fbd8daa261efd1ea89e905121a3e
Reviewed-on: https://go-review.googlesource.com/36090
Run-TryBot: Elias Naur <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-01 08:04:07 -07:00
|
|
|
pkgpath, err := copyLocalData(appdir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-01 11:47:54 -07:00
|
|
|
entitlementsPath := filepath.Join(tmpdir, "Entitlements.plist")
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile(entitlementsPath, []byte(entitlementsPlist()), 0744); err != nil {
|
2015-03-01 11:47:54 -07:00
|
|
|
return err
|
|
|
|
}
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile(filepath.Join(appdir, "Info.plist"), []byte(infoPlist(pkgpath)), 0744); err != nil {
|
2015-03-01 11:47:54 -07:00
|
|
|
return err
|
|
|
|
}
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile(filepath.Join(appdir, "ResourceRules.plist"), []byte(resourceRules), 0744); err != nil {
|
2015-03-01 11:47:54 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-09-16 07:23:58 -06:00
|
|
|
return nil
|
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
|
2020-09-16 07:23:58 -06:00
|
|
|
func signApp(appdir string) error {
|
|
|
|
entitlementsPath := filepath.Join(tmpdir, "Entitlements.plist")
|
2015-03-01 11:47:54 -07:00
|
|
|
cmd := exec.Command(
|
|
|
|
"codesign",
|
|
|
|
"-f",
|
2015-04-13 12:31:41 -06:00
|
|
|
"-s", devID,
|
2015-03-01 11:47:54 -07:00
|
|
|
"--entitlements", entitlementsPath,
|
|
|
|
appdir,
|
|
|
|
)
|
|
|
|
if debug {
|
|
|
|
log.Println(strings.Join(cmd.Args, " "))
|
|
|
|
}
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("codesign: %v", err)
|
|
|
|
}
|
2018-04-12 12:33:47 -06:00
|
|
|
return nil
|
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
|
2018-04-15 10:39:14 -06:00
|
|
|
// mountDevImage ensures a developer image is mounted on the device.
|
|
|
|
// The image contains the device lldb server for idevicedebugserverproxy
|
|
|
|
// to connect to.
|
|
|
|
func mountDevImage() error {
|
|
|
|
// Check for existing mount.
|
2018-05-03 05:43:52 -06:00
|
|
|
cmd := idevCmd(exec.Command("ideviceimagemounter", "-l", "-x"))
|
2018-04-15 10:39:14 -06:00
|
|
|
out, err := cmd.CombinedOutput()
|
2015-03-30 06:36:37 -06:00
|
|
|
if err != nil {
|
2018-04-15 10:39:14 -06:00
|
|
|
os.Stderr.Write(out)
|
|
|
|
return fmt.Errorf("ideviceimagemounter: %v", err)
|
2015-03-30 06:36:37 -06:00
|
|
|
}
|
2018-05-03 05:43:52 -06:00
|
|
|
var info struct {
|
|
|
|
Dict struct {
|
|
|
|
Data []byte `xml:",innerxml"`
|
|
|
|
} `xml:"dict"`
|
|
|
|
}
|
|
|
|
if err := xml.Unmarshal(out, &info); err != nil {
|
|
|
|
return fmt.Errorf("mountDevImage: failed to decode mount information: %v", err)
|
|
|
|
}
|
|
|
|
dict, err := parsePlistDict(info.Dict.Data)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("mountDevImage: failed to parse mount information: %v", err)
|
|
|
|
}
|
|
|
|
if dict["ImagePresent"] == "true" && dict["Status"] == "Complete" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Some devices only give us an ImageSignature key.
|
|
|
|
if _, exists := dict["ImageSignature"]; exists {
|
2018-04-15 10:39:14 -06:00
|
|
|
return nil
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
// No image is mounted. Find a suitable image.
|
|
|
|
imgPath, err := findDevImage()
|
2015-11-04 09:21:55 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
sigPath := imgPath + ".signature"
|
|
|
|
cmd = idevCmd(exec.Command("ideviceimagemounter", imgPath, sigPath))
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
os.Stderr.Write(out)
|
|
|
|
return fmt.Errorf("ideviceimagemounter: %v", err)
|
2015-11-04 09:21:55 -07:00
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
return nil
|
2015-11-04 09:21:55 -07:00
|
|
|
}
|
|
|
|
|
2018-04-15 10:39:14 -06:00
|
|
|
// findDevImage use the device iOS version and build to locate a suitable
|
|
|
|
// developer image.
|
|
|
|
func findDevImage() (string, error) {
|
|
|
|
cmd := idevCmd(exec.Command("ideviceinfo"))
|
|
|
|
out, err := cmd.Output()
|
2016-03-23 09:17:44 -06:00
|
|
|
if err != nil {
|
2018-04-15 10:39:14 -06:00
|
|
|
return "", fmt.Errorf("ideviceinfo: %v", err)
|
2017-08-20 10:57:18 -06:00
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
var iosVer, buildVer string
|
|
|
|
lines := bytes.Split(out, []byte("\n"))
|
|
|
|
for _, line := range lines {
|
2021-09-22 08:46:32 -06:00
|
|
|
key, val, ok := strings.Cut(string(line), ": ")
|
|
|
|
if !ok {
|
2018-04-15 10:39:14 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "ProductVersion":
|
|
|
|
iosVer = val
|
|
|
|
case "BuildVersion":
|
|
|
|
buildVer = val
|
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
if iosVer == "" || buildVer == "" {
|
|
|
|
return "", errors.New("failed to parse ideviceinfo output")
|
2015-03-06 07:45:24 -07:00
|
|
|
}
|
2018-05-03 05:43:52 -06:00
|
|
|
verSplit := strings.Split(iosVer, ".")
|
|
|
|
if len(verSplit) > 2 {
|
|
|
|
// Developer images are specific to major.minor ios version.
|
|
|
|
// Cut off the patch version.
|
|
|
|
iosVer = strings.Join(verSplit[:2], ".")
|
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
sdkBase := "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport"
|
|
|
|
patterns := []string{fmt.Sprintf("%s (%s)", iosVer, buildVer), fmt.Sprintf("%s (*)", iosVer), fmt.Sprintf("%s*", iosVer)}
|
|
|
|
for _, pattern := range patterns {
|
|
|
|
matches, err := filepath.Glob(filepath.Join(sdkBase, pattern, "DeveloperDiskImage.dmg"))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("findDevImage: %v", err)
|
|
|
|
}
|
|
|
|
if len(matches) > 0 {
|
|
|
|
return matches[0], nil
|
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
return "", fmt.Errorf("failed to find matching developer image for iOS version %s build %s", iosVer, buildVer)
|
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
|
2018-04-15 10:39:14 -06:00
|
|
|
// startDebugBridge ensures that the idevicedebugserverproxy runs on
|
|
|
|
// port 3222.
|
|
|
|
func startDebugBridge() (func(), error) {
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
cmd := idevCmd(exec.Command("idevicedebugserverproxy", "3222"))
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nil, fmt.Errorf("idevicedebugserverproxy: %v", err)
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
|
|
|
go func() {
|
2018-04-15 10:39:14 -06:00
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
|
|
errChan <- fmt.Errorf("idevicedebugserverproxy: %s", stderr.Bytes())
|
|
|
|
} else {
|
|
|
|
errChan <- fmt.Errorf("idevicedebugserverproxy: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errChan <- nil
|
2015-03-01 11:47:54 -07:00
|
|
|
}()
|
2018-04-15 10:39:14 -06:00
|
|
|
closer := func() {
|
|
|
|
cmd.Process.Kill()
|
|
|
|
<-errChan
|
|
|
|
}
|
|
|
|
// Dial localhost:3222 to ensure the proxy is ready.
|
|
|
|
delay := time.Second / 4
|
|
|
|
for attempt := 0; attempt < 5; attempt++ {
|
|
|
|
conn, err := net.DialTimeout("tcp", "localhost:3222", 5*time.Second)
|
|
|
|
if err == nil {
|
|
|
|
conn.Close()
|
|
|
|
return closer, nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(delay):
|
|
|
|
delay *= 2
|
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
closer()
|
|
|
|
return nil, errors.New("failed to set up idevicedebugserverproxy")
|
2015-11-04 09:21:55 -07:00
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
|
2018-04-15 10:39:14 -06:00
|
|
|
// findDeviceAppPath returns the device path to the app with the
|
|
|
|
// given bundle ID. It parses the output of ideviceinstaller -l -o xml,
|
|
|
|
// looking for the bundle ID and the corresponding Path value.
|
|
|
|
func findDeviceAppPath(bundleID string) (string, error) {
|
|
|
|
cmd := idevCmd(exec.Command("ideviceinstaller", "-l", "-o", "xml"))
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
os.Stderr.Write(out)
|
|
|
|
return "", fmt.Errorf("ideviceinstaller: -l -o xml %v", err)
|
|
|
|
}
|
|
|
|
var list struct {
|
|
|
|
Apps []struct {
|
|
|
|
Data []byte `xml:",innerxml"`
|
|
|
|
} `xml:"array>dict"`
|
|
|
|
}
|
|
|
|
if err := xml.Unmarshal(out, &list); err != nil {
|
2018-05-03 05:43:52 -06:00
|
|
|
return "", fmt.Errorf("failed to parse ideviceinstaller output: %v", err)
|
2018-04-15 10:39:14 -06:00
|
|
|
}
|
|
|
|
for _, app := range list.Apps {
|
2018-05-03 05:43:52 -06:00
|
|
|
values, err := parsePlistDict(app.Data)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("findDeviceAppPath: failed to parse app dict: %v", err)
|
2018-04-15 10:39:14 -06:00
|
|
|
}
|
|
|
|
if values["CFBundleIdentifier"] == bundleID {
|
|
|
|
if path, ok := values["Path"]; ok {
|
|
|
|
return path, nil
|
2015-11-04 09:21:55 -07:00
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
return "", fmt.Errorf("failed to find device path for bundle: %s", bundleID)
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
|
|
|
|
2018-05-03 05:43:52 -06:00
|
|
|
// Parse an xml encoded plist. Plist values are mapped to string.
|
|
|
|
func parsePlistDict(dict []byte) (map[string]string, error) {
|
|
|
|
d := xml.NewDecoder(bytes.NewReader(dict))
|
|
|
|
values := make(map[string]string)
|
|
|
|
var key string
|
|
|
|
var hasKey bool
|
|
|
|
for {
|
|
|
|
tok, err := d.Token()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tok, ok := tok.(xml.StartElement); ok {
|
|
|
|
if tok.Name.Local == "key" {
|
|
|
|
if err := d.DecodeElement(&key, &tok); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hasKey = true
|
|
|
|
} else if hasKey {
|
|
|
|
var val string
|
|
|
|
var err error
|
|
|
|
switch n := tok.Name.Local; n {
|
|
|
|
case "true", "false":
|
|
|
|
// Bools are represented as <true/> and <false/>.
|
|
|
|
val = n
|
|
|
|
err = d.Skip()
|
|
|
|
default:
|
|
|
|
err = d.DecodeElement(&val, &tok)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
values[key] = val
|
|
|
|
hasKey = false
|
|
|
|
} else {
|
|
|
|
if err := d.Skip(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values, nil
|
|
|
|
}
|
|
|
|
|
2020-09-16 07:23:58 -06:00
|
|
|
func installSimulator(appdir string) error {
|
|
|
|
cmd := exec.Command(
|
|
|
|
"xcrun", "simctl", "install",
|
|
|
|
"booted", // Install to the booted simulator.
|
|
|
|
appdir,
|
|
|
|
)
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
os.Stderr.Write(out)
|
|
|
|
return fmt.Errorf("xcrun simctl install booted %q: %v", appdir, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func uninstallDevice(bundleID string) error {
|
2018-05-07 05:05:27 -06:00
|
|
|
cmd := idevCmd(exec.Command(
|
|
|
|
"ideviceinstaller",
|
|
|
|
"-U", bundleID,
|
|
|
|
))
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
os.Stderr.Write(out)
|
|
|
|
return fmt.Errorf("ideviceinstaller -U %q: %s", bundleID, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-16 07:23:58 -06:00
|
|
|
func installDevice(appdir string) error {
|
2018-05-02 15:26:58 -06:00
|
|
|
attempt := 0
|
|
|
|
for {
|
|
|
|
cmd := idevCmd(exec.Command(
|
|
|
|
"ideviceinstaller",
|
|
|
|
"-i", appdir,
|
|
|
|
))
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
// Sometimes, installing the app fails for some reason.
|
|
|
|
// Give the device a few seconds and try again.
|
|
|
|
if attempt < 5 {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
attempt++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
os.Stderr.Write(out)
|
|
|
|
return fmt.Errorf("ideviceinstaller -i %q: %v (%d attempts)", appdir, err, attempt)
|
|
|
|
}
|
|
|
|
return nil
|
2018-04-15 10:39:14 -06:00
|
|
|
}
|
2015-03-06 07:45:24 -07:00
|
|
|
}
|
|
|
|
|
2018-04-15 10:39:14 -06:00
|
|
|
func idevCmd(cmd *exec.Cmd) *exec.Cmd {
|
|
|
|
if deviceID != "" {
|
2018-05-08 14:59:35 -06:00
|
|
|
// Inject -u device_id after the executable, but before the arguments.
|
|
|
|
args := []string{cmd.Args[0], "-u", deviceID}
|
|
|
|
cmd.Args = append(args, cmd.Args[1:]...)
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
2018-04-15 10:39:14 -06:00
|
|
|
return cmd
|
|
|
|
}
|
2015-03-06 07:45:24 -07:00
|
|
|
|
2020-09-16 07:23:58 -06:00
|
|
|
func runSimulator(appdir, bundleID string, args []string) error {
|
|
|
|
cmd := exec.Command(
|
|
|
|
"xcrun", "simctl", "launch",
|
|
|
|
"--wait-for-debugger",
|
|
|
|
"booted",
|
|
|
|
bundleID,
|
|
|
|
)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
os.Stderr.Write(out)
|
|
|
|
return fmt.Errorf("xcrun simctl launch booted %q: %v", bundleID, err)
|
|
|
|
}
|
|
|
|
var processID int
|
|
|
|
var ignore string
|
|
|
|
if _, err := fmt.Sscanf(string(out), "%s %d", &ignore, &processID); err != nil {
|
|
|
|
return fmt.Errorf("runSimulator: couldn't find processID from `simctl launch`: %v (%q)", err, out)
|
2018-05-02 12:13:14 -06:00
|
|
|
}
|
2020-09-16 07:23:58 -06:00
|
|
|
_, err = runLLDB("ios-simulator", appdir, strconv.Itoa(processID), args)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func runDevice(appdir, bundleID string, args []string) error {
|
2018-05-03 03:43:25 -06:00
|
|
|
attempt := 0
|
|
|
|
for {
|
2018-05-08 02:21:09 -06:00
|
|
|
// The device app path reported by the device might be stale, so retry
|
|
|
|
// the lookup of the device path along with the lldb launching below.
|
2018-05-03 03:43:25 -06:00
|
|
|
deviceapp, err := findDeviceAppPath(bundleID)
|
|
|
|
if err != nil {
|
2018-05-08 02:21:09 -06:00
|
|
|
// The device app path might not yet exist for a newly installed app.
|
|
|
|
if attempt == 5 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
attempt++
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
continue
|
2018-05-03 03:43:25 -06:00
|
|
|
}
|
2020-09-16 07:23:58 -06:00
|
|
|
out, err := runLLDB("remote-ios", appdir, deviceapp, args)
|
2018-05-03 03:43:25 -06:00
|
|
|
// If the program was not started it can be retried without papering over
|
|
|
|
// real test failures.
|
2020-09-16 07:23:58 -06:00
|
|
|
started := bytes.HasPrefix(out, []byte("lldb: running program"))
|
2018-05-03 03:43:25 -06:00
|
|
|
if started || err == nil || attempt == 5 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Sometimes, the app was not yet ready to launch or the device path was
|
|
|
|
// stale. Retry.
|
|
|
|
attempt++
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
|
|
|
|
2020-09-16 07:23:58 -06:00
|
|
|
func runLLDB(target, appdir, deviceapp string, args []string) ([]byte, error) {
|
|
|
|
var env []string
|
|
|
|
for _, e := range os.Environ() {
|
|
|
|
// Don't override TMPDIR, HOME, GOCACHE on the device.
|
|
|
|
if strings.HasPrefix(e, "TMPDIR=") || strings.HasPrefix(e, "HOME=") || strings.HasPrefix(e, "GOCACHE=") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
env = append(env, e)
|
|
|
|
}
|
|
|
|
lldb := exec.Command(
|
|
|
|
"python",
|
|
|
|
"-", // Read script from stdin.
|
|
|
|
target,
|
|
|
|
appdir,
|
|
|
|
deviceapp,
|
|
|
|
)
|
|
|
|
lldb.Args = append(lldb.Args, args...)
|
|
|
|
lldb.Env = env
|
|
|
|
lldb.Stdin = strings.NewReader(lldbDriver)
|
|
|
|
lldb.Stdout = os.Stdout
|
|
|
|
var out bytes.Buffer
|
|
|
|
lldb.Stderr = io.MultiWriter(&out, os.Stderr)
|
|
|
|
err := lldb.Start()
|
|
|
|
if err == nil {
|
|
|
|
// Forward SIGQUIT to the lldb driver which in turn will forward
|
|
|
|
// to the running program.
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGQUIT)
|
|
|
|
proc := lldb.Process
|
|
|
|
go func() {
|
|
|
|
for sig := range sigs {
|
|
|
|
proc.Signal(sig)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err = lldb.Wait()
|
|
|
|
signal.Stop(sigs)
|
|
|
|
close(sigs)
|
|
|
|
}
|
|
|
|
return out.Bytes(), err
|
|
|
|
}
|
|
|
|
|
2015-03-01 11:47:54 -07:00
|
|
|
func copyLocalDir(dst, src string) error {
|
|
|
|
if err := os.Mkdir(dst, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer d.Close()
|
|
|
|
fi, err := d.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range fi {
|
|
|
|
if f.IsDir() {
|
|
|
|
if f.Name() == "testdata" {
|
|
|
|
if err := cp(dst, filepath.Join(src, f.Name())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := cp(dst, filepath.Join(src, f.Name())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cp(dst, src string) error {
|
|
|
|
out, err := exec.Command("cp", "-a", src, dst).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
os.Stderr.Write(out)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyLocalData(dstbase string) (pkgpath string, err error) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
finalPkgpath, underGoRoot, err := subdir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
cwd = strings.TrimSuffix(cwd, finalPkgpath)
|
|
|
|
|
|
|
|
// Copy all immediate files and testdata directories between
|
|
|
|
// the package being tested and the source root.
|
|
|
|
pkgpath = ""
|
|
|
|
for _, element := range strings.Split(finalPkgpath, string(filepath.Separator)) {
|
|
|
|
if debug {
|
|
|
|
log.Printf("copying %s", pkgpath)
|
|
|
|
}
|
|
|
|
pkgpath = filepath.Join(pkgpath, element)
|
|
|
|
dst := filepath.Join(dstbase, pkgpath)
|
|
|
|
src := filepath.Join(cwd, pkgpath)
|
|
|
|
if err := copyLocalDir(dst, src); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if underGoRoot {
|
2017-04-17 13:09:56 -06:00
|
|
|
// Copy timezone file.
|
|
|
|
//
|
|
|
|
// Typical apps have the zoneinfo.zip in the root of their app bundle,
|
|
|
|
// read by the time package as the working directory at initialization.
|
|
|
|
// As we move the working directory to the GOROOT pkg directory, we
|
|
|
|
// install the zoneinfo.zip file in the pkgpath.
|
2015-03-02 14:05:11 -07:00
|
|
|
err := cp(
|
2017-02-01 11:54:03 -07:00
|
|
|
filepath.Join(dstbase, pkgpath),
|
2015-03-02 14:05:11 -07:00
|
|
|
filepath.Join(cwd, "lib", "time", "zoneinfo.zip"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2015-03-01 11:47:54 -07:00
|
|
|
return "", err
|
|
|
|
}
|
2017-04-17 13:09:56 -06:00
|
|
|
// Copy src/runtime/textflag.h for (at least) Test386EndToEnd in
|
|
|
|
// cmd/asm/internal/asm.
|
|
|
|
runtimePath := filepath.Join(dstbase, "src", "runtime")
|
|
|
|
if err := os.MkdirAll(runtimePath, 0755); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = cp(
|
|
|
|
filepath.Join(runtimePath, "textflag.h"),
|
|
|
|
filepath.Join(cwd, "src", "runtime", "textflag.h"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return finalPkgpath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// subdir determines the package based on the current working directory,
|
|
|
|
// and returns the path to the package source relative to $GOROOT (or $GOPATH).
|
|
|
|
func subdir() (pkgpath string, underGoRoot bool, err error) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
2019-03-01 00:25:35 -07:00
|
|
|
cwd, err = filepath.EvalSymlinks(cwd)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-02-28 17:15:24 -07:00
|
|
|
goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(cwd, goroot) {
|
|
|
|
subdir, err := filepath.Rel(goroot, cwd)
|
2015-03-01 11:47:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
return subdir, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range filepath.SplitList(build.Default.GOPATH) {
|
2019-02-28 17:15:24 -07:00
|
|
|
pabs, err := filepath.EvalSymlinks(p)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(cwd, pabs) {
|
2015-03-01 11:47:54 -07:00
|
|
|
continue
|
|
|
|
}
|
2019-02-28 17:15:24 -07:00
|
|
|
subdir, err := filepath.Rel(pabs, cwd)
|
2015-03-01 11:47:54 -07:00
|
|
|
if err == nil {
|
|
|
|
return subdir, false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false, fmt.Errorf(
|
|
|
|
"working directory %q is not in either GOROOT(%q) or GOPATH(%q)",
|
|
|
|
cwd,
|
|
|
|
runtime.GOROOT(),
|
|
|
|
build.Default.GOPATH,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
misc/ios,cmd/go, runtime/cgo: fix iOS test harness (again)
The iOS test harness was recently changed in response to lldb bugs
to replace breakpoints with the SIGUSR2 signal (CL 34926), and to
pass the current directory in the test binary arguments (CL 35152).
Both the signal sending and working directory setup is done from
the go test driver.
However, the new method doesn't work with tests where a C program is
the test driver instead of go test: the current working directory
will not be changed and SIGUSR2 is not raised.
Instead of copying that logic into any C test program, rework the
test harness (again) to move the setup logic to the early runtime
cgo setup code. That way, the harness will run even in the library
build modes.
Then, use the app Info.plist file to pass the working
directory, removing the need to alter the arguments after running.
Finally, use the SIGINT signal instead of SIGUSR2 to avoid
manipulating the signal masks or handlers.
Fixes the testcarchive tests on iOS.
With this CL, both darwin/arm and darwin/arm64 passes all.bash.
This CL replaces CL 34926, CL 35152 as well as the fixup CL
35123 and CL 35255. They are reverted in CLs earlier in the
relation chain.
Change-Id: I8485c7db1404fbd8daa261efd1ea89e905121a3e
Reviewed-on: https://go-review.googlesource.com/36090
Run-TryBot: Elias Naur <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-01 08:04:07 -07:00
|
|
|
func infoPlist(pkgpath string) string {
|
2017-02-01 01:37:47 -07:00
|
|
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
2015-03-01 11:47:54 -07:00
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
<plist version="1.0">
|
|
|
|
<dict>
|
|
|
|
<key>CFBundleName</key><string>golang.gotest</string>
|
|
|
|
<key>CFBundleSupportedPlatforms</key><array><string>iPhoneOS</string></array>
|
|
|
|
<key>CFBundleExecutable</key><string>gotest</string>
|
|
|
|
<key>CFBundleVersion</key><string>1.0</string>
|
2020-09-16 07:23:58 -06:00
|
|
|
<key>CFBundleShortVersionString</key><string>1.0</string>
|
2017-02-01 01:37:47 -07:00
|
|
|
<key>CFBundleIdentifier</key><string>` + bundleID + `</string>
|
2015-03-01 11:47:54 -07:00
|
|
|
<key>CFBundleResourceSpecification</key><string>ResourceRules.plist</string>
|
|
|
|
<key>LSRequiresIPhoneOS</key><true/>
|
|
|
|
<key>CFBundleDisplayName</key><string>gotest</string>
|
misc/ios,cmd/go, runtime/cgo: fix iOS test harness (again)
The iOS test harness was recently changed in response to lldb bugs
to replace breakpoints with the SIGUSR2 signal (CL 34926), and to
pass the current directory in the test binary arguments (CL 35152).
Both the signal sending and working directory setup is done from
the go test driver.
However, the new method doesn't work with tests where a C program is
the test driver instead of go test: the current working directory
will not be changed and SIGUSR2 is not raised.
Instead of copying that logic into any C test program, rework the
test harness (again) to move the setup logic to the early runtime
cgo setup code. That way, the harness will run even in the library
build modes.
Then, use the app Info.plist file to pass the working
directory, removing the need to alter the arguments after running.
Finally, use the SIGINT signal instead of SIGUSR2 to avoid
manipulating the signal masks or handlers.
Fixes the testcarchive tests on iOS.
With this CL, both darwin/arm and darwin/arm64 passes all.bash.
This CL replaces CL 34926, CL 35152 as well as the fixup CL
35123 and CL 35255. They are reverted in CLs earlier in the
relation chain.
Change-Id: I8485c7db1404fbd8daa261efd1ea89e905121a3e
Reviewed-on: https://go-review.googlesource.com/36090
Run-TryBot: Elias Naur <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-01 08:04:07 -07:00
|
|
|
<key>GoExecWrapperWorkingDirectory</key><string>` + pkgpath + `</string>
|
2015-03-01 11:47:54 -07:00
|
|
|
</dict>
|
|
|
|
</plist>
|
|
|
|
`
|
2017-02-01 01:37:47 -07:00
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
|
2015-04-13 12:31:41 -06:00
|
|
|
func entitlementsPlist() string {
|
|
|
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
2015-03-01 11:47:54 -07:00
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
<plist version="1.0">
|
|
|
|
<dict>
|
|
|
|
<key>keychain-access-groups</key>
|
2017-02-01 01:37:47 -07:00
|
|
|
<array><string>` + appID + `</string></array>
|
2015-03-01 11:47:54 -07:00
|
|
|
<key>get-task-allow</key>
|
|
|
|
<true/>
|
|
|
|
<key>application-identifier</key>
|
2017-02-01 01:37:47 -07:00
|
|
|
<string>` + appID + `</string>
|
2015-03-01 11:47:54 -07:00
|
|
|
<key>com.apple.developer.team-identifier</key>
|
2015-04-13 12:31:41 -06:00
|
|
|
<string>` + teamID + `</string>
|
2015-03-01 11:47:54 -07:00
|
|
|
</dict>
|
2015-05-03 01:13:46 -06:00
|
|
|
</plist>
|
|
|
|
`
|
2015-04-13 12:31:41 -06:00
|
|
|
}
|
2015-03-01 11:47:54 -07:00
|
|
|
|
|
|
|
const resourceRules = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
<plist version="1.0">
|
|
|
|
<dict>
|
2015-05-03 01:13:46 -06:00
|
|
|
<key>rules</key>
|
|
|
|
<dict>
|
|
|
|
<key>.*</key>
|
|
|
|
<true/>
|
|
|
|
<key>Info.plist</key>
|
2015-03-01 11:47:54 -07:00
|
|
|
<dict>
|
2015-05-03 01:13:46 -06:00
|
|
|
<key>omit</key>
|
|
|
|
<true/>
|
|
|
|
<key>weight</key>
|
|
|
|
<integer>10</integer>
|
2015-03-01 11:47:54 -07:00
|
|
|
</dict>
|
|
|
|
<key>ResourceRules.plist</key>
|
|
|
|
<dict>
|
2015-05-03 01:13:46 -06:00
|
|
|
<key>omit</key>
|
|
|
|
<true/>
|
|
|
|
<key>weight</key>
|
|
|
|
<integer>100</integer>
|
2015-03-01 11:47:54 -07:00
|
|
|
</dict>
|
|
|
|
</dict>
|
|
|
|
</dict>
|
|
|
|
</plist>
|
|
|
|
`
|
2018-04-15 10:39:14 -06:00
|
|
|
|
|
|
|
const lldbDriver = `
|
|
|
|
import sys
|
|
|
|
import os
|
misc/ios: forward SIGQUIT to the iOS program
When running tests that fails to complete within the test timeout,
the go tool sends the test program a SIGQUIT signal to print
backtraces. However, for tests running with an exec wrapper, the
resulting backtraces will come from the exec wrapper process and
not the test program.
Change the iOS exec wrapper to forward SIGQUIT signals to the lldb
python driver and change the driver to forward the signals to the
running test on the device.
Before:
$ GOARCH=arm64 go test forever_test.go
lldb: running program
SIGQUIT: quit
PC=0x10816fe m=0 sigcode=0
goroutine 54 [syscall]:
syscall.Syscall6(0x7, 0x16ab, 0xc000033dfc, 0x0, 0xc000116f30, 0x0, 0x0, 0xc000116f30, 0x0, 0x1328820)
/Users/elias/go-tip/src/syscall/asm_darwin_amd64.s:41 +0x5 fp=0xc000033d48 sp=0xc000033d40 pc=0x10816d5
syscall.wait4(0x16ab, 0xc000033dfc, 0x0, 0xc000116f30, 0x90, 0x1200e00, 0x1)
/Users/elias/go-tip/src/syscall/zsyscall_darwin_amd64.go:34 +0x7b fp=0xc000033dc0 sp=0xc000033d48 pc=0x107e4eb
syscall.Wait4(0x16ab, 0xc000033e4c, 0x0, 0xc000116f30, 0xc0000fd518, 0x0, 0x0)
/Users/elias/go-tip/src/syscall/syscall_bsd.go:129 +0x51 fp=0xc000033e10 sp=0xc000033dc0 pc=0x107b7b1
os.(*Process).wait(0xc00008d440, 0x1095e2e, 0xc0000fd518, 0x0)
/Users/elias/go-tip/src/os/exec_unix.go:38 +0x7b fp=0xc000033e80 sp=0xc000033e10 pc=0x109af2b
os.(*Process).Wait(0xc00008d440, 0xc000033fb0, 0x10, 0x11d1f00)
/Users/elias/go-tip/src/os/exec.go:125 +0x2b fp=0xc000033eb0 sp=0xc000033e80 pc=0x109a47b
os/exec.(*Cmd).Wait(0xc0000b1ce0, 0xc000033f90, 0x11394df)
/Users/elias/go-tip/src/os/exec/exec.go:463 +0x5b fp=0xc000033f28 sp=0xc000033eb0 pc=0x1136f0b
main.startDebugBridge.func1(0xc0000b1ce0, 0xc0000b8ae0, 0xc0000e2a80)
/Users/elias/go-tip/misc/ios/go_darwin_arm_exec.go:314 +0x40 fp=0xc000033fc8 sp=0xc000033f28 pc=0x11a1980
runtime.goexit()
/Users/elias/go-tip/src/runtime/asm_amd64.s:1360 +0x1 fp=0xc000033fd0 sp=0xc000033fc8 pc=0x10565a1
created by main.startDebugBridge
/Users/elias/go-tip/misc/ios/go_darwin_arm_exec.go:313 +0x15f
...
After:
$ GOARCH=arm64 go test forever_test.go
lldb: running program
=== RUN TestForever
SIGQUIT: quit
PC=0x100144e24 m=0 sigcode=0
...
goroutine 19 [select (no cases)]:
command-line-arguments.TestForever(0x1300b60f0)
/Users/elias/go-tip/src/forever_test.go:6 +0x18
testing.tRunner(0x1300b60f0, 0x100211aa0)
/Users/elias/go-tip/src/testing/testing.go:795 +0xa8
created by testing.(*T).Run
/Users/elias/go-tip/src/testing/testing.go:840 +0x22c
...
Change-Id: I6b3cf1662d07a43ade0530842733b0944bee1ace
Reviewed-on: https://go-review.googlesource.com/112676
Run-TryBot: Elias Naur <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-05-11 09:46:00 -06:00
|
|
|
import signal
|
2018-04-15 10:39:14 -06:00
|
|
|
|
2020-09-16 07:23:58 -06:00
|
|
|
platform, exe, device_exe_or_pid, args = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4:]
|
2018-04-15 10:39:14 -06:00
|
|
|
|
|
|
|
env = []
|
|
|
|
for k, v in os.environ.items():
|
|
|
|
env.append(k + "=" + v)
|
|
|
|
|
|
|
|
sys.path.append('/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python')
|
|
|
|
|
|
|
|
import lldb
|
|
|
|
|
|
|
|
debugger = lldb.SBDebugger.Create()
|
|
|
|
debugger.SetAsync(True)
|
|
|
|
debugger.SkipLLDBInitFiles(True)
|
|
|
|
|
|
|
|
err = lldb.SBError()
|
2020-09-16 07:23:58 -06:00
|
|
|
target = debugger.CreateTarget(exe, None, platform, True, err)
|
2018-04-15 10:39:14 -06:00
|
|
|
if not target.IsValid() or not err.Success():
|
|
|
|
sys.stderr.write("lldb: failed to setup up target: %s\n" % (err))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
listener = debugger.GetListener()
|
2020-09-16 07:23:58 -06:00
|
|
|
|
|
|
|
if platform == 'remote-ios':
|
|
|
|
target.modules[0].SetPlatformFileSpec(lldb.SBFileSpec(device_exe_or_pid))
|
|
|
|
process = target.ConnectRemote(listener, 'connect://localhost:3222', None, err)
|
|
|
|
else:
|
|
|
|
process = target.AttachToProcessWithID(listener, int(device_exe_or_pid), err)
|
|
|
|
|
2018-04-15 10:39:14 -06:00
|
|
|
if not err.Success():
|
2020-09-16 07:23:58 -06:00
|
|
|
sys.stderr.write("lldb: failed to connect to remote target %s: %s\n" % (device_exe_or_pid, err))
|
2018-04-15 10:39:14 -06:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Don't stop on signals.
|
|
|
|
sigs = process.GetUnixSignals()
|
|
|
|
for i in range(0, sigs.GetNumSignals()):
|
|
|
|
sig = sigs.GetSignalAtIndex(i)
|
|
|
|
sigs.SetShouldStop(sig, False)
|
|
|
|
sigs.SetShouldNotify(sig, False)
|
|
|
|
|
|
|
|
event = lldb.SBEvent()
|
2018-05-03 03:43:25 -06:00
|
|
|
running = False
|
misc/ios: forward SIGQUIT to the iOS program
When running tests that fails to complete within the test timeout,
the go tool sends the test program a SIGQUIT signal to print
backtraces. However, for tests running with an exec wrapper, the
resulting backtraces will come from the exec wrapper process and
not the test program.
Change the iOS exec wrapper to forward SIGQUIT signals to the lldb
python driver and change the driver to forward the signals to the
running test on the device.
Before:
$ GOARCH=arm64 go test forever_test.go
lldb: running program
SIGQUIT: quit
PC=0x10816fe m=0 sigcode=0
goroutine 54 [syscall]:
syscall.Syscall6(0x7, 0x16ab, 0xc000033dfc, 0x0, 0xc000116f30, 0x0, 0x0, 0xc000116f30, 0x0, 0x1328820)
/Users/elias/go-tip/src/syscall/asm_darwin_amd64.s:41 +0x5 fp=0xc000033d48 sp=0xc000033d40 pc=0x10816d5
syscall.wait4(0x16ab, 0xc000033dfc, 0x0, 0xc000116f30, 0x90, 0x1200e00, 0x1)
/Users/elias/go-tip/src/syscall/zsyscall_darwin_amd64.go:34 +0x7b fp=0xc000033dc0 sp=0xc000033d48 pc=0x107e4eb
syscall.Wait4(0x16ab, 0xc000033e4c, 0x0, 0xc000116f30, 0xc0000fd518, 0x0, 0x0)
/Users/elias/go-tip/src/syscall/syscall_bsd.go:129 +0x51 fp=0xc000033e10 sp=0xc000033dc0 pc=0x107b7b1
os.(*Process).wait(0xc00008d440, 0x1095e2e, 0xc0000fd518, 0x0)
/Users/elias/go-tip/src/os/exec_unix.go:38 +0x7b fp=0xc000033e80 sp=0xc000033e10 pc=0x109af2b
os.(*Process).Wait(0xc00008d440, 0xc000033fb0, 0x10, 0x11d1f00)
/Users/elias/go-tip/src/os/exec.go:125 +0x2b fp=0xc000033eb0 sp=0xc000033e80 pc=0x109a47b
os/exec.(*Cmd).Wait(0xc0000b1ce0, 0xc000033f90, 0x11394df)
/Users/elias/go-tip/src/os/exec/exec.go:463 +0x5b fp=0xc000033f28 sp=0xc000033eb0 pc=0x1136f0b
main.startDebugBridge.func1(0xc0000b1ce0, 0xc0000b8ae0, 0xc0000e2a80)
/Users/elias/go-tip/misc/ios/go_darwin_arm_exec.go:314 +0x40 fp=0xc000033fc8 sp=0xc000033f28 pc=0x11a1980
runtime.goexit()
/Users/elias/go-tip/src/runtime/asm_amd64.s:1360 +0x1 fp=0xc000033fd0 sp=0xc000033fc8 pc=0x10565a1
created by main.startDebugBridge
/Users/elias/go-tip/misc/ios/go_darwin_arm_exec.go:313 +0x15f
...
After:
$ GOARCH=arm64 go test forever_test.go
lldb: running program
=== RUN TestForever
SIGQUIT: quit
PC=0x100144e24 m=0 sigcode=0
...
goroutine 19 [select (no cases)]:
command-line-arguments.TestForever(0x1300b60f0)
/Users/elias/go-tip/src/forever_test.go:6 +0x18
testing.tRunner(0x1300b60f0, 0x100211aa0)
/Users/elias/go-tip/src/testing/testing.go:795 +0xa8
created by testing.(*T).Run
/Users/elias/go-tip/src/testing/testing.go:840 +0x22c
...
Change-Id: I6b3cf1662d07a43ade0530842733b0944bee1ace
Reviewed-on: https://go-review.googlesource.com/112676
Run-TryBot: Elias Naur <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-05-11 09:46:00 -06:00
|
|
|
prev_handler = None
|
2020-09-16 07:23:58 -06:00
|
|
|
|
|
|
|
def signal_handler(signal, frame):
|
|
|
|
process.Signal(signal)
|
|
|
|
|
|
|
|
def run_program():
|
|
|
|
# Forward SIGQUIT to the program.
|
|
|
|
prev_handler = signal.signal(signal.SIGQUIT, signal_handler)
|
|
|
|
# Tell the Go driver that the program is running and should not be retried.
|
|
|
|
sys.stderr.write("lldb: running program\n")
|
|
|
|
running = True
|
|
|
|
# Process is stopped at attach/launch. Let it run.
|
|
|
|
process.Continue()
|
|
|
|
|
|
|
|
if platform != 'remote-ios':
|
|
|
|
# For the local emulator the program is ready to run.
|
|
|
|
# For remote device runs, we need to wait for eStateConnected,
|
|
|
|
# below.
|
|
|
|
run_program()
|
|
|
|
|
2018-04-15 10:39:14 -06:00
|
|
|
while True:
|
|
|
|
if not listener.WaitForEvent(1, event):
|
|
|
|
continue
|
|
|
|
if not lldb.SBProcess.EventIsProcessEvent(event):
|
|
|
|
continue
|
2018-05-03 03:43:25 -06:00
|
|
|
if running:
|
|
|
|
# Pass through stdout and stderr.
|
|
|
|
while True:
|
|
|
|
out = process.GetSTDOUT(8192)
|
|
|
|
if not out:
|
|
|
|
break
|
|
|
|
sys.stdout.write(out)
|
|
|
|
while True:
|
|
|
|
out = process.GetSTDERR(8192)
|
|
|
|
if not out:
|
|
|
|
break
|
|
|
|
sys.stderr.write(out)
|
2018-04-15 10:39:14 -06:00
|
|
|
state = process.GetStateFromEvent(event)
|
2018-05-03 01:48:32 -06:00
|
|
|
if state in [lldb.eStateCrashed, lldb.eStateDetached, lldb.eStateUnloaded, lldb.eStateExited]:
|
misc/ios: forward SIGQUIT to the iOS program
When running tests that fails to complete within the test timeout,
the go tool sends the test program a SIGQUIT signal to print
backtraces. However, for tests running with an exec wrapper, the
resulting backtraces will come from the exec wrapper process and
not the test program.
Change the iOS exec wrapper to forward SIGQUIT signals to the lldb
python driver and change the driver to forward the signals to the
running test on the device.
Before:
$ GOARCH=arm64 go test forever_test.go
lldb: running program
SIGQUIT: quit
PC=0x10816fe m=0 sigcode=0
goroutine 54 [syscall]:
syscall.Syscall6(0x7, 0x16ab, 0xc000033dfc, 0x0, 0xc000116f30, 0x0, 0x0, 0xc000116f30, 0x0, 0x1328820)
/Users/elias/go-tip/src/syscall/asm_darwin_amd64.s:41 +0x5 fp=0xc000033d48 sp=0xc000033d40 pc=0x10816d5
syscall.wait4(0x16ab, 0xc000033dfc, 0x0, 0xc000116f30, 0x90, 0x1200e00, 0x1)
/Users/elias/go-tip/src/syscall/zsyscall_darwin_amd64.go:34 +0x7b fp=0xc000033dc0 sp=0xc000033d48 pc=0x107e4eb
syscall.Wait4(0x16ab, 0xc000033e4c, 0x0, 0xc000116f30, 0xc0000fd518, 0x0, 0x0)
/Users/elias/go-tip/src/syscall/syscall_bsd.go:129 +0x51 fp=0xc000033e10 sp=0xc000033dc0 pc=0x107b7b1
os.(*Process).wait(0xc00008d440, 0x1095e2e, 0xc0000fd518, 0x0)
/Users/elias/go-tip/src/os/exec_unix.go:38 +0x7b fp=0xc000033e80 sp=0xc000033e10 pc=0x109af2b
os.(*Process).Wait(0xc00008d440, 0xc000033fb0, 0x10, 0x11d1f00)
/Users/elias/go-tip/src/os/exec.go:125 +0x2b fp=0xc000033eb0 sp=0xc000033e80 pc=0x109a47b
os/exec.(*Cmd).Wait(0xc0000b1ce0, 0xc000033f90, 0x11394df)
/Users/elias/go-tip/src/os/exec/exec.go:463 +0x5b fp=0xc000033f28 sp=0xc000033eb0 pc=0x1136f0b
main.startDebugBridge.func1(0xc0000b1ce0, 0xc0000b8ae0, 0xc0000e2a80)
/Users/elias/go-tip/misc/ios/go_darwin_arm_exec.go:314 +0x40 fp=0xc000033fc8 sp=0xc000033f28 pc=0x11a1980
runtime.goexit()
/Users/elias/go-tip/src/runtime/asm_amd64.s:1360 +0x1 fp=0xc000033fd0 sp=0xc000033fc8 pc=0x10565a1
created by main.startDebugBridge
/Users/elias/go-tip/misc/ios/go_darwin_arm_exec.go:313 +0x15f
...
After:
$ GOARCH=arm64 go test forever_test.go
lldb: running program
=== RUN TestForever
SIGQUIT: quit
PC=0x100144e24 m=0 sigcode=0
...
goroutine 19 [select (no cases)]:
command-line-arguments.TestForever(0x1300b60f0)
/Users/elias/go-tip/src/forever_test.go:6 +0x18
testing.tRunner(0x1300b60f0, 0x100211aa0)
/Users/elias/go-tip/src/testing/testing.go:795 +0xa8
created by testing.(*T).Run
/Users/elias/go-tip/src/testing/testing.go:840 +0x22c
...
Change-Id: I6b3cf1662d07a43ade0530842733b0944bee1ace
Reviewed-on: https://go-review.googlesource.com/112676
Run-TryBot: Elias Naur <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-05-11 09:46:00 -06:00
|
|
|
if running:
|
|
|
|
signal.signal(signal.SIGQUIT, prev_handler)
|
2018-04-15 10:39:14 -06:00
|
|
|
break
|
|
|
|
elif state == lldb.eStateConnected:
|
2020-09-16 07:23:58 -06:00
|
|
|
if platform == 'remote-ios':
|
|
|
|
process.RemoteLaunch(args, env, None, None, None, None, 0, False, err)
|
|
|
|
if not err.Success():
|
|
|
|
sys.stderr.write("lldb: failed to launch remote process: %s\n" % (err))
|
|
|
|
process.Kill()
|
|
|
|
debugger.Terminate()
|
|
|
|
sys.exit(1)
|
|
|
|
run_program()
|
2018-04-15 10:39:14 -06:00
|
|
|
|
|
|
|
exitStatus = process.GetExitStatus()
|
2020-09-16 07:23:58 -06:00
|
|
|
exitDesc = process.GetExitDescription()
|
2018-04-15 10:39:14 -06:00
|
|
|
process.Kill()
|
|
|
|
debugger.Terminate()
|
2020-09-16 07:23:58 -06:00
|
|
|
if exitStatus == 0 and exitDesc is not None:
|
|
|
|
# Ensure tests fail when killed by a signal.
|
|
|
|
exitStatus = 123
|
|
|
|
|
2018-04-15 10:39:14 -06:00
|
|
|
sys.exit(exitStatus)
|
|
|
|
`
|