nohajc / termux-adb

Run adb in Termux without root permissions!
MIT License
359 stars 36 forks source link

Help #1

Closed RohitVerma882 closed 1 year ago

RohitVerma882 commented 1 year ago

Screenshot_2022-11-06-07-07-28-02_84d3000e3f4017145260f7618db1d683 Not working in Android 12!

nohajc commented 1 year ago

Could you re-run with RUST_LOG=debug ./termux-adb and attach logs from $TMPDIR? There should be adb.*.log and also termux-adb.*.log.

Anyway, that line about "pointer tag" is really peculiar. Nothing like that on my Pixel 3a (with Android 12 too).

What kind of a device do you have actually?

RohitVerma882 commented 1 year ago

Realme 7 - RMX2151

nohajc commented 1 year ago

Ok, if I can take a look at the logs, maybe I'll see where the problem is. Judging from the screenshot, adb server doesn't crash immediately but it doesn't become ready either.

nohajc commented 1 year ago

Could be related to https://source.android.com/docs/security/test/tagged-pointers

RohitVerma882 commented 1 year ago

Ok, if I can take a look at the logs, maybe I'll see where the problem is. Judging from the screenshot, adb server doesn't crash immediately but it doesn't become ready either.

using /dev/bus/usb/001/024: fd = 7
cannot connect to daemon at tcp:5037: Connection refused
DEBUG: dlsym works!
[2022-11-06T13:35:21Z INFO  adbhooks] listening on socket fd 4
* daemon not running; starting now at tcp:5037
DEBUG: dlsym works!
[2022-11-06T13:35:21Z DEBUG adbhooks] reading TERMUX_USB_FD
[2022-11-06T13:35:21Z DEBUG adbhooks] calling libusb_set_option
[2022-11-06T13:35:21Z DEBUG adbhooks] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2022-11-06T13:35:21Z DEBUG adbhooks] called readdir with virtual DirStream
[2022-11-06T13:35:21Z DEBUG adbhooks] readdir returned dirent with d_name=001
[2022-11-06T13:35:21Z DEBUG adbhooks] opening device from 7
[2022-11-06T13:35:21Z DEBUG adbhooks] getting device from handle
[2022-11-06T13:35:21Z DEBUG adbhooks] requesting device descriptor
[2022-11-06T13:35:21Z DEBUG adbhooks] device descriptor: vid=6353, pid=20199, iSerial=3
[2022-11-06T13:35:21Z INFO  adbhooks] device serial path: /sys/bus/usb/devices/1-1/serial
Pointer tag for 0x7741e15000 was truncated, see 'https://source.android.com/devices/tech/debug/tagged-pointers'.
ADB server didn't ACK
Full server startup log: /data/data/com.termux/files/usr/tmp/adb.10245.log
Server had pid: 29169
[2022-11-06T13:35:21Z DEBUG adbhooks] called open with pathname=/data/data/com.termux/files/usr/tmp/adb.10245.log flags=0
[2022-11-06T13:35:21Z DEBUG adbhooks] open returned fd with value -1
* failed to start daemon
error: cannot connect to daemon
adb start-server exited with error status
nohajc commented 1 year ago

Thanks. Was there a second log file or termux-adb.*.log only? I expect there should also be the log generated by adb itself. This is just what the launcher logged.

Anyway, I have a theory what could be wrong here but I'm unable to reproduce the issue on any of my devices.

If you'd be willing to test some custom builds for me, that would be really helpful. I'll prepare a change that I think could fix the problem.

RohitVerma882 commented 1 year ago

Okay sir 🙂,

What shall we do now?

nohajc commented 1 year ago

Well, if the file /data/data/com.termux/files/usr/tmp/adb.10245.log (or $TMPDIR/adb.10245.log) exists, it would help me if you shared it too. I think you only gave me termux-adb.10245.log.

I'll get back to you when I have an update. I can try a few things.

RohitVerma882 commented 1 year ago

Screenshot_2022-11-06-20-46-28-03_84d3000e3f4017145260f7618db1d683 Adb log file is not exist

nohajc commented 1 year ago

Ok then, no worries. I'll try to work with what I have.

nohajc commented 1 year ago

I'd like to collect a little more information. Can you please download the new release (https://github.com/nohajc/termux-adb/releases/tag/v0.1.1), run RUST_LOG=debug ./termux-adb again and post the termux-adb log here?

It will fail but also log some pointer addresses which should help me diagnose the exact cause of the issue.

Also, if you shared output of adb version, that would be great.

RohitVerma882 commented 1 year ago

adb logs

--- adb starting (pid 29662) ---
adb I 11-07 07:30:10 29662 29662 main.cpp:63] Android Debug Bridge version 1.0.41
adb I 11-07 07:30:10 29662 29662 main.cpp:63] Version 31.0.3p1-android-tools
adb I 11-07 07:30:10 29662 29662 main.cpp:63] Installed as /data/data/com.termux/files/usr/bin/adb
adb I 11-07 07:30:10 29662 29662 main.cpp:63] 
adb I 11-07 07:30:10 29662 29662 auth.cpp:417] adb_auth_init...
adb I 11-07 07:30:10 29662 29662 auth.cpp:152] loaded new key from '/data/data/com.termux/files/home/.android/adbkey' with fingerprint 5B17F393BDE89465D0EE415317447A34BA4B246B1F7CE5B7A0320DCB04FBF348
adb I 11-07 07:30:10 29662 29662 auth.cpp:392] adb_auth_inotify_init...

termux-adb logs

using /dev/bus/usb/001/003: fd = 7
cannot connect to daemon at tcp:5037: Connection refused
DEBUG: dlsym works!
[2022-11-07T02:10:07Z INFO  adbhooks] listening on socket fd 4
* daemon not running; starting now at tcp:5037
DEBUG: dlsym works!
[2022-11-07T02:10:07Z DEBUG adbhooks] reading TERMUX_USB_FD
[2022-11-07T02:10:07Z DEBUG adbhooks] calling libusb_set_option
[2022-11-07T02:10:07Z DEBUG adbhooks] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2022-11-07T02:10:07Z DEBUG adbhooks] called readdir with dirp 0xb400007345e4c000
[2022-11-07T02:10:07Z DEBUG adbhooks] readdir: dirp is virtual DirStream
[2022-11-07T02:10:07Z DEBUG adbhooks] readdir returned dirent 0xb400007345e4c008 with d_name=001
[2022-11-07T02:10:07Z DEBUG adbhooks] called closedir with dirp 0xb400007345e4c000
[2022-11-07T02:10:07Z DEBUG adbhooks] closedir: dirp is virtual DirStream
[2022-11-07T02:10:07Z DEBUG adbhooks] opening device from 7
[2022-11-07T02:10:07Z DEBUG adbhooks] getting device from handle
[2022-11-07T02:10:07Z DEBUG adbhooks] requesting device descriptor
[2022-11-07T02:10:07Z DEBUG adbhooks] device descriptor: vid=6353, pid=20199, iSerial=3
[2022-11-07T02:10:07Z INFO  adbhooks] device serial path: /sys/bus/usb/devices/1-1/serial
Pointer tag for 0x7345e15000 was truncated, see 'https://source.android.com/devices/tech/debug/tagged-pointers'.
ADB server didn't ACK
Full server startup log: /data/data/com.termux/files/usr/tmp/adb.10245.log
Server had pid: 4516
[2022-11-07T02:10:07Z DEBUG adbhooks] called open with pathname=/data/data/com.termux/files/usr/tmp/adb.10245.log flags=0
[2022-11-07T02:10:07Z DEBUG adbhooks] open returned fd with value 5
* failed to start daemon
error: cannot connect to daemon
adb start-server exited with error status
RohitVerma882 commented 1 year ago

Screenshot_2022-11-07-07-45-28-90_84d3000e3f4017145260f7618db1d683

nohajc commented 1 year ago

Ok, I'm still not sure what exactly is happening. Can you try running RUST_LOG=debug ./termux-adb without any connected USB device, see if it starts and then connect the cable?

I'd like to know how the logs look like in that case.

RohitVerma882 commented 1 year ago

Not working Screenshot_2022-11-07-17-36-57-47_84d3000e3f4017145260f7618db1d683 Screenshot_2022-11-07-17-38-41-12_84d3000e3f4017145260f7618db1d683

nohajc commented 1 year ago

But it can still see some USB device at the start. I need you to disconnect everything and just run RUST_LOG=debug ./termux-adb. Then share the log files again please.

You should see that it prints "no device connected yet" to standard output.

In case it starts, then you can try to connect the device.

There is some initialization code which only runs if there's already something connected. I'm trying to pinpoint if the problem is in the initialization or something else...

RohitVerma882 commented 1 year ago

Screenshot_2022-11-07-18-36-26-51_84d3000e3f4017145260f7618db1d683 It works but take some times to show device open dialog

RohitVerma882 commented 1 year ago

Logs files: https://drive.google.com/file/d/1hbRbCgtim2QTYa-BKh6UfqqKK0uEqDIZ/view?usp=drivesdk

nohajc commented 1 year ago

All right, this is interesting! Must be some weird timing issue if it doesn't manifest in this case.

Yeah, it can take a couple seconds for the dialogue to appear. This is expected because I don't get any event when a device is connected. I have to poll every couple of seconds and I don't want to waste resources by having the period too short.

nohajc commented 1 year ago

Anyway, now I can hopefully figure out a workaround so you can have the device connected beforehand.

RohitVerma882 commented 1 year ago

I have one question! Can i ask?

nohajc commented 1 year ago

Of course, ask anything. You also tried to make this work before, right? By patching adb.

RohitVerma882 commented 1 year ago

I want to make an Android application that contains static adb binary, how to execute using ProcessBuilder class with usb support on non-rooted device?

RohitVerma882 commented 1 year ago

Of course, ask anything. You also tried to make this work before, right? By patching adb.

Yes

RohitVerma882 commented 1 year ago

I want to make an Android application that contains static adb binary, how to execute using ProcessBuilder class with usb support on non-rooted device?

If you don't want to reply to this then ignore it, sorry

nohajc commented 1 year ago

Sorry, but I don't know much about the Java/Android SDK part. I'm more of a linux person. You'd probably need to check how termux-usb does its magic. I now it uses some Android API to get the file descriptors for USB devices (after opening the authorization dialogue). So your application would presumably do the same and then feed the descriptor into adb.

If you're bundling your own copy of adb, it might be simpler to just patch it. But the problem I see is the background execution. Android has some strict rules about that. Termux somehow emulates the unix way but I'm not sure how all that would work in your custom APK.

Either way, if you don't want to reimplement the adb protocol, adb binary needs to run in the background and receive USB file descriptors as devices connect and disconnect. I use unix domain sockets for that. Normally only child processes inherit open file descriptors but if you have a socket connection between two arbitrary processes, you can use it to transfer descriptors from one to the other.

nohajc commented 1 year ago

If you don't want to reply to this then ignore it, sorry

It's all right, I was just slow.

RohitVerma882 commented 1 year ago

Sorry, but I don't know much about the Java/Android SDK part. I'm more of a linux person. You'd probably need to check how termux-usb does its magic. I now it uses some Android API to get the file descriptors for USB devices (after opening to authorization dialogue). So your application would presumably do the same and then feed the descriptor into adb.

If you're bundling your own copy of adb, it might be simpler to just patch it. But the problem I see is the background execution. Android has some strict rules about that. Termux somehow emulates the unix way but I'm not sure how all that would work in your custom APK.

Either way, if you don't want to reimplement the adb protocol, adb binary needs to run in the background and receive USB file descriptors as devices connect and disconnect. I use unix domain sockets for that. Normally only child processes inherit open file descriptors but if you have a socket connection between two arbitrary processes, you can use it to transfer descriptors from one to the other.

Okey sir, thanks

nohajc commented 1 year ago

Can you try the latest release and let me know if it fixes the original issue?

First connect the device, then start termux-adb. I removed special handling when the device is available before adb server is started, so now it should behave the same as if you connected the device later. Hopefully.

RohitVerma882 commented 1 year ago

Can you try the latest release and let me know if it fixes the original issue?

First connect the device, then start termux-adb. I removed special handling when the device is available before adb server is started, so now it should behave the same as if you connected the device later. Hopefully.

Screenshot_2022-11-08-09-09-18-01_84d3000e3f4017145260f7618db1d683 It works

nohajc commented 1 year ago

That's great! Thanks again for cooperation. If there isn't anything else, I'm closing the issue.

RohitVerma882 commented 1 year ago

How did the Bugjeager application work and how did it get modified?

nohajc commented 1 year ago

I don't know anything about that app... Does it do the same thing as adb in Termux?

gnostic-apollo commented 1 year ago

I don't know anything about that app... Does it do the same thing as adb in Termux?

adb in termux doesn't connect to another Android device via USB OTG(until your project came, thankyou very much for that), But that app (Bugjaeger) can connect to another Android device via USB OTG and then can perform adb commands.

I also wonder why can't android-tools (package of termux) do what that app does? [Performing adb commands over USB OTG.]

nohajc commented 1 year ago

I guess the difference is android-tools ships basically unmodified adb for linux (which only works on rooted devices) whereas Bugjaeger is a custom app re-implementing parts of the adb USB protocol specifically for Android.

Fastboot from android-tools is actually already patched to work with termux-usb but it's not obvious (or even documented AFAIK) how to use it. Adb would be more tricky to patch (though of course not impossible). I chose the dll injection approach exactly because it lets me use the unmodified adb binary.

RohitVerma882 commented 1 year ago

Sir, can you write one for me 🙃; like this for my app but without termux-usb