File: dump.c

package info (click to toggle)
libpulp 0.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,976 kB
  • sloc: ansic: 11,792; python: 1,216; sh: 881; makefile: 871; cpp: 582; asm: 387
file content (140 lines) | stat: -rw-r--r-- 3,924 bytes parent folder | download
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 *  libpulp - User-space Livepatching Library
 *
 *  Copyright (C) 2017-2021 SUSE Software Solutions GmbH
 *
 *  This file is part of libpulp.
 *
 *  libpulp is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  libpulp is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with libpulp.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <sys/types.h>
#include <argp.h>

#include "arguments.h"
#include "dump.h"
#include "introspection.h"
#include "patches.h"

extern struct ulp_metadata ulp;

static void
dump_metadata(struct ulp_metadata *ulp, int buildid_only)
{
  struct ulp_object *obj;
  struct ulp_unit *unit;
  struct ulp_reference *ref;
  const char *buildid_string = buildid_to_string(ulp->patch_id);

  if (ulp) {
    if (!buildid_only) {
      fprintf(stdout, "patch id: %s\n", buildid_string);
      fprintf(stdout, "so filename: %s\n", ulp->so_filename);
    }
    obj = ulp->objs;
    if (obj) {
      buildid_string = buildid_to_string((unsigned char *)obj->build_id);
      if (!buildid_only) {
        fprintf(stdout, "\n* build id: %s\n", buildid_string);
      }
      else {
        fprintf(stdout, "%s\n", buildid_string);
      }
      if (obj->name && !buildid_only) {
        fprintf(stdout, "* name: %s\n", obj->name);
      }
      else if (!buildid_only) {
        fprintf(stdout, "* name: \n");
      }
      if (!buildid_only) {
        fprintf(stdout, "* units: %d\n", obj->nunits);
      }
      unit = obj->units;
      while (unit && !buildid_only) {
        fprintf(stdout, "\n** old_fname: %s\n", unit->old_fname);
        fprintf(stdout, "** new_fname: %s\n", unit->new_fname);
        fprintf(stdout, "** old_faddr: %p\n", unit->old_faddr);
        unit = unit->next;
      }

      /* Show static references:  */
      fprintf(stdout, "\nstatic references: %d\n", ulp->nrefs);
      for (ref = ulp->refs; ref != NULL; ref = ref->next) {
        fprintf(stdout, "*** ref_name: %s\n", ref->reference_name);
        fprintf(stdout, "*** target_offset: %lx\n", ref->target_offset);
        fprintf(stdout, "*** patch_offset: %lx\n", ref->target_offset);
        fprintf(stdout, "*** tls variable: %s\n\n", ref->tls ? "yes" : "no");
      }
    }
  }
}

static void
dump_ulp_comments(const char *lib)
{
  char *out;

  extract_ulp_comment_to_mem(lib, &out);

  if (out == NULL)
    return;

  printf("* comments: \n");
  puts(out);

  free(out);
}

int
run_dump(struct arguments *arguments)
{
  char *livepatch;
  size_t livepatch_size;
  bool revert = arguments->revert;

  livepatch_size =
      extract_ulp_from_so_to_mem(arguments->args[0], revert, &livepatch, NULL);
  if (!livepatch) {
    WARN("Unable to extract metadata file from %s", arguments->args[0]);
    return 1;
  }

  if (load_patch_info_from_mem(livepatch, livepatch_size)) {
    WARN("error parsing the metadata file (%s).", arguments->args[0]);
    free(livepatch);
    return 1;
  }
  dump_metadata(&ulp, arguments->buildid);

  dump_ulp_comments(arguments->args[0]);
  free(livepatch);
  return 0;
}

struct argp_option *
get_command_option_dump(void)
{
  static struct argp_option options[] = {
    { 0, 0, 0, 0, "Options:", 0 },
    { "verbose", 'v', 0, 0, "Produce verbose output", 0 },
    { "quiet", 'q', 0, 0, "Don't produce any output", 0 },
    { "buildid", 'b', 0, 0, "Print the build id", 0 },
    { "revert", ULP_OP_REVERT, 0, 0,
      "dump reverse patch info.", 0 },
    { 0 }
  };

  return options;
}