View Single Post
Old 02-07-2024, 03:41 PM   #13
elinkser
Addict
elinkser has survived committing the World's Second Greatest Blunder.elinkser has survived committing the World's Second Greatest Blunder.elinkser has survived committing the World's Second Greatest Blunder.elinkser has survived committing the World's Second Greatest Blunder.elinkser has survived committing the World's Second Greatest Blunder.elinkser has survived committing the World's Second Greatest Blunder.elinkser has survived committing the World's Second Greatest Blunder.elinkser has survived committing the World's Second Greatest Blunder.elinkser has survived committing the World's Second Greatest Blunder.elinkser has survived committing the World's Second Greatest Blunder.elinkser has survived committing the World's Second Greatest Blunder.
 
Posts: 242
Karma: 146236
Join Date: Oct 2022
Device: Kobo Clara HD
DBSERVER AND VALGRIND (USE AT OWN RISK - SEE WARNING)

***

GDBSERVER
(This is mainly for minimalists - you can get full gdb from NiLuJe kobostuff developer suite.)


The GNU Debugger - gdbserver

The gdb app has only the following dependencies:

Depends (9)
gmp
libexpat
libgcc
libstdc++**
musl*
ncurses-libs
python3
readline
zlib

*already got it from elinks/nano install in Post #3
**gdbserver has this additional dependency


RUN THESE COMMANDS FROM LINUX DESKTOP:

DOWNLOAD THE REQUIRED PACKAGES:

$ cd myalpine/


$ wget https://dl-cdn.alpinelinux.org/alpin...db-12.1-r2.apk

$ wget https://dl-cdn.alpinelinux.org/alpin...0220924-r4.apk

$ tar zxvf gdb-12.1-r2.apk

$ mv usr/bin/gdbserver scripts/

$ tar zxvf libstdc++-12.2.1_git20220924-r4.apk

$ mv usr/lib/libstdc++.so.6.0.30 libs/libstdc++.so.6


On your PC recompile fbpad2 with -g option:

$ cd fbpad-eink-master/

$ source ~/koxtoolchain/refs/x-compile.sh kobo env bare

$ nano -l Makefile
Code:
  1 CFLAGS += -Wall -O2  -g
$ make

$ file fbpad2
fbpad2: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.33, with debug_info, not stripped


NOW CONNECT YOUR KOBO TO YOUR PC:

Copy your gdbserver binary from the scripts/ folder on the PC to the /mnt/onboard/.adds/koreader/scripts/ folder on the kobo.

Copy fbpad2 to /mnt/onboard/.adds/koreader/scripts/ folder on your Kobo.

***

***Backup the existing libstdc++.so.6 in the
/mnt/onboard/.adds/koreader/libs/ folder on the kobo.***

Copy libstdc++.so.6 from the libs/ folder on the PC to the /mnt/onboard/.adds/koreader/libs/ folder on the kobo.

Make sure KOReader can still run.
If not, restore the original libstdc++.so.6 to the /mnt/onboard/.adds/koreader/libs/ folder.

***

$ sudo apt install gdb-multiarch

$ sudo ufw allow proto tcp from 192.168.2.1 to 192.168.2.2 port 2345

From SSH shell run (after first running ". /korenv.sh" as was done in Post #4):

# gdbserver host:2345 fbpad2 -p 58 0</dev/tty1 &
# Process fbpad2 created; pid = 1532
Listening on port 2345


From host desktop run:

$ gdb-multiarch fbpad2
GNU gdb (Debian 8.2.1-2+b3) 8.2.1
...
Reading symbols from fbpad2...done.
(gdb)

Connect to gdbserver:

(gdb) target remote 192.168.2.2:2345
Remote debugging using 192.168.2.2:2345
...
Reading symbols from target:/lib/ld-linux-armhf.so.3...(no debugging symbols found)...done.
0x76fcfb00 in ?? () from target:/lib/ld-linux-armhf.so.3

(gdbserver response: Remote debugging from host 192.168.2.1, port 51944)

(gdb) b main
Breakpoint 1 at 0x10de1

(gdb) continue
Continuing.
Reading /lib/libarmmem-v7l.so from remote target...
Reading /lib/libutil.so.1 from remote target...
Reading /lib/libc.so.6 from remote target...
Breakpoint 1, 0x00010dec in main ()


From a second SSH shell to Kobo run:

# fbkeyboard2

Enter ALT-c and you get the shell prompt on the Kobo display.
(may have to issue "continue" command again from gdb.)

Enter ALT-p <Ret> to see tag list.

CTRL-ALT-q to end the fbpad session.

CTRL-ALT-x to end the fbkeyboard session.



***

Do NOT attempt the following on your Kobo.
Having valgrind is NOT worth the risk of having to factory wipe your device.

I had a replaceable sdcard-based system with multiple backups, previous proof that the substitute build environment was compatible, and an SFTP-capable filemanager running on my desktop with an already loaded SSH server in Kobo memory.

Spoiler:



*** WARNING - DO NOT ATTEMPT THIS ***

Valgrind is a tool to help find memory-management problems in programs


The valgrind app has only the following dependencies:

Depends (1)
musl*

*already got it from elinks/nano install in Post #3.


RUN THESE COMMANDS FROM LINUX DESKTOP:

DOWNLOAD THE REQUIRED PACKAGES:

$ cd myalpine/

$ wget https://dl-cdn.alpinelinux.org/alpin...-3.20.0-r0.apk

$ wget https://dl-cdn.alpinelinux.org/alpin...g-1.2.3-r5.apk


$ tar zxvf valgrind-3.20.0-r0.apk

$ mv usr/bin/valgrind scripts/

$ mkdir libexec

$ mkdir libexec/valgrind

$ mv usr/libexec/valgrind/default.supp libexec/valgrind/

$ mv usr/libexec/valgrind/memcheck-arm-linux libexec/valgrind/

$ mv usr/libexec/valgrind/vgpreload_core-arm-linux.so libexec/valgrind/


Get alternate dynamic linker/loader from Linaro toolchain.

$ cp ../gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib/ld-2.19-2014.08-1-git.so .


NOW CONNECT YOUR KOBO TO YOUR PC:

Copy your valgrind binary from the scripts/ folder on the PC to the /mnt/onboard/.adds/koreader/scripts/ folder on the kobo.

Create a /usr/libexec folder on the Kobo.

Copy valgrind/ folder from libexec on the PC to the /mnt/onboard/.adds/ folder on the kobo.

# ln -s /mnt/onboard/.adds/valgrind/ /usr/libexec/valgrind

Copy ld-2.19-2014.08-1-git.so to root directory / of Kobo.



# valgrind /mnt/onboard/.adds/koreader/scripts/nano
(works)


# valgrind /bin/ls
Fatal error at startup:
A must-be-redirected function valgrind:
whose name matches the pattern: index
in an object with soname matching: ld-linux-armhf.so.3
was not found whilst processing

See source of problem:
https://sourceforge.net/p/valgrind/m...m/#msg30887009


# cd /lib

# ls -l ld*
-rwxr-xr-x 134412 ld-2.11.1.so
lrwxrwxrwx 13 ld-linux-armhf.so.3 -> ld-linux.so.3
lrwxrwxrwx 12 ld-linux.so.3 -> ld-2.11.1.so
-rwxr-xr-x 525980 ld-musl-armhf.so.1

Check if the stock ld-linux.so.3 includes the memcpy function:

# readelf --symbols /lib/ld-linux.so.3 | grep mem
12: 000176bc 332 FUNC WEAK DEFAULT 10 __libc_memalign@@GLIBC_2.4


Check if the alternate /ld-2.19-2014.08-1-git.so includes the memcpy function:

# readelf --symbols /ld-2.19-2014.08-1-git.so | grep memcpy
804: 00000000 0 FILE LOCAL DEFAULT ABS rtld-memcpy.os
907: 00012700 1056 FUNC LOCAL DEFAULT 11 memcpy
1097: 000132e9 16 FUNC LOCAL DEFAULT 11 __memcpy_from_thumb



**********************************************
***CRITICAL STEP***
* MAKE SURE YOU HAVE SSH CONNECTION TO KOBO ALREADY LOADED IN MEMORY *
* AND AN SFTP-CAPABLE FILEMANAGER RUNNING ON DESKTOP ABLE TO RESTORE DYNAMIC LINKER/LOADER FILE REMOTELY*
* AND AN SDCARD-BASED FILESYSTEM ON KOBO, BACKED UP
* YOU WILL BE REPLACING LINKER WHICH MAY CAUSE COMMANDS TO STOP WORKING ON KOBO *
* YOU WILL BE REPLACING LINKER WHICH MAY CAUSE COMMANDS TO STOP WORKING ON KOBO *
* YOU WILL BE REPLACING LINKER WHICH MAY CAUSE COMMANDS TO STOP WORKING ON KOBO *

Back up stock dynamic linker/loader:

# cp -P ld-linux-armhf.so.3 /

Check that it is backed up:

# ls -l /ld-linux-armhf.so.3
lrwxrwxrwx 13 /ld-linux-armhf.so.3 -> ld-linux.so.3

Copy alternate linker over the stock linker:

# cp /ld-2.19-2014.08-1-git.so ld-linux-armhf.so.3

Check if commands still work:

# pwd
/lib

* IF COMMANDS DON'T WORK, RESTORE /lib/ld-linux-armhf.so.3 FROM YOUR BACKUP USING YOUR SSH CONNECTION *

**********************************************

Now let's retry valgrind with the new linker:

# valgrind /bin/ls
(works)


On your PC recompile fbpad2 with -g option:

$ cd fbpad-eink-master/

$ source ~/koxtoolchain/refs/x-compile.sh kobo env bare

$ nano -l Makefile
Code:
  1 CFLAGS += -Wall -O2  -g
$ make

Copy fbpad2 to /mnt/onboard/.adds/koreader/scripts/ folder on your Kobo.

From SSH shell run (after first running ". /korenv.sh" as was done in Post #4):

# valgrind fbpad2 -p 58 0</dev/tty1 &
<Ret>
# fbkeyboard2

Enter ALT-c and you get the shell prompt on the Kobo display.

Enter ALT-p <Ret> to see tag list.

CTRL-ALT-q to end the fbpad session.

CTRL-ALT-x to end the fbkeyboard session.

Output:
==2059== Memcheck, a memory error detector
==2059== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==2059== Using Valgrind-3.20.0 and LibVEX; rerun with -h for copyright info
==2059== Command: fbpad2 -p 58
...
==2059==
==2059== All heap blocks were freed -- no leaks are possible
==2059==
==2059== Use --track-origins=yes to see where uninitialised values come from
==2059== For lists of detected and suppressed errors, rerun with: -s
==2059== ERROR SUMMARY: 4 errors from 3 contexts (suppressed: 0 from 0)
[1]+ Segmentation fault valgrind fbpad2 -p 58 0</dev/tty1


**********************************************
***CRITICAL STEP***

* RESTORE /lib/ld-linux-armhf.so.3 FROM YOUR BACKUP USING YOUR SSH CONNECTION *

Check if commands still work:

# pwd
/lib

**********************************************

***
Attached Files
File Type: zip fbpad2-debug.zip (118.5 KB, 426 views)

Last edited by elinkser; 02-08-2024 at 03:31 AM. Reason: warning
elinkser is offline   Reply With Quote