libc: support more time modes

We are currently only answering requests for CLOCK_REALTIME, but we could
easily handle:

    * CLOCK_REALTIME_COARSE, which is effective the same as CLOCK_REALTIME
      but faster. In our case, all time sources are equally fast
    * CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID, since we can
      easily get runtimes for our threads and publish that.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
tests: add test for thread clock

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
vga: Fix VGA base address

Physical memory base address is 0xffffc00000000000, so VGA base address
should be 0xffffc000000b8000.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Takuya ASADA <syuu@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
vga: Handle line feed

Handle line feed in the VGA.  To access termios flag, add a constructor.

Signed-off-by: Takuya ASADA <syuu@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
VGA: Add input_ready() and readch() stubs

To use VGAConsole class, add empty input_ready() and readch().

Signed-off-by: Takuya ASADA <syuu@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
loader: Add '--vga' to switch console

Add --vga on cmdline, to switch vga console.

Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Takuya ASADA <syuu@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
loader: fix spurious error message

When trying to run a nonexistant program, e.g., "run.py -e xyz", sometimes
(especially in a debug build) we would see the spurious message:

    program xyz returned -16384

The bug is simple: when osv::run returns null, meaning the program was not
found, it does not set the "ret" value. Our code checked if ret!=0 before
checking if osv::run actually found the program, which is wrong. Simply
changing the order of the code solves this bug.

Fixes #156.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Remove vga.cc.orig committed by mistake

I messed up in commit cc6b3a3 ("vga: Handle line feed"). Clean it up.

Reported-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Add patch command backup files to gitignore

To prevent similar mistakes as in commit cc6b3a3 ("vga: Handle line
feed"), add the backup files generated by 'patch' to gitignore.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
time.cc: generalize the std::chrono -> timespec conversion

The code "file_ts" converting a std::chrono::duration into a struct timespec,
was unnecessarily constrained to take as input a std::chrono::nanoseconds,
whereas it can actually work with durations of any resolution (the
duration_cast will convert it to the correct units), so this patch makes
this generalization which, I think, makes the code clearer.

At first I decided to move this general function into <osv/clock.hh> and
make it a bit more friendly (returning a timespec, instead of writing into
one), but at the end, I decided not to move it. typespec is a dangerous
type because though it does specify the clock's resolution (always
nanoseconds) it doesn't specify the clock's epoch, so one can make mistakes
like calling clock_gettime(CLOCK_MONOTONIC), and pass the returned timespec
to pthread_cond_timedwait, which expects CLOCK_REALTIME not CLOCK_MONOTONIC.
So we should avoid timespec whenever we can, and I don't think we'll need
the fill_ts() function anywhere outside time.cc.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
gdb: make Trace convertible to string

Useful for ad-hoc printing.

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
gdb: support backtraces shorter than backtrace_len

If there are fewer frames than backtrace_len the remaining
addresses will be zeros. Let's not print them.

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
gdb: resolve tracepoint_base outside the loop

Speeds things up slightly.

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Add python wrapper for addr2line

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
gdb: extract trace related abstractions to an independent module

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
trace: support persisting trace stream to file

Having traces persisted in a file has the follwing advantages:

 - trace collection is decoupled from trace analysis. trace analysis
   tools no longer have to be part of GDB scripts. This is important
   for EC2 instances, to which we cannot connect with GDB. In this
   case trace data can be downloaded using other channels, but tools
   remain unchanged.

 - trace analysis doesn't have to be performed in the same OSv session.
   After trace is collected OSv can be shut down or left running without
   further disturbance.

The trace is saved in a binary format of the following structure:

  <format-version>
  <N_tp>
  <tracepoint 1>
  ...
  <tracepoint N_tp>
  <trace 1>
  <trace 2>
  ...
  <trace N_t>

Where:

  <tracepoint X> = <tp_key><name><signature><format>
  <trace X> = <tp_key><thread><time><cpu>(<backtrace_addr>*)<0><data>

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
gdb: introduce command to save traces in binary form

Usage:

  osv trace save <filename>

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
trace: add command-line tool for processing trace files

For all options check:

  scripts/trace.py -h

Example: list all traces:

  scripts/trace.py list trace-file

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Introduce python module for tree printing

Prints hierarchical data in a form of tree:

Example:

Node0
 |-- Node1
 |    |-- Node2
 |    |
 |    \-- Node3
 |
 \-- Node4

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
trace: add source address formatting options

Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>