PengZheng commented on code in PR #473: URL: https://github.com/apache/celix/pull/473#discussion_r1071859437
########## misc/error_injector/error_injector.h: ########## @@ -0,0 +1,86 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +#ifndef CELIX_ERROR_INJECTOR_H +#define CELIX_ERROR_INJECTOR_H +#ifdef __cplusplus +extern "C" { +#endif + +#include <assert.h> +#include <dlfcn.h> +#include <stddef.h> + +#define CELIX_EI_GET_CALLER(addr, level) \ +do { \ + Dl_info dlinfo; \ + void *dlret; \ + dlret = __builtin_return_address((level)); \ + assert(dladdr(dlret, &dlinfo)); \ + (addr) = dlinfo.dli_saddr; \ +} while(0) + +#define CELIX_EI_UNKNOWN_CALLER ((void *)-1) + +#define CELIX_EI_DECLARE(name, ret_type) \ +void celix_ei_expect_##name(void *caller, unsigned int level, ret_type ret, size_t ordinal=1) + +#define CELIX_EI_DEFINE(name, ret_type) \ +static void *name ## _caller; \ +static unsigned int name ## _caller_level; \ +static ret_type name ## _ret; \ +static size_t name ## _ordinal; \ + \ +void celix_ei_expect_##name(void *caller, unsigned int level, ret_type ret, size_t ordinal) \ +{ \ + name ## _caller = (caller); \ + name ## _caller_level = (level); \ + name ## _ret = (ret); \ + name ## _ordinal = (ordinal); \ +} + + +#define CELIX_EI_IMPL0(name) \ Review Comment: This is jest one error pattern, in which zero return value is treated as an error. `IMPL0` really means error on zero return value. Macros for other patterns (indeed, there are plenty of them) will be added when needed. ########## misc/error_injector/error_injector.h: ########## @@ -0,0 +1,86 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +#ifndef CELIX_ERROR_INJECTOR_H +#define CELIX_ERROR_INJECTOR_H +#ifdef __cplusplus +extern "C" { +#endif + +#include <assert.h> +#include <dlfcn.h> +#include <stddef.h> + +#define CELIX_EI_GET_CALLER(addr, level) \ +do { \ + Dl_info dlinfo; \ + void *dlret; \ + dlret = __builtin_return_address((level)); \ + assert(dladdr(dlret, &dlinfo)); \ + (addr) = dlinfo.dli_saddr; \ +} while(0) + +#define CELIX_EI_UNKNOWN_CALLER ((void *)-1) + +#define CELIX_EI_DECLARE(name, ret_type) \ +void celix_ei_expect_##name(void *caller, unsigned int level, ret_type ret, size_t ordinal=1) + +#define CELIX_EI_DEFINE(name, ret_type) \ +static void *name ## _caller; \ +static unsigned int name ## _caller_level; \ +static ret_type name ## _ret; \ +static size_t name ## _ordinal; \ + \ +void celix_ei_expect_##name(void *caller, unsigned int level, ret_type ret, size_t ordinal) \ +{ \ + name ## _caller = (caller); \ + name ## _caller_level = (level); \ + name ## _ret = (ret); \ + name ## _ordinal = (ordinal); \ +} + + +#define CELIX_EI_IMPL0(name) \ Review Comment: This is jest one error pattern, in which zero return value is treated as an error. Macros for other patterns (indeed, there are plenty of them) will be added when needed. ########## misc/error_injector/error_injector.h: ########## @@ -0,0 +1,86 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +#ifndef CELIX_ERROR_INJECTOR_H +#define CELIX_ERROR_INJECTOR_H +#ifdef __cplusplus +extern "C" { +#endif + +#include <assert.h> +#include <dlfcn.h> +#include <stddef.h> + +#define CELIX_EI_GET_CALLER(addr, level) \ +do { \ + Dl_info dlinfo; \ + void *dlret; \ + dlret = __builtin_return_address((level)); \ + assert(dladdr(dlret, &dlinfo)); \ + (addr) = dlinfo.dli_saddr; \ +} while(0) + +#define CELIX_EI_UNKNOWN_CALLER ((void *)-1) + +#define CELIX_EI_DECLARE(name, ret_type) \ +void celix_ei_expect_##name(void *caller, unsigned int level, ret_type ret, size_t ordinal=1) + +#define CELIX_EI_DEFINE(name, ret_type) \ +static void *name ## _caller; \ +static unsigned int name ## _caller_level; \ +static ret_type name ## _ret; \ +static size_t name ## _ordinal; \ + \ +void celix_ei_expect_##name(void *caller, unsigned int level, ret_type ret, size_t ordinal) \ +{ \ + name ## _caller = (caller); \ + name ## _caller_level = (level); \ + name ## _ret = (ret); \ + name ## _ordinal = (ordinal); \ +} + + +#define CELIX_EI_IMPL0(name) \ +do { \ + void *addr = CELIX_EI_UNKNOWN_CALLER; \ + if(name##_caller) { \ + if(name ## _caller != CELIX_EI_UNKNOWN_CALLER) { \ + /* we can not use CELIX_EI_GET_CALLER(addr, name ## _caller_level) */ \ + switch(name ## _caller_level) { \ + case 0: \ + CELIX_EI_GET_CALLER(addr, 0); \ Review Comment: IIRC, constant is required here. ########## misc/error_injector/error_injector.h: ########## @@ -0,0 +1,86 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +#ifndef CELIX_ERROR_INJECTOR_H +#define CELIX_ERROR_INJECTOR_H +#ifdef __cplusplus +extern "C" { +#endif + +#include <assert.h> +#include <dlfcn.h> +#include <stddef.h> + +#define CELIX_EI_GET_CALLER(addr, level) \ +do { \ + Dl_info dlinfo; \ + void *dlret; \ + dlret = __builtin_return_address((level)); \ + assert(dladdr(dlret, &dlinfo)); \ Review Comment: `-rdynamic` is used to make `dladdr` work. This tech works for release and debug build. However, there is a limitation, if the caller is a static function (not visible outside the current translation unit), it will not work. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org