mirror of
https://github.com/kdave/btrfs-progs
synced 2025-01-26 23:43:09 +00:00
btrfs-progs: add simple error injection framework
To be able to test errors at specific locations, add a simple way to check for a condition in code and controlled from user space environment variable INJECT. For now a single value is accepted. Use like: if (inject_error(0x1234)) { do_something(); return -ERROR; } This is enabled in debugging build by default (make D=1) and can be enabled on demand too (make EXTRA_CFLAGS=-DINJECT). Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
parent
4790665142
commit
8922ab5d6a
3
Makefile
3
Makefile
@ -67,7 +67,7 @@ include Makefile.extrawarn
|
||||
EXTRA_CFLAGS :=
|
||||
EXTRA_LDFLAGS :=
|
||||
|
||||
DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3
|
||||
DEBUG_CFLAGS_DEFAULT = -O0 -U_FORTIFY_SOURCE -ggdb3 -DINJECT
|
||||
DEBUG_CFLAGS_INTERNAL =
|
||||
DEBUG_CFLAGS :=
|
||||
|
||||
@ -202,6 +202,7 @@ objects = \
|
||||
common/format-output.o \
|
||||
common/fsfeatures.o \
|
||||
common/help.o \
|
||||
common/inject-error.o \
|
||||
common/messages.o \
|
||||
common/open-utils.o \
|
||||
common/parse-utils.o \
|
||||
|
88
common/inject-error.c
Normal file
88
common/inject-error.c
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public
|
||||
* License v2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This program 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 a copy of the GNU General Public
|
||||
* License along with this program; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 021110-1307, USA.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Simple error injection framework. Enabled at build time by -DINJECT.
|
||||
*/
|
||||
|
||||
#include "inject-error.h"
|
||||
|
||||
#ifdef INJECT
|
||||
|
||||
static bool inject_verbose = true;
|
||||
|
||||
static bool cookie_enabled(unsigned long cookie) {
|
||||
const char *inj = getenv("INJECT");
|
||||
unsigned long envcookie;
|
||||
|
||||
if (inj == NULL || inj[0] == 0)
|
||||
return false;
|
||||
|
||||
envcookie = strtoul(inj, NULL, 0);
|
||||
if (envcookie == cookie)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
#define inject_error(cookie) __inject_error((cookie), __FILE__, __LINE__)
|
||||
|
||||
bool __inject_error(unsigned long cookie, const char *file, int line) {
|
||||
if (cookie == 0x0) {
|
||||
fprintf(stderr, "Error injection testing cookie 0x0\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (cookie_enabled(cookie)) {
|
||||
if (inject_verbose)
|
||||
fprintf(stderr, "Error injection: cookie 0x%lx in %s:%d\n",
|
||||
cookie, file, line);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define inject_error(cookie) (false)
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef DEMO
|
||||
#include <unistd.h>
|
||||
/* gcc -o inject-error inject-error.c -DDEMO */
|
||||
int work(int x) {
|
||||
sleep(1);
|
||||
printf("x=%d\n", x);
|
||||
if (x == 3 && inject_error(0x03)) {
|
||||
printf("error injected\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int ret = 0;
|
||||
int x = 1;
|
||||
|
||||
printf("Injection: INJECT=%s\n", getenv("INJECT"));
|
||||
while (ret == 0) {
|
||||
ret = work(x);
|
||||
x++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
35
common/inject-error.h
Normal file
35
common/inject-error.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public
|
||||
* License v2 as published by the Free Software Foundation.
|
||||
*
|
||||
* This program 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 a copy of the GNU General Public
|
||||
* License along with this program; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 021110-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef __INJECT_ERROR_H__
|
||||
#define __INJECT_ERROR_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef INJECT
|
||||
|
||||
#define inject_error(cookie) __inject_error((cookie), __FILE__, __LINE__)
|
||||
bool __inject_error(unsigned long cookie, const char *file, int line);
|
||||
|
||||
#else
|
||||
|
||||
#define inject_error(cookie) (false)
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user