[Tarantool-patches] [PATCH v2 1/4] fiber: add PoC for fiber creation backtrace

Cyrill Gorcunov gorcunov at gmail.com
Mon Jul 12 14:42:59 MSK 2021


On Fri, Jul 09, 2021 at 02:03:50PM +0300, Egor Elchinov via Tarantool-patches wrote:
> +void
> +backtrace_foreach_ip(backtrace_cb cb, void **ip_buf, int limit,
> +		     void *cb_ctx)
> +{
> +#ifndef TARGET_OS_DARWIN
> +	char proc_name[BACKTRACE_NAME_MAX];
> +	unw_word_t ip = 0, offset = 0;
> +	unw_proc_info_t pi;
> +	int frame_no, ret = 0;
> +	char *proc = NULL;
> +
> +	unw_accessors_t *acc = unw_get_accessors(unw_local_addr_space);
> +
> +	/*
> +	 * RIPs collecting comes from inside a helper routine
> +	 * so we skip the collector function address itself thus
> +	 * start fetching functions with frame number = 1.
> +	 */
> +	for (frame_no = 1; frame_no < limit && ip_buf[frame_no] != NULL;
> +	     frame_no++) {
> +		ip = (unw_word_t)ip_buf[frame_no];
> +
> +		if (acc->get_proc_name == NULL) {
> +			ret = unw_get_proc_info_by_ip(unw_local_addr_space,
> +						      ip, &pi, NULL);
> +			offset = ip - pi.start_ip;
> +		} else {
> +			ret = acc->get_proc_name(unw_local_addr_space, ip,
> +						 proc_name, sizeof(proc_name),
> +						 &offset, NULL);
> +			proc = proc_name;
> +		}
> +		if (ret != 0 || cb(frame_no - 1, (void *)ip, proc,
> +				   (size_t)offset, cb_ctx) != 0)
> +			break;
> +	}
> +
> +	if (ret != 0)
> +		say_debug("unwinding error: %s", unw_strerror(ret));
> +#else
> +	int frame_no, ret = 1;
> +	void *ip = NULL;
> +	size_t offset = 0;
> +	Dl_info dli;
> +
> +	for (frame_no = 1; frame_no < limit && ip_buf[frame_no] != NULL;
> +	     ++frame_no) {
> +		ip = ip_buf[frame_no];
> +		ret = dladdr(ip, &dli);
> +		offset = (char *)ip - (char *)dli.dli_saddr;
> +
> +		if (cb(frame_no - 1, ip, dli.dli_sname, offset, cb_ctx) != 0)
> +			break;

Looks like you forgot to test for dladdr error if I'm not missing
something obvious. So it could be like

		ret = dladdr(ip, &dli);
		if (ret == 0)
			break;
		offset = (char *)ip - (char *)dli.dli_saddr;
		if (cb(frame_no - 1, ip, dli.dli_sname, offset, cb_ctx) != 0)
			break;

no?

> +	}
> +
> +	if (ret == 0)
> +		say_debug("unwinding error: %i", ret);
> +#endif
> +}


More information about the Tarantool-patches mailing list