惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Help Net Security
Help Net Security
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
O
OpenAI News
美团技术团队
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
Cyberwarzone
Cyberwarzone
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google Online Security Blog
Google Online Security Blog
T
Tenable Blog
S
Security Affairs
博客园_首页
S
Schneier on Security
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
Spread Privacy
Spread Privacy
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
Vercel News
Vercel News
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
B
Blog
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 最新话题
D
DataBreaches.Net
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - Franky
W
WeLiveSecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Check Point Blog
H
Hacker News: Front Page

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker Family Ties in Your DNA: Some relatives are closer than others Doctors per capita Two days in Tallinn, Estonia Ship tools as standalone static binaries Made in America Two days in Helsinki, Finland Maintaining an Android app is a lot of work The land of good deals Two days in Oslo, Norway FastAPI vs Flask performance comparison Google Search is losing to Perplexity Two days in Dublin, Ireland Continuous integration ≠ Continuous delivery World's simplest project success heuristic London in 5 days It is hard to recommend Python in production Inflation, IRS, Credit cards, and Vendors Temu and the Chinese approach Things to do in Miami Florida Revenue vs Cost Axis Language learning as an adult The unanchored babies of the green card limbo Price variance in the United States A day in Louisville, Kentucky A surprisingly positive experience with Air India Unhospitable Airports Android: Don't use stale views USA = Union of Sales and Advertisement A day in Nashville, Tennessee Minimize Javascript in your codebase A day in Birmingham, Alabama In defense of ad-supported products Real vs artificial world The science behind Punjabi singers Hiking Mt. Fuji The Indian startup bubble is insane Repairing database on the fly for millions of users Book Summary: One up on Wall Street by Peter Lynch It is hard to recommend Google Cloud At the Prague airport Kyoto in three days Migrating from WordPress to Hugo Book summary: Sick Societies by Robert B. Edgerton Statistical outcomes require statistical games Illegal immigrants to Europe via Cairo Tokyo in three days Mobs are Status Games Writing Script matters as much as the spoken language Sri Lanka in 5 days LLMs: great for business but bad business Book Summary: Safe Haven by Mark Spitznagel Mac shortcut for typing Avagraha symbol On a bus with an asylum seeker Nicaragua in 5 days When to commit Generated code to version control Why I always buy a local SIM in a foreign country Use Makefile for Android Four days in Guadalajara, Mexico Android Navigation: Up vs Back Hotels vs Airbnb vs Hostels Currency issues in Argentina Abstractions should be deep not wide Some data on podcasting Always support compressed response in an API service A day in El Calafate - Patagonia, Argentina Hermetic docker images with Hugging Face machine learning models American Elections The sound of "ch" API services should always have usage Limits Hiking in El Chaltén - trekking capital of Argentina
Android: Catching NDK crashes
Ashish Bhatia · 2021-02-07 · via ashishb.net

On Android catching Java exceptions is easy via UncaughtExceptionHandler. Catching NDK crashes is a bit more convoluted. Since the native stack is probably corrupted, you want the crash handler to run on a separate process. Also, since the system might be in an unstable shape, don’t send the crash report to your web server or do anything fancy. Just write the crash report to a file, and on the next restart of the app, send to your web server and delete it from the disk. I ended up using jndcrash package for this.

  1. Create a new Service that extends ru.ivanarh.jndcrash.NDCrashService

  2. Run this as a sticky service in a separate process by adding the following to the AndroidManifest.xml

    1
    2
    3
    
    <!-- Create a new process to handle native crashes
             https://github.com/ivanarh/jndcrash#out-of-process -->
        <service android:name=".NdkCrashService" android:process=":ndkCrashReportProcess"/>
  3. Override onCrash to read the logcat logs and write a report to a new location. I didn’t care about it overwriting an existing native crash report, but if your app has a million+ installs, you should generate filename patterns for crash reports.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    @Override
      public void onCrash(String reportPath) {
        String logcatLogs = getLogcatLogs(NUM_LOGCAT_LINES);
        String ndkLogcatLogsReportPath = getNdkCrashLogcatLogsPath(this);
        Log.i(TAG, "onCrash, stack trace in " + reportPath);
        Log.i(TAG, "onCrash, logcat logs are in " + ndkLogcatLogsReportPath);
        Log.d(
          TAG,
          "Logcat logs for the native error (from last " +
          NUM_LOGCAT_LINES +
          " lines): \"" +
          logcatLogs +
          "\""
        );
        try (FileWriter fileWriter = new FileWriter(ndkLogcatLogsReportPath, false/* append */)) {
          for (String line : logcatLogs.split("\n")) {
            // Build fingerprint marks the beginning of native crash dump which is already
            // present in the reportPath file.
            if (line.contains("Build fingerprint")) {
              break;
            }
            fileWriter.write(line);
            fileWriter.write("\n");
          }
          fileWriter.flush();
        } catch (IOException e) {
          Log.e(TAG, "Error writing more logs to native crash report " + reportPath);
        }
      }
  4. In the app’s onCreate, initialize the crash reporter.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    private void initNdkCrashHandler() {
        final String reportPath = NdkCrashService.getNdkCrashLogReportPath(this);
        final NDCrashError error = NDCrash.initializeOutOfProcess(
          this,
          reportPath,
          NDCrashUnwinder.libunwind,
          NdkCrashService.class
        );
        if (error == NDCrashError.ok) {
          Log.i("MainApplication@initJndcrash", "NDK crash handler init successful");
        } else {
          Log.e("MainApplication@initJndcrash", "NDK crash handler init failed: " + error);
        }
      }
  5. It is probably best to read and submit any existing reports in the same initNdkCrashHandler method. Since we were using React Native, we ended up doing it a bit differently. We used Sentry to wrap a native crash and report it as a Java Exception; you can do this for your crash reporting mechanism as well.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    const uploadNdkCrashesIfAny = async () => {
      // This file path should be same here and in MainApplication.java
      const ndkCrashLogsFilePath = RNFS.CachesDirectoryPath + '/ndk_crash_logs.txt'
      const ndkCrashLogcatLogsFilePath = RNFS.CachesDirectoryPath + '/ndk_crash_logcat_logs.txt'
    
      if (!(await RNFS.exists(ndkCrashLogsFilePath))) {
        Logger.debug(
          'Sentry@uploadNdkCrashesIfAny',
          `crash log file ${ndkCrashLogsFilePath} not found, no native crashes recorded`
        )
        return
      }
    
      const fileSize = parseInt((await RNFS.stat(ndkCrashLogsFilePath)).size, 10)
      Logger.info(
        'Sentry@uploadNdkCrashesIfAny',
        `crash log file ${ndkCrashLogsFilePath} found (${fileSize} bytes), capturing it via Sentry`
      )
      const msg1 = (await RNFS.exists(ndkCrashLogcatLogsFilePath))
        ? await RNFS.readFile(ndkCrashLogcatLogsFilePath)
        : 'Logcat logs not available'
      const msg2 = await RNFS.readFile(ndkCrashLogsFilePath)
    
      Sentry.captureMessage(`NDK crash\n${msg1}\n${msg2}`)
      await RNFS.unlink(ndkCrashLogsFilePath)
    
      if (!(await RNFS.exists(ndkCrashLogcatLogsFilePath))) {
        await RNFS.unlink(ndkCrashLogcatLogsFilePath)
      }
    }

    And a simple mechanism to handle native crashes would be ready. The best part is that this approach is not tied to your crash reporter, so, you can choose Sentry or Firebase Crashlytics or any other mechanism.