Giter Site home page Giter Site logo

How to assert()? about cloudlibc HOT 4 CLOSED

nuxinl avatar nuxinl commented on August 18, 2024
How to assert()?

from cloudlibc.

Comments (4)

EdSchouten avatar EdSchouten commented on August 18, 2024

Ideally, cloudlibc would have no observable global mutable state, and thus no global stdio streams (stdin, stdout, stderr). As you pointed out, this would be annoying for assert(), which doesn't take an explicit logging sink.

Though I think people should write their software in a dependency injected way, I think we can also agree that requiring software to have logging sinks injected (both for events and failure diagnostics) would be far too pedantic. This is why cloudlibc does provide a stderr. The following libc interfaces write to stderr:

  • Assertions: assert(),
  • Error message printing: fmtmsg(), perror() and psignal(),
  • Logging: syslog().

By default, stderr simply discards anything that's written to it. On a POSIX-like system, you'd be able to use dup2() to replace the underlying file descriptor. This wouldn't work on CloudABI, because stderr is not attached to file descriptor 2. File descriptor 2 has no special meaning. It can be used arbitrarily.

ISO C provides the freopen() function that could be used to redirect stderr, but just like fopen(), we can't implement it in a meaningful way. This is why cloudlibc provides a generalization of freopen(), called fswap():

void fswap(FILE *, FILE *);

This function can be used to swap the state of two FILE objects. Below is an example of how it may be used in practice:

#include <argdata.h>
#include <assert.h>
#include <program.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>

void program_main(const argdata_t *ad) {
  // Reconfigure stderr.
  int fd;
  if (argdata_get_fd(ad, &fd) == 0) {
    FILE *f = fdopen(fd, "w");
    if (f != NULL) {
      fswap(f, stderr);
      fclose(f);
    }
  }

  // Write various things over stderr.
  fprintf(stderr, "I am now going to crash.\n");
  syslog(LOG_INFO, "Also logging that I'm going to crash.");
  assert(0 && "Crashing!");
  fprintf(stderr, "Assertions disabled?\n");
  exit(1);
}

And here is what it looks like when invoked:

$ x86_64-unknown-cloudabi-cc -o bla bla.c
$ cat bla.yaml
%TAG ! tag:nuxi.nl,2015:cloudabi/
---
!fd stdout
$ cloudabi-run bla < bla.yaml
I am now going to crash.
2019-01-01T21:57:19.636344863Z INFO      Also logging that I'm going to crash.
bla.c:21: assertion failed in program_main(): 0 && "Crashing!"
zsh: abort      cloudabi-run bla < bla.yaml

Hope that helps!

from cloudlibc.

mcandre avatar mcandre commented on August 18, 2024

@EdSchouten Thank you for the tip! assert() definitely works as long as stderr is remapped to the stderr file descriptor configured in YAML.

Say, could we flesh out some similar printf(), puts(), putchar(), implementations, so that users have the option of remapping stdout as well?

from cloudlibc.

EdSchouten avatar EdSchouten commented on August 18, 2024

I'm not convinced we should add those. Looking at the bulk of library C/C++ code out there, you see that stdin and stdout are not used that often. When they are, it is often used accidentally or in bad taste. You do see that stderr is used quite often, typically for logging events/errors that happen in the background. This, together with what's discussed above, is the reason why only stderr is provided.

Within user-interactive applications (e.g., UNIX shell tools) you do see stdin and stdout being used quite often. So far the focus hasn't really been to bring those over to CloudABI in close to unmodified form. For those applications, the absence of stdin and stdout is a nuisance compared to other things that are missing (e.g., open()). I therefore think it it would make more sense to have stdin, stdout, printf(), scanf(), etc. part of a compat layer as mentioned here.

from cloudlibc.

mcandre avatar mcandre commented on August 18, 2024

Well in that case, let's at least offer an assert_fd() with a stream parameter, so that our users can enjoy easily integrating assertions in their codebases with cloudlibc!

from cloudlibc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.