php扩展开发 Segmentation fault (core dumped)
最近在学习开发php扩展,创建类的时候,编译成功,但是执行测试文件的时候报错了:Segmentation fault (core dumped)
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_Person.h"
/* If you declare any globals in php_Person.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(Person)
*/
/* True global resources - no need for thread safety here */
static int le_Person;
zend_class_entry *person_ce;
ZEND_BEGIN_ARG_INFO(arg_person_setname, 0)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("Person.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_Person_globals, Person_globals)
STD_PHP_INI_ENTRY("Person.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_Person_globals, Person_globals)
PHP_INI_END()
*/
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_Person_compiled(string arg)
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_Person_compiled)
{
char *arg = NULL;
size_t arg_len, len;
zend_string *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
return;
}
strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "Person", arg);
RETURN_STR(strg);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
PHP_METHOD(Person, __construct)
{
php_printf("begin");
}
PHP_METHOD(Person, __destruct)
{
php_printf("end");
}
PHP_METHOD(Person, getname)
{
zval *self, *name, rv;
self = getThis();
name = zend_read_property(Z_OBJCE_P(self), self, "name", sizeof("name") - 1, 0, &rv);
//int type;
//type = Z_TYPE_P(name);
//RETURN_LONG(type);
RETURN_STRING(Z_STRVAL_P(name));
}
PHP_METHOD(Person, setname)
{
char *name;
int arg_len;
zval *value ,*self;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &arg_len) == FAILURE) {
return;
}
self = getThis();
zend_update_property_string(Z_OBJCE_P(self), self, "name", sizeof("name")-1, name);
//RETURN_TRUE;
}
/* {{{ php_Person_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_Person_init_globals(zend_Person_globals *Person_globals)
{
Person_globals->global_value = 0;
Person_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ Person_functions[]
*
* Every user visible function must have an entry in Person_functions[].
*/
const zend_function_entry Person_functions[] = {
PHP_ME(Person, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Person, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
PHP_ME(Person, getname, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Person, setname, arg_person_setname, ZEND_ACC_PUBLIC)
PHP_FE(confirm_Person_compiled, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in Person_functions[] */
};
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(Person)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
zend_class_entry person;
INIT_CLASS_ENTRY(person, "Person", Person_functions);
person_ce = zend_register_internal_class_ex(&person, NULL);
zend_declare_property_string(person_ce, ZEND_STRL("name"),"test", ZEND_ACC_PUBLIC);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(Person)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(Person)
{
#if defined(COMPILE_DL_PERSON) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(Person)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(Person)
{
php_info_print_table_start();
php_info_print_table_header(2, "Person support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/* {{{ Person_module_entry
*/
zend_module_entry Person_module_entry = {
STANDARD_MODULE_HEADER,
"Person",
Person_functions,
PHP_MINIT(Person),
PHP_MSHUTDOWN(Person),
PHP_RINIT(Person), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(Person), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(Person),
PHP_PERSON_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_PERSON
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(Person)
#endif
php测试代码
$obj = new Person();
$obj->setname("aaaaa");
gdb 调试截图
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@lifei6671 这样改编译报warning,但是执行测试文件可以了
请看问题第二张图,很明显是内存越界了,无法访问的地址是高地址,在栈上,说明有地方的指针变量没加*,贴出的代码没问题
错误提示里面说你访问内存越界,应该是这个方法有问题:
这个函数的第一个方法是类的实例一般是
zend_class_entry
的指针类型。而你提供的明显不是类的实例。改成这样试试: