[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Building for Android


Hi Aris,

Thank you for your interest, and for a great library.

I have good news: it seems to work now.
And the funny thing is, it seems to have something to do with logging.

Quick recap here, but feel free to ask for more informations:
The problem is that, unfortunately, my device is not rooted, and thus
access to logs and stacktraces is limited.
So, while trying to find an efficient way to log library events, I
stumbled upon the ssh_set_log_callback() function.
That seemed perfect in this case, as I can redirect the log messages to
screen and to a "private" log file that the app has access to.

So I quickly implemented it into the tester program and... to my
surprise, no crash anymore!
I could successfully establish a connection, as confirmed also by the
server logs.

Here the working code:
void callback(int priority, const char *function, const char *buffer,
void *userdata)
{
    //this function can be empty, does not make any difference
}

void MainWindow::test()
{
    ssh::Session session;
    session.setOption(SSH_OPTIONS_TIMEOUT, 20);
    session.setOption(SSH_OPTIONS_HOST, "192.168.0.13");
    session.setOption(SSH_OPTIONS_USER, "root");
    session.setOption(SSH_OPTIONS_PORT, 2222);

    //added these lines for logging
    session.setOption(SSH_OPTIONS_LOG_VERBOSITY, 2);
    ssh_set_log_level(SSH_LOG_FUNCTIONS);
    ssh_set_log_callback(callback); //commenting out this line causes
the crash

    session.connect();
}

My guess is that libSSH is logging to some facility that, on Android,
the app does not have access to.
Please find attached an excerpt from the Android logcat with a stacktrace.
The log starts with a "touch event" corresponding to pushing the button
connected to MainWindow::test(), and ends with the app crashing.
From the stacktrace, one can see that the last called function seems to
be _ssh_log().

Tomorrow I will try to slowly expand the test to a more complete use case.

Regards
Matteo

On 11/20/2015 07:34 PM, Aris Adamantiadis wrote:
> Hi Matteo,
>
> You did a great work compiling libssh for android. I have no idea what's
> causing the crash, it would greatly help if you could provide us with a
> stacktrace. Running libssh in verbose mode and capturing the output
> would also help figuring out what works .
>
> Aris
>
> On 20/11/15 18:46, Matteo wrote:
>> After several attempts, I could get a successful build.
>> However, when I test it, it segfaults.
>>
>> In the following, I will try to explain what I did.
>> I will try to be brief but, if there is any interest, I can provide
>> more detailed instructions (or even a script that automates the whole
>> process).
>>
>> 0) The build environment is as follows:
>> - Ubuntu 15.04 64 bit with 3.19.0-33 kernel
>> - cmake 3.0.2 and gcc 4.9.2 (both from repos)
>> - Android NDK r9d
>> - OpenSSL 1.0.2d
>> - libSSH 0.7.2
>> - Qt 5.4 and QtCreator 3.3.0
>> - Target platform arm-linux-androideabi-4.8 API 19
>> - Target device Samsung Galaxy S5 klte
>>
>> 1) First, we need to build the dependencies.
>> zlib is already part of the NDK, so we only need to cross-compile OpenSSL.
>> This is easily accomplished by following the instructions found here:
>> https://wiki.openssl.org/index.php/Android
>> After installation I symlinked libcrypto.so, libssl.so and the include
>> files inside the NDK "usr" folder (so that they can easily be linked
>> against).
>>
>> 2) Following Daniel Kroker's advice, I added the taka-no-me toolchain
>> into the libSSH folder.
>> Before I could complete the build, however, I had to solve following
>> problems.
>>
>> 3) My version of cmake has a bug that was fixed in 3.1 an later versions.
>> This prevents it to correctly detect OpenSSL 1.0.2 (it is reported to
>> work with earlier versions, though).
>> To fix it, I manually applied this patch:
>> https://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=c5d9a8283cfac15b4a5a07f18d5eb10c1f388505
>>
>> 4) The cmake configuration of libSSH contains a TRY_RUN() step.
>> As far as I could understand, this is supposed to run a dummy test to
>> check if a certain feature works correctly.
>> This is obviously not possible in cross-compilation mode: fortunately,
>> the error issued by cmake contains an explanation on how to fix it.
>> In the libSSH folder one needs to create a file (named e.g.
>> "TryRunResults.cmake") with following content:
>>         set( THREADS_PTHREAD_ARG 0
>>              CACHE STRING "Result from TRY_RUN" FORCE)
>>
>> 5) The build still fails with error "implicit declaration of function
>> 'getpwuid_r'"
>> This function is supposed to return informations about the current
>> user, like username and home directory.
>> This has no equivalent on android, so I needed to change these two
>> functions in the file "misc.c":
>>    char *ssh_get_user_home_dir(void)
>>    char *ssh_get_local_username(void)
>> For my tests, I resigned to return hard-coded values, but more work is
>> required for a general solution.
>>
>> 6) Now the build completes successfully. These are the command issued:
>> cd /path/to/downloaded/libSSH/
>> mkdir build
>> cd build
>>         cmake \
>>             -C ../TryRunResults.cmake \
>>             -DCMAKE_INSTALL_PREFIX=/opt/libssh-android \
>>             -DWITH_INTERNAL_DOC=OFF \
>>             -DWITH_STATIC_LIB=ON \
>>             -DWITH_TESTING=OFF \
>>             -DWITH_SERVER=OFF \
>>             -DWITH_EXAMPLES=OFF \
>>             -DCMAKE_BUILD_TYPE=Release \
>>             -DCMAKE_TOOLCHAIN_FILE=../android.toolchain.cmake \
>>             -DANDROID_NDK="$ANDROID_NDK_ROOT" \
>>             -DANDROID_NATIVE_API_LEVEL=android-19 \
>>             -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.8 \
>>             -DANDROID_ABI="armeabi-v7a with NEON" \
>>         ..
>>         cmake --build .
>>
>> 7) Before deployment, there are two more obstacles.
>> First, the Android NDK doesnt provide OpenSSL, but the library is
>> installed in Android and loaded by default (taking precedence over
>> libraries packaged in the APK).
>> This means that we have to rename the libcrypto.so and libssl.so to
>> something else, and repeat the linking step. However, see below.
>> (this problem is documented here:
>> https://mta.openssl.org/pipermail/openssl-users/2015-April/001183.html)
>> Second, Android does not support loading of versioned libraries
>> (meaning that e.g. libssh.so is a symlink to libssh.so.4.4.0).
>> This means that we have to configure OpenSSL and libSSH to produce
>> non-versioned files, and relink. However, see below.
>> (this problem is documented here:
>> https://bugreports.qt.io/browse/QTCREATORBUG-11062)
>> The link above also provided a solution (hack?) for both problems,
>> which I implemented with following BASH code:
>>     local subs=()
>>     subs+=(-e
>> 's/libcrypto.so.1.0.0/libcryptm.so\x00\x00\x00\x00\x00\x00/g')
>>     subs+=(-e 's/libcrypto.so/libcryptm.so/g')
>>     subs+=(-e 's/libssl.so.1.0.0/libssm.so\x00\x00\x00\x00\x00\x00/g')
>>     subs+=(-e 's/libssl.so/libssm.so/g')
>>     subs+=(-e 's/libssh.so.4.4.0/libssh.so\x00\x00\x00\x00\x00\x00/g')
>>     subs+=(-e 's/libssh.so.4/libssh.so\x00\x00/g')
>>     sed "${subs[@]}" "/opt/openssl/android-19/lib/libcrypto.so" >
>> "/home/matteo/deploy/libcryptm.so"
>>     sed "${subs[@]}" "/opt/openssl/android-19/lib/libssl.so" >
>> "/home/matteo/deploy/libssm.so"
>>     sed "${subs[@]}" "/opt/libssh-android/lib/libssh.so" >
>> "/home/matteo/deploy/libssh.so"
>>     chmod a+x /home/matteo/deploy/*
>>
>> 8) I prepared a very simple test project in Qt.
>> Starting from the widget-based template for android, I added a
>> function that, when clicking on a QPushButton, attempts to connect to
>> a ssh server in my LAN.
>> void MainWindow::test()
>> {
>>     ssh::Session session;
>>     session.setOption(SSH_OPTIONS_TIMEOUT, 20);
>>     session.setOption(SSH_OPTIONS_HOST, "192.168.0.13");
>>     session.setOption(SSH_OPTIONS_USER, "root");
>>     session.setOption(SSH_OPTIONS_PORT, 2222);
>>
>>     session.connect();
>> }
>>
>> This works if "session.connect();" is commented out, but the program
>> crashes otherwise, with error:
>> F/libc    (22871): Fatal signal 11 (SIGSEGV), code 1, fault addr
>> 0x10100 in tid 23039 (QtThread)
>> Note that the very same code works in "desktop mode".
>>
>> Right now I am out of ideas on how to debug this, so if someone can
>> give an hint, I would much appreciate it.
>> As I said above, I can provide more details and code upon request.
>>
>> Thanks in advance
>> Matteo
>>
>> On 11/19/2015 09:28 AM, Daniel Kroker wrote:
>>> Hi,
>>>
>>> its not so easy to build libssh for android. you need the android
>>> toolchain look at
>>> https://github.com/taka-no-me/android-cmake/blob/master/android.toolchain.cmake
>>>
>>> Am 19.11.2015 um 02:35 schrieb Matteo:
>>>> Hi all,
>>>>
>>>> My project depends on libssh and I am trying to port it to Android.
>>>>
>>>> After some research in the Internet, it appears that I have to
>>>> cross-compile libssh by using the Android NDK.
>>>> However, I could not find any good sources of information about the
>>>> topic.
>>>>
>>>> Could somebody please provide a starting point for me?
>>>>
>>>> Thank you in advance.
>>>> Mat
>>>>
>>>>
>

I/InputReader(  867): Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=] when=245200693686000
I/InputDispatcher(  867): Delivering touch to (28394): action: 0x1, toolType: 1
F/libc    (28394): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x10100 in tid 28585 (QtThread)
I/DEBUG   (25158): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG   (25158): Build fingerprint: 'samsung/kltedx/klte:5.0/LRX21T/G900FDXU1BOJ1:user/release-keys'
I/DEBUG   (25158): Revision: '14'
I/DEBUG   (25158): ABI: 'arm'
I/DEBUG   (25158): pid: 28394, tid: 28585, name: QtThread  >>> org.qtproject.example.tester <<<
I/DEBUG   (25158): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x10100
I/DEBUG   (25158):     r0 00000002  r1 b39c2358  r2 af4fe9bc  r3 00000000
I/DEBUG   (25158):     r4 b6fa2e04  r5 00000002  r6 b39c2358  r7 00010101
I/DEBUG   (25158):     r8 af4fe9bc  r9 af4ff060  sl af13faf0  fp af4fee14
I/DEBUG   (25158):     ip b39e0cf8  sp af4fe5b0  lr b39a3413  pc 00010100  cpsr 40010030
I/DEBUG   (25158): 
I/DEBUG   (25158): backtrace:
I/DEBUG   (25158):     #00 pc 00010100  <unknown>
I/DEBUG   (25158):     #01 pc 00014411  /data/app/org.qtproject.example.tester-1/lib/arm/libssh.so (_ssh_log+132)
I/DEBUG   (25158):     #02 pc 0000f441  /data/app/org.qtproject.example.tester-1/lib/arm/libssh.so (ssh_connect+124)
I/DEBUG   (25158):     #03 pc 00005284  /data/app/org.qtproject.example.tester-1/lib/arm/libtester.so (ssh::Session::connect()+28)
I/DEBUG   (25158):     #04 pc 00005c94  /data/app/org.qtproject.example.tester-1/lib/arm/libtester.so (MainWindow::write()+160)
I/DEBUG   (25158):     #05 pc 00006550  /data/app/org.qtproject.example.tester-1/lib/arm/libtester.so
I/DEBUG   (25158):     #06 pc 001cd0af  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Core.so (QMetaObject::activate(QObject*, int, int, void**)+1142)
I/DEBUG   (25158):     #07 pc 0026104f  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so (QAbstractButton::clicked(bool)+26)
I/DEBUG   (25158):     #08 pc 00145af1  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so
I/DEBUG   (25158):     #09 pc 001462ef  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so
I/DEBUG   (25158):     #10 pc 00146371  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so (QAbstractButton::mouseReleaseEvent(QMouseEvent*)+88)
I/DEBUG   (25158):     #11 pc 001007bd  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so (QWidget::event(QEvent*)+536)
I/DEBUG   (25158):     #12 pc 000e26a9  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so (QApplicationPrivate::notify_helper(QObject*, QEvent*)+104)
I/DEBUG   (25158):     #13 pc 000e51eb  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so (QApplication::notify(QObject*, QEvent*)+1382)
I/DEBUG   (25158):     #14 pc 001ae387  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Core.so (QCoreApplication::notifyInternal(QObject*, QEvent*)+62)
I/DEBUG   (25158):     #15 pc 000e49c3  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so (QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer<QWidget>&, bool)+286)
I/DEBUG   (25158):     #16 pc 00109c07  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so
I/DEBUG   (25158):     #17 pc 0010a33f  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so
I/DEBUG   (25158):     #18 pc 000e26a9  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so (QApplicationPrivate::notify_helper(QObject*, QEvent*)+104)
I/DEBUG   (25158):     #19 pc 000e5ac1  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Widgets.so (QApplication::notify(QObject*, QEvent*)+3644)
I/DEBUG   (25158):     #20 pc 001ae387  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Core.so (QCoreApplication::notifyInternal(QObject*, QEvent*)+62)
I/DEBUG   (25158):     #21 pc 000a06b3  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Gui.so (QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent*)+514)
I/DEBUG   (25158):     #22 pc 000a03d5  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Gui.so (QGuiApplicationPrivate::processTouchEvent(QWindowSystemInterfacePrivate::TouchEvent*)+3492)
I/DEBUG   (25158):     #23 pc 000a175b  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Gui.so (QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent*)+382)
I/DEBUG   (25158):     #24 pc 00092e81  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Gui.so (QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>)+20)
I/DEBUG   (25158):     #25 pc 00027f9d  /data/data/org.qtproject.example.tester/qt-reserved-files/plugins/platforms/android/libqtforandroid.so
I/DEBUG   (25158):     #26 pc 001ad4ab  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Core.so (QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)+14)
I/DEBUG   (25158):     #27 pc 001adb8b  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Core.so (QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>)+226)
I/DEBUG   (25158):     #28 pc 001b249b  /data/app/org.qtproject.example.tester-1/lib/arm/libQt5Core.so (QCoreApplication::exec()+82)
I/DEBUG   (25158):     #29 pc 00004258  /data/app/org.qtproject.example.tester-1/lib/arm/libtester.so (main+80)
I/DEBUG   (25158):     #30 pc 00019085  /data/data/org.qtproject.example.tester/qt-reserved-files/plugins/platforms/android/libqtforandroid.so
I/DEBUG   (25158):     #31 pc 000137bb  /system/lib/libc.so (__pthread_start(void*)+30)
I/DEBUG   (25158):     #32 pc 0001189b  /system/lib/libc.so (__start_thread+6)
I/DEBUG   (25158): 
I/DEBUG   (25158): Tombstone written to: /data/tombstones/tombstone_09
E/        (25158): ro.product_ship = true
E/        (25158): ro.debug_level = 0x4f4c
E/audit   ( 1259): type=1701 msg=audit(1448070439.573:45607): auid=4294967295 uid=10293 gid=10293 ses=4294967295 subj=u:r:untrusted_app:s0 pid=28585 comm="QtThread" reason="memory violation" sig=11
I/BootReceiver(  867): Copying /data/tombstones/tombstone_09 to DropBox (SYSTEM_TOMBSTONE)
W/ActivityManager(  867):   Force finishing activity org.qtproject.example.tester/org.qtproject.qt5.android.bindings.QtActivity
D/FocusedStackFrame(  867): Set to : 0
I/WindowState(  867): WIN DEATH: Window{37dd2e88 u0 org.qtproject.example.tester/org.qtproject.qt5.android.bindings.QtActivity}
E/android.os.Debug(  867): ro.product_ship = true
E/android.os.Debug(  867): ro.debug_level = 0x4f4c
I/WindowState(  867): WIN DEATH: Window{29659e4c u0 SurfaceView}
I/SQLiteSecureOpenHelper(16545): getWritableDatabase(pwd)
I/SQLiteSecureOpenHelper(16545): getDatabaseLocked(b,b,pwd)...
D/StatusBarManagerService(  867): manageDisableList userId=0 what=0x0 pkg=WindowManager.LayoutParams
I/ActivityManager(  867): Process org.qtproject.example.tester (pid 28394)(adj 0) has died(70,372)
I/Zygote  (  320): Process 28394 exited due to signal (11)


Follow-Ups:
Re: Building for AndroidAris Adamantiadis <aris@xxxxxxxxxxxx>
References:
Building for AndroidMatteo <matpen@xxxxxxx>
Re: Building for AndroidDaniel Kroker <dk@xxxxxxxxx>
Re: Building for AndroidMatteo <matpen@xxxxxxx>
Re: Building for AndroidAris Adamantiadis <aris@xxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org