Biblioteca

How to collect dumps for Firebird on Linux and generate stack traces

1. Enable core dumps:
   First, ensure that your system is configured to generate core dumps:

   ```
   sudo sysctl -w kernel.core_pattern=/var/crash/core-%e-%s-%u-%g-%p-%t
   echo '* soft core unlimited' | sudo tee -a /etc/security/limits.conf
   ```

   This sets the core dump pattern and removes the core file size limit.

2. Install necessary tools:
   ```
   sudo apt update
   sudo apt install gdb apport
   ```

3. Configure Apport (if not using core dumps directly):
   Edit /etc/apport/crashdb.conf to enable crash reporting:
   ```
   sudo nano /etc/apport/crashdb.conf
   ```
   Ensure 'problem_types': ['crash'] is uncommented.

4. Collect crash dumps:
   - For Apport: Crashes will be automatically collected in /var/crash
   - For core dumps: They will be in /var/crash as per the pattern we set

5. Analyze with GDB:
   For Apport crash reports:
   ```
   apport-unpack /var/crash/your_crash_report.crash /tmp/crash_unpacked
   zcat /tmp/crash_unpacked/CoreDump > /tmp/core_decompressed
   gdb $(cat /tmp/crash_unpacked/ExecutablePath) /tmp/core_decompressed
   ```

   For direct core dumps:
   ```
   gdb /path/to/executable /var/crash/core-file
   ```

6. Basic GDB commands for analysis:
   Once in GDB:
   - `backtrace` or `bt`: Show the call stack
   - `frame` or `f`: Move between stack frames
   - `print` or `p`: Print variable values
   - `disassemble` or `disas`: Show disassembled code

7. Automating analysis:
   Create a script (e.g., analyze_crash.sh):

   ```bash
   #!/bin/bash

   if [ "$#" -ne 1 ]; then
       echo "Usage: $0 /path/to/crash_report.crash"
       exit 1
   fi

   CRASH_REPORT=$1
   UNPACK_DIR=$(mktemp -d)

   apport-unpack "$CRASH_REPORT" "$UNPACK_DIR"
   zcat "$UNPACK_DIR/CoreDump" > "$UNPACK_DIR/core_decompressed"
   EXECUTABLE=$(cat "$UNPACK_DIR/ExecutablePath")

   gdb -ex "backtrace full" -ex "info registers" -ex "thread apply all bt" -ex "quit" --batch $EXECUTABLE "$UNPACK_DIR/core_decompressed" > "$UNPACK_DIR/analysis.txt"

   echo "Analysis complete. Results in $UNPACK_DIR/analysis.txt"
   ```

   Make it executable: `chmod +x analyze_crash.sh`

8. Organize crash dumps:
   - Keep crashes in /var/crash
   - Move analyzed crashes to a separate directory, e.g., /var/crash_analyzed
   - Use a naming convention, e.g., YYYYMMDD_programname_crash.report

9. Regular cleanup:
   Set up a cron job to remove old crash dumps:

   ```
   0 2 * * * find /var/crash -name "core-*" -mtime +30 -delete
   ```

   This deletes core dumps older than 30 days at 2 AM daily.

10. Symbolication:
    Ensure debug symbols are available for accurate stack traces:
    ```
    sudo apt install libc6-dbg linux-image-$(uname -r)-dbgsym
    ```

11. For recurrent issues, consider using tools like `valgrind` or `AddressSanitizer` during development to catch memory errors before they cause crashes.

Would you like me to elaborate on any part of this process or provide more details on GDB usage for crash analysis?