Files
u-boot/examples/ulib
Simon Glass d5e3d066c7 backtrace: sandbox: Add support for obtaining symbols
Add backtrace functions for sandbox, including:

- os_backtrace() to collect addresses into a caller-supplied buffer
- os_backtrace_symbols() to convert addresses to symbol strings
- os_backtrace_symbols_free() to free the symbol array

The libbacktrace library (bundled with GCC) reads DWARF debug information
to provide detailed symbol resolution including function names (even for
static functions), source file paths, and line numbers.

The sandbox backtrace implementation wraps these OS functions to implement
the generic backtrace API (backtrace_init, backtrace_get_syms, etc.).

Enable it for just the 'sandbox' board. Add the library for the Rust
example too.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-12-01 15:42:04 +00:00
..
2025-09-10 03:37:08 -06:00
2025-09-10 03:37:08 -06:00
2025-09-10 03:37:08 -06:00
2025-09-11 15:19:22 -06:00
2025-09-10 03:37:08 -06:00
2025-09-10 03:37:08 -06:00

U-Boot Library Example
======================

This directory contains example programs showing how to use the U-Boot library
(libu-boot.so) in external C programs.

Building U-Boot Library
-----------------------

First, build U-Boot with library support:

    make O=/tmp/b/sandbox -j$(nproc) sandbox_defconfig all

This creates:
- /tmp/b/sandbox/libu-boot.so (shared library)
- /tmp/b/sandbox/libu-boot.a (static library)

Example Programs
----------------

The examples are built automatically as part of the U-Boot build. Each program
demonstrates different aspects of the U-Boot library.

**demo.c** - Demonstrates using U-Boot library functions

- Shows how to init the library with ulib_init()
- Uses U-Boot's OS functions (os_open(), os_fgets(), os_close())
- Reads and displays system information
- Shows the U-Boot version

**boot.c** - Demonstrates booting an OS using U-Boot bootflow

- Shows bootflow scanning and booting functionality
- Uses system headers for main program logic (boot.c)
- Uses U-Boot headers for bootflow internals (bootflow.c)
- Demonstrates attaching disk images and scanning for bootable OS
- Attempts to boot the first discovered bootflow

Building Examples
-----------------

The examples are built automatically when U-Boot has these options enabled::

    CONFIG_ULIB=y
    CONFIG_EXAMPLES=y

To build manually:

    # From this directory examples/ulib
    make UBOOT_BUILD=/tmp/b/sandbox/ srctree=../..

Running Examples
----------------

    # Run the demo
    LD_LIBRARY_PATH=/tmp/b/sandbox ./demo

    # Run the demo (static version)
    ./demo_static

    # Run the boot example (requires disk image at /home/sglass/u/mmc1.img)
    LD_LIBRARY_PATH=/tmp/b/sandbox ./boot

    # Run the boot example (static version)
    ./boot_static

Key Points
----------

- Files are compiled with U-Boot headers by default, except those listed in
  sys-objs which use system headers
- Use ulib_init() to init the library
- Use ulib_uninit() to clean up
- Set LD_LIBRARY_PATH when running dynamically linked programs
- Each program automatically gets both dynamic and static versions built

Copying for External Use
-------------------------

This directory can be copied elsewhere and used independently:

    cp -r examples/ulib ~/my-project/
    cd ~/my-project/ulib
    make UBOOT_BUILD=/path/to/u-boot-build  srctree=/path/to/u-boot-source

License
-------

All examples are licensed under GPL-2.0+