Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 3698e44 in mainline for uspace/app/taskdump/taskdump.c


Ignore:
Timestamp:
2010-01-26T22:46:29Z (12 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master
Children:
196a1439, 2314381
Parents:
e515b21a
Message:

Add ability to determine task name and load symbol table from the binary executable. Resolve symbol names in stack traces when dumping.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/taskdump/taskdump.c

    re515b21a r3698e44  
    4646#include <bool.h>
    4747
     48#include <symtab.h>
    4849#include <stacktrace.h>
    4950
     
    5657static task_id_t task_id;
    5758static bool dump_memory;
     59static char *app_name;
     60static symtab_t *app_symtab;
    5861
    5962static int connect_task(task_id_t task_id);
     
    6770static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value);
    6871
     72static void autoload_syms(void);
     73static char *get_app_task_name(void);
     74static char *fmt_sym_address(uintptr_t addr);
     75
    6976int main(int argc, char *argv[])
    7077{
     
    9097        }
    9198
    92         printf("Dumping task %lld.\n\n", task_id);
     99        app_name = get_app_task_name();
     100        app_symtab = NULL;
     101
     102        printf("Dumping task '%s' (task ID %lld).\n", app_name, task_id);
     103        autoload_syms();
     104        putchar('\n');
    93105
    94106        rc = threads_dump();
     
    303315        uintptr_t pc, fp, nfp;
    304316        stacktrace_t st;
     317        char *sym_pc;
    305318        int rc;
    306319
     
    320333
    321334        while (stacktrace_fp_valid(&st, fp)) {
    322                 printf("%p: %p()\n", fp, pc);
     335                sym_pc = fmt_sym_address(pc);
     336                printf("  %p: %s()\n", fp, sym_pc);
     337                free(sym_pc);
    323338
    324339                rc = stacktrace_ra_get(&st, fp, &pc);
     
    412427}
    413428
     429/** Attempt to find the right executable file and load the symbol table. */
     430static void autoload_syms(void)
     431{
     432        char *file_name;
     433        int rc;
     434
     435        assert(app_name != NULL);
     436        assert(app_symtab == NULL);
     437
     438        rc = asprintf(&file_name, "/app/%s", app_name);
     439        if (rc < 0) {
     440                printf("Memory allocation failure.\n");
     441                exit(1);
     442        }
     443
     444        rc = symtab_load(file_name, &app_symtab);
     445        if (rc == EOK) {
     446                printf("Loaded symbol table from %s\n", file_name);
     447                free(file_name);
     448                return;
     449        }
     450
     451        free(file_name);
     452
     453        rc = asprintf(&file_name, "/srv/%s", app_name);
     454        if (rc < 0) {
     455                printf("Memory allocation failure.\n");
     456                exit(1);
     457        }
     458
     459        rc = symtab_load("/srv/xyz", &app_symtab);
     460        if (rc == EOK) {
     461                printf("Loaded symbol table from %s\n", file_name);
     462                free(file_name);
     463                return;
     464        }
     465
     466        free(file_name);
     467        printf("Failed autoloading symbol table.\n");
     468}
     469
     470static char *get_app_task_name(void)
     471{
     472        char dummy_buf;
     473        size_t copied, needed, name_size;
     474        char *name;
     475        int rc;
     476
     477        rc = udebug_name_read(phoneid, &dummy_buf, 0, &copied, &needed);
     478        if (rc < 0)
     479                return NULL;
     480
     481        name_size = needed;
     482        name = malloc(name_size + 1);
     483        rc = udebug_name_read(phoneid, name, name_size, &copied, &needed);
     484        if (rc < 0) {
     485                free(name);
     486                return NULL;
     487        }
     488
     489        assert(copied == name_size);
     490        assert(copied == needed);
     491        name[copied] = '\0';
     492
     493        return name;
     494}
     495
     496/** Format address in symbolic form.
     497 *
     498 * Formats address as <symbol_name>+<offset> (<address>), if possible,
     499 * otherwise as <address>.
     500 *
     501 * @param addr  Address to format.
     502 * @return      Newly allocated string, address in symbolic form.
     503 */
     504static char *fmt_sym_address(uintptr_t addr)
     505{
     506        char *name;
     507        size_t offs;
     508        int rc;
     509        char *str;
     510
     511        if (app_symtab != NULL) {
     512                rc = symtab_addr_to_name(app_symtab, addr, &name, &offs);
     513        } else {
     514                rc = ENOTSUP;
     515        }
     516
     517        if (rc == EOK) {
     518                rc = asprintf(&str, "(%p) %s+%p", addr, name, offs);
     519        } else {
     520                rc = asprintf(&str, "%p", addr);
     521        }
     522
     523        if (rc < 0) {
     524                printf("Memory allocation error.\n");
     525                exit(1);
     526        }
     527
     528        return str;
     529}
     530
    414531/** @}
    415532 */
Note: See TracChangeset for help on using the changeset viewer.