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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
/* Check VMCOREINFO per-line post-set hooks.
Copyright (C) 2016 Petr Tesarik <ptesarik@suse.com>
This file is free software; you can redistribute it and/or modify
it under the terms of either
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at
your option) any later version
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at
your option) any later version
or both in parallel, as here.
libkdumpfile 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
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <libkdumpfile/kdumpfile.h>
#include "testutil.h"
#define xstr(s) #s
#define str(s) xstr(s)
#define OSRELEASE "3.4.5-test"
#define ALT_OSRELEASE "4.0.0-test"
#define ATTR_OSRELEASE "linux.uts.release"
#define PAGESIZE 2048 /* unlikely to match host page size. */
#define ALT_PAGESIZE 8192
#define ATTR_PAGESIZE "arch.page_size"
#define SYM_NAME "test_symbol"
#define SYM_VALUE 0x123456
#define ALT_SYM_VALUE 0x789abc
#define LEN_NAME "test_length"
#define LEN_VALUE 16
#define ALT_LEN_VALUE 20
#define ATTR_LEN "linux.vmcoreinfo.LENGTH." LEN_NAME
#define NUM_NAME "test_number"
#define NUM_VALUE 64
#define ALT_NUM_VALUE 128
#define ATTR_NUM "linux.vmcoreinfo.NUMBER." NUM_NAME
#define OFF_NAME "test_struct.test_off"
#define OFF_VALUE 80
#define ALT_OFF_VALUE 96
#define ATTR_OFF "linux.vmcoreinfo.OFFSET." OFF_NAME
#define SIZE_NAME "test_size"
#define SIZE_VALUE 240
#define ALT_SIZE_VALUE 320
#define ATTR_SIZE "linux.vmcoreinfo.SIZE." SIZE_NAME
#define ATTR_LINES "linux.vmcoreinfo.lines"
static const char vmcore[] =
"OSRELEASE=" OSRELEASE "\n"
"PAGESIZE=" str(PAGESIZE) "\n"
"SYMBOL(" SYM_NAME ")=" str(SYM_VALUE) "\n"
"LENGTH(" LEN_NAME ")=" str(LEN_VALUE) "\n"
"NUMBER(" NUM_NAME ")=" str(NUM_VALUE) "\n"
"OFFSET(" OFF_NAME ")=" str(OFF_VALUE) "\n"
"SIZE(" SIZE_NAME ")=" str(SIZE_VALUE) "\n"
"";
static int
set_vmcoreinfo_value(kdump_ctx_t *ctx, const char *val)
{
kdump_blob_t *blob;
kdump_attr_t attr;
kdump_status status;
blob = kdump_blob_new_dup(val, strlen(val));
if (!blob) {
fprintf(stderr, "Cannot allocate VMCOREINFO blob.\n");
return TEST_ERR;
}
attr.type = KDUMP_BLOB;
attr.val.blob = blob;
status = kdump_set_attr(ctx, "linux.vmcoreinfo.raw", &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set vmcoreinfo: %s\n",
kdump_get_err(ctx));
return TEST_ERR;
}
return TEST_OK;
}
static int
check_string(kdump_ctx_t *ctx, const char *attrpath, const char *expect)
{
kdump_attr_t attr;
kdump_status status;
status = kdump_get_attr(ctx, attrpath, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "%s: Cannot get value: %s\n",
attrpath, kdump_get_err(ctx));
return TEST_ERR;
}
if (attr.type != KDUMP_STRING) {
fprintf(stderr, "%s: Wrong attribute type: %d\n",
attrpath, (int) attr.type);
return TEST_FAIL;
}
if (strcmp(attr.val.string, expect)) {
fprintf(stderr, "%s: Invalid attribute value: '%s' != '%s'\n",
attrpath, attr.val.string, expect);
return TEST_FAIL;
}
printf("%s: %s\n", attrpath, attr.val.string);
return TEST_OK;
}
static int
check_number(kdump_ctx_t *ctx, const char *attrpath, long long expect)
{
kdump_attr_t attr;
kdump_status status;
status = kdump_get_attr(ctx, attrpath, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "%s: Cannot get value: %s\n",
attrpath, kdump_get_err(ctx));
return TEST_ERR;
}
if (attr.type != KDUMP_NUMBER) {
fprintf(stderr, "%s: Wrong attribute type: %d\n",
attrpath, (int) attr.type);
return TEST_FAIL;
}
if (attr.val.number != expect) {
fprintf(stderr, "%s: Invalid attribute value: %lld != %lld\n",
attrpath, (long long) attr.val.number, expect);
return TEST_FAIL;
}
printf("%s: %lld\n", attrpath, (long long) attr.val.number);
return TEST_OK;
}
static int
check(kdump_ctx_t *ctx)
{
kdump_attr_t attr;
kdump_addr_t symval;
kdump_status status;
int rc, tmprc;
attr.type = KDUMP_STRING;
attr.val.string = "linux";
status = kdump_set_attr(ctx, "addrxlat.ostype", &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set ostype: %s\n",
kdump_get_err(ctx));
return TEST_ERR;
}
rc = set_vmcoreinfo_value(ctx, vmcore);
if (rc != TEST_OK)
return rc;
attr.type = KDUMP_STRING;
/* Check modifications */
attr.val.string = ALT_OSRELEASE;
status = kdump_set_attr(ctx, ATTR_LINES ".OSRELEASE", &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot modify %s: %s\n",
ATTR_LINES ".OSRELEASE", kdump_get_err(ctx));
return TEST_ERR;
}
tmprc = check_string(ctx, ATTR_OSRELEASE, ALT_OSRELEASE);
if (tmprc == TEST_ERR)
return tmprc;
if (tmprc != TEST_OK)
rc = tmprc;
attr.val.string= str(ALT_PAGESIZE);
status = kdump_set_attr(ctx, ATTR_LINES ".PAGESIZE", &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot modify %s: %s\n",
ATTR_LINES ".PAGESIZE", kdump_get_err(ctx));
return TEST_ERR;
}
tmprc = check_number(ctx, ATTR_PAGESIZE, ALT_PAGESIZE);
if (tmprc == TEST_ERR)
return tmprc;
if (tmprc != TEST_OK)
rc = tmprc;
attr.val.string= str(ALT_SYM_VALUE);
status = kdump_set_attr(ctx, ATTR_LINES ".SYMBOL(" SYM_NAME ")",
&attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot modify %s: %s\n",
ATTR_LINES ".SYMBOL(" SYM_NAME ")",
kdump_get_err(ctx));
return TEST_ERR;
}
status = kdump_vmcoreinfo_symbol(ctx, SYM_NAME, &symval);
if (status != KDUMP_OK) {
fprintf(stderr, "%s: Cannot get value: %s\n",
SYM_NAME, kdump_get_err(ctx));
return TEST_ERR;
}
if (symval != ALT_SYM_VALUE) {
fprintf(stderr, "%s: Invalid attribute value: %llx != %llx\n",
SYM_NAME, (long long) symval,
(long long) ALT_SYM_VALUE);
rc = TEST_FAIL;
} else
printf("%s = %llx\n", SYM_NAME, (long long) symval);
attr.val.string= str(ALT_LEN_VALUE);
status = kdump_set_attr(ctx, ATTR_LINES ".LENGTH(" LEN_NAME ")",
&attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot modify %s: %s\n",
ATTR_LINES ".LENGTH(" LEN_NAME ")",
kdump_get_err(ctx));
return TEST_ERR;
}
tmprc = check_number(ctx, ATTR_LEN, ALT_LEN_VALUE);
if (tmprc == TEST_ERR)
return tmprc;
if (tmprc != TEST_OK)
rc = tmprc;
attr.val.string= str(ALT_NUM_VALUE);
status = kdump_set_attr(ctx, ATTR_LINES ".NUMBER(" NUM_NAME ")",
&attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot modify %s: %s\n",
ATTR_LINES ".NUMBER(" NUM_NAME ")",
kdump_get_err(ctx));
return TEST_ERR;
}
tmprc = check_number(ctx, ATTR_NUM, ALT_NUM_VALUE);
if (tmprc == TEST_ERR)
return tmprc;
if (tmprc != TEST_OK)
rc = tmprc;
attr.val.string= str(ALT_OFF_VALUE);
status = kdump_set_attr(ctx, ATTR_LINES ".OFFSET(" OFF_NAME ")",
&attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot modify %s: %s\n",
ATTR_LINES ".OFFSET(" OFF_NAME ")",
kdump_get_err(ctx));
return TEST_ERR;
}
tmprc = check_number(ctx, ATTR_OFF, ALT_OFF_VALUE);
if (tmprc == TEST_ERR)
return tmprc;
if (tmprc != TEST_OK)
rc = tmprc;
attr.val.string= str(ALT_SIZE_VALUE);
status = kdump_set_attr(ctx, ATTR_LINES ".SIZE(" SIZE_NAME ")",
&attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot modify %s: %s\n",
ATTR_LINES ".SIZE(" SIZE_NAME ")",
kdump_get_err(ctx));
return TEST_ERR;
}
tmprc = check_number(ctx, ATTR_SIZE, ALT_SIZE_VALUE);
if (tmprc == TEST_ERR)
return tmprc;
if (tmprc != TEST_OK)
rc = tmprc;
/* Check cleanup */
return rc;
}
int
main(int argc, char **argv)
{
kdump_ctx_t *ctx;
int rc;
ctx = kdump_new();
if (!ctx) {
perror("Cannot initialize dump context");
return TEST_ERR;
}
rc = check(ctx);
kdump_free(ctx);
return rc;
}
|