New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: redirect println(runtime.writeErr) to NSLog(asl_log) on iOS #19744

Open
bronze1man opened this Issue Mar 28, 2017 · 9 comments

Comments

Projects
None yet
5 participants
@bronze1man

bronze1man commented Mar 28, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.7.5 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/m9/qtbxkp6s3p96fk54rln7qhj80000gp/T/go-build412033723=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

What did you do?

Get a uncatch panic on ios in network extension process.As I can not attach xcode to that process.I can only use devices window of xcode to view the NSLog output from that process.

What did you expect to see?

See panic message in NSLog output of that network extension process.

What did you see instead?

Nothing.

@bronze1man

This comment has been minimized.

bronze1man commented Mar 28, 2017

I managed to implement this feature with go1.7.5 .
I want someone who is familiar with runtime and cgo package to review my code.
So that I can sure they are correct in all situation.

add /usr/local/go/src/runtime/cgo/write_err_ios.go

// +build ios
// +build arm arm64

package cgo

/*
#include <asl.h>

void asl_log_wrap(const char *str) {
	asl_log(NULL, NULL, ASL_LEVEL_NOTICE, "%s", str);
}
*/
import "C"

add /usr/local/go/src/runtime/write_err_ios.go

// +build ios
// +build arm arm64

package runtime

import "unsafe"

var (
	gWriteBuf [1024]byte
	gWritePos int
)

//go:nosplit
func asmAslLogWrap(b unsafe.Pointer)

//go:linkname writeErr writeErr
func writeErr(b []byte) {
	// Write to stderr for command-line programs.
	write(2, unsafe.Pointer(&b[0]), int32(len(b)))

	for _, v := range b {
		if v == 0 {
			v = '0'
		}
		gWriteBuf[gWritePos] = v
		gWritePos++
		if v == '\n' || gWritePos == len(gWriteBuf)-1 {
			gWriteBuf[gWritePos] = 0
			asmAslLogWrap(unsafe.Pointer( &gWriteBuf[0] ))
			gWritePos = 0
		}
	}
}

//go:cgo_import_static asl_log_wrap

add /usr/local/go/src/runtime/write_err_ios_arm.s

// +build ios,arm

#include "go_asm.h"
#include "go_tls.h"
#include "funcdata.h"
#include "textflag.h"

TEXT runtime·asmAslLogWrap(SB),NOSPLIT,$-4
    MOVW 4(R13),R0 
    SUB $32,R13
    MOVW g,16(R13)
    MOVW R14,24(R13)

    BL asl_log_wrap(SB)

    MOVW 24(R13),R14
    MOVW 16(R13),g
    ADD $32,R13
	RET

add /usr/local/go/src/runtime/write_err_ios_arm64.s

// +build ios,arm64

#include "go_asm.h"
#include "go_tls.h"
#include "funcdata.h"
#include "textflag.h"

TEXT runtime·asmAslLogWrap(SB),NOSPLIT,$-8
    MOVD 8(RSP),R0 
    SUB $32,RSP
    MOVD g,16(RSP)
    MOVD R30,24(RSP)

    BL asl_log_wrap(SB)

    MOVD 24(RSP),R30
    MOVD 16(RSP),g
    ADD $32,RSP
	RET

change /usr/local/go/src/runtime/write_err.go

// Copyright 2009 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.

// +build !android
// +build !ios !arm,!arm64

package runtime

import "unsafe"

func writeErr(b []byte) {
	write(2, unsafe.Pointer(&b[0]), int32(len(b)))
}

I do not use NSLog, because NSLog will output its output to stderr. You will see two same message in xcode output window.
i do not add this feature to ios x86, because network extension process can not run on a ios simulator.
I do not send pr, because you guys do not allow it, and your guys may not accept this one. I just need it works right now.
I do not try apply this into go1.8, because I also have other modifies to my own version of go1.7.5.I will send more Proposals with change set to discuss those modifies with your guys later.

@gopherbot gopherbot added this to the Proposal milestone Mar 28, 2017

@gopherbot gopherbot added the Proposal label Mar 28, 2017

@bradfitz

This comment has been minimized.

Member

bradfitz commented Mar 28, 2017

@eliasnaur

This comment has been minimized.

Contributor

eliasnaur commented Mar 28, 2017

Seems reasonable. Android already redirects its output to android_log_print. @bronze1man would you like to propose your changes as a CL?

@bronze1man

This comment has been minimized.

bronze1man commented Mar 28, 2017

@eliasnaur
Sorry,I also have other patches to my own version of go1.7.5.
If I do not have them, my toolbox do not work.I do not know how to port one patch to go1.8. So I can not port this one to go1.8.I can not test them with go1.8 and ios right now.I do not know how to write/run golang code in ios without my toolbox right now.There is a lot of detail about cgo/compiler stuff that I have forgot.

Is it ok to send a CL to go1.7.5?

@bradfitz

This comment has been minimized.

Member

bradfitz commented Mar 28, 2017

Is it ok to send a CL to go1.7.5?

No. It's not even okay to send a CL to Go 1.8. You need to send a change to Go tip.

See https://golang.org/doc/contribute.html

@bronze1man

This comment has been minimized.

bronze1man commented Mar 28, 2017

@bradfitz
Looks like I need send another two propose/patches first, then this one.
Or other guys can help me test that code in Go tip and send a CL.

I managed to port #19745 to go1.8, but do not found a way to port #19746 to go1.8.

@rsc

This comment has been minimized.

Contributor

rsc commented Apr 3, 2017

Accepting based on discussion (Elias is in favor, Android already does this).

@rsc rsc modified the milestones: Unreleased, Proposal Apr 3, 2017

@rsc rsc changed the title from proposal: ios: redirect println(runtime.writeErr) to NSLog(asl_log) to runtime: redirect println(runtime.writeErr) to NSLog(asl_log) on iOS Apr 3, 2017

@bronze1man

This comment has been minimized.

bronze1man commented May 5, 2017

change set that works on go1.8(it is not the same as go1.7)

diff --git a/src/runtime/cgo/write_err_ios.c b/src/runtime/cgo/write_err_ios.c
new file mode 100644
index 0000000..452f525
--- /dev/null
+++ b/src/runtime/cgo/write_err_ios.c
@@ -0,0 +1,8 @@
+// +build ios
+// +build arm arm64
+
+#include <asl.h>
+
+void x_cgo_asl_log_wrap(const char *str) {
+	asl_log(NULL, NULL, ASL_LEVEL_NOTICE, "%s", str);
+}
\ No newline at end of file
diff --git a/src/runtime/cgo/write_err_ios.go b/src/runtime/cgo/write_err_ios.go
new file mode 100644
index 0000000..b9c4e67
--- /dev/null
+++ b/src/runtime/cgo/write_err_ios.go
@@ -0,0 +1,10 @@
+// +build ios
+// +build arm arm64
+
+package cgo
+
+import _ "unsafe"
+
+//go:cgo_import_static x_cgo_asl_log_wrap
+//go:linkname x_cgo_asl_log_wrap runtime.x_cgo_asl_log_wrap
+var x_cgo_asl_log_wrap byte
\ No newline at end of file
diff --git a/src/runtime/write_err.go b/src/runtime/write_err.go
index 6b1467b..73ed4ba 100644
--- a/src/runtime/write_err.go
+++ b/src/runtime/write_err.go
@@ -3,6 +3,7 @@
 // license that can be found in the LICENSE file.
 
 // +build !android
+// +build !ios !arm,!arm64
 
 package runtime
 
diff --git a/src/runtime/write_err_ios.go b/src/runtime/write_err_ios.go
new file mode 100644
index 0000000..059e6fd
--- /dev/null
+++ b/src/runtime/write_err_ios.go
@@ -0,0 +1,37 @@
+// +build ios
+// +build arm arm64
+
+package runtime
+
+import "unsafe"
+
+var (
+	gWriteBuf [1024]byte
+	gWritePos int
+)
+
+//go:nosplit
+func asmAslLogWrap(b unsafe.Pointer)
+
+//go:linkname writeErr writeErr
+func writeErr(b []byte) {
+	// Write to stderr for command-line programs.
+	write(2, unsafe.Pointer(&b[0]), int32(len(b)))
+
+	for _, v := range b {
+		if v == 0 {
+			v = '0'
+		}
+		gWriteBuf[gWritePos] = v
+		gWritePos++
+		if v == '\n' || gWritePos == len(gWriteBuf)-1 {
+			gWriteBuf[gWritePos] = 0
+			asmAslLogWrap(unsafe.Pointer( &gWriteBuf[0] ))
+			gWritePos = 0
+		}
+	}
+}
+
+// define in src/runtime/cgo/write_err_ios.go:10 and /usr/local/go/src/runtime/cgo/write_err_ios.c
+//go:linkname x_cgo_asl_log_wrap x_cgo_asl_log_wrap
+var x_cgo_asl_log_wrap unsafe.Pointer
\ No newline at end of file
diff --git a/src/runtime/write_err_ios_arm.s b/src/runtime/write_err_ios_arm.s
new file mode 100644
index 0000000..57be68e
--- /dev/null
+++ b/src/runtime/write_err_ios_arm.s
@@ -0,0 +1,21 @@
+// +build ios,arm
+
+#include "go_asm.h"
+#include "go_tls.h"
+#include "funcdata.h"
+#include "textflag.h"
+
+TEXT runtime·asmAslLogWrap(SB),NOSPLIT,$-4
+    MOVW 4(R13),R0 // 第一个参数
+    SUB $32,R13
+    MOVW g,16(R13)
+    MOVW R14,24(R13)
+
+    BL x_cgo_asl_log_wrap(SB)
+
+    MOVW 24(R13),R14
+    MOVW 16(R13),g
+    ADD $32,R13
+	RET
+
+
diff --git a/src/runtime/write_err_ios_arm64.s b/src/runtime/write_err_ios_arm64.s
new file mode 100644
index 0000000..adcd844
--- /dev/null
+++ b/src/runtime/write_err_ios_arm64.s
@@ -0,0 +1,21 @@
+// +build ios,arm64
+
+#include "go_asm.h"
+#include "go_tls.h"
+#include "funcdata.h"
+#include "textflag.h"
+
+TEXT runtime·asmAslLogWrap(SB),NOSPLIT,$-8
+    MOVD 8(RSP),R0 // 第一个参数
+    SUB $32,RSP
+    MOVD g,16(RSP)
+    MOVD R30,24(RSP)
+
+    BL x_cgo_asl_log_wrap(SB)
+
+    MOVD 24(RSP),R30
+    MOVD 16(RSP),g
+    ADD $32,RSP
+	RET
+
+
@eliasnaur

This comment has been minimized.

Contributor

eliasnaur commented May 5, 2017

Thank you very much. Changes to Go needs to go through a review process before they can be considered. If you submit a CL according to https://golang.org/doc/contribute.html I'll make sure it gets reviewed.

Also note that we've entered the freeze stage for Go 1.9. The earliest a change like this can go in is for Go 1.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment