Skip to content
Snippets Groups Projects
ex_cmds2.c 127 KiB
Newer Older
			v = eval_expr(bp->dbg_name, NULL);
			debug_newval = typval_tostring(v);
			free_tv(bp->dbg_val);
			bp->dbg_val = v;
		    }
		    free_tv(tv);
		}
	    }
	    else if (bp->dbg_val != NULL)
	    {
		debug_oldval = typval_tostring(bp->dbg_val);
		debug_newval = typval_tostring(NULL);
		free_tv(bp->dbg_val);
		bp->dbg_val = NULL;
		line = TRUE;
	    }

	    if (line)
	    {
		lnum = after > 0 ? after : 1;
		break;
	    }

	    got_int |= prev_got_int;
	}
#endif
    }
    if (name != fname)
	vim_free(name);

    return lnum;
}

/*
 * Called when a breakpoint was encountered.
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
dbg_breakpoint(char_u *name, linenr_T lnum)
{
    /* We need to check if this line is actually executed in do_one_cmd() */
    debug_breakpoint_name = name;
    debug_breakpoint_lnum = lnum;
}
# if defined(FEAT_PROFILE) || defined(FEAT_RELTIME) || defined(PROTO)
/*
 * Store the current time in "tm".
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_start(proftime_T *tm)
# ifdef WIN3264
    QueryPerformanceCounter(tm);
# else
    gettimeofday(tm, NULL);
# endif
}

/*
 * Compute the elapsed time from "tm" till now and store in "tm".
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_end(proftime_T *tm)
{
    proftime_T now;

# ifdef WIN3264
    QueryPerformanceCounter(&now);
    tm->QuadPart = now.QuadPart - tm->QuadPart;
# else
    gettimeofday(&now, NULL);
    tm->tv_usec = now.tv_usec - tm->tv_usec;
    tm->tv_sec = now.tv_sec - tm->tv_sec;
    if (tm->tv_usec < 0)
    {
	tm->tv_usec += 1000000;
	--tm->tv_sec;
    }
# endif
}

/*
 * Subtract the time "tm2" from "tm".
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_sub(proftime_T *tm, proftime_T *tm2)
# ifdef WIN3264
    tm->QuadPart -= tm2->QuadPart;
# else
    tm->tv_usec -= tm2->tv_usec;
    tm->tv_sec -= tm2->tv_sec;
    if (tm->tv_usec < 0)
    {
	tm->tv_usec += 1000000;
	--tm->tv_sec;
    }
# endif
/*
 * Return a string that represents the time in "tm".
 * Uses a static buffer!
 */
    char *
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_msg(proftime_T *tm)
{
    static char buf[50];

# ifdef WIN3264
    LARGE_INTEGER   fr;

    QueryPerformanceFrequency(&fr);
    sprintf(buf, "%10.6lf", (double)tm->QuadPart / (double)fr.QuadPart);
# else
    sprintf(buf, "%3ld.%06ld", (long)tm->tv_sec, (long)tm->tv_usec);
# endif
Bram Moolenaar's avatar
Bram Moolenaar committed
# if defined(FEAT_FLOAT) || defined(PROTO)
/*
 * Return a float that represents the time in "tm".
 */
    float_T
profile_float(proftime_T *tm)
{
#  ifdef WIN3264
    LARGE_INTEGER   fr;

    QueryPerformanceFrequency(&fr);
    return (float_T)tm->QuadPart / (float_T)fr.QuadPart;
#  else
    return (float_T)tm->tv_sec + (float_T)tm->tv_usec / 1000000.0;
#  endif
}
# endif

/*
 * Put the time "msec" past now in "tm".
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_setlimit(long msec, proftime_T *tm)
{
    if (msec <= 0)   /* no limit */
	profile_zero(tm);
    else
    {
# ifdef WIN3264
	LARGE_INTEGER   fr;

	QueryPerformanceCounter(tm);
	QueryPerformanceFrequency(&fr);
	tm->QuadPart += (LONGLONG)((double)msec / 1000.0 * (double)fr.QuadPart);
# else
	long	    usec;

	gettimeofday(tm, NULL);
	usec = (long)tm->tv_usec + (long)msec * 1000;
	tm->tv_usec = usec % 1000000L;
	tm->tv_sec += usec / 1000000L;
# endif
    }
}
 * Return TRUE if the current time is past "tm".
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_passed_limit(proftime_T *tm)
{
    proftime_T	now;

# ifdef WIN3264
    if (tm->QuadPart == 0)  /* timer was not set */
	return FALSE;
    QueryPerformanceCounter(&now);
    return (now.QuadPart > tm->QuadPart);
# else
    if (tm->tv_sec == 0)    /* timer was not set */
	return FALSE;
    gettimeofday(&now, NULL);
    return (now.tv_sec > tm->tv_sec
	    || (now.tv_sec == tm->tv_sec && now.tv_usec > tm->tv_usec));
# endif
}

/*
 * Set the time in "tm" to zero.
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_zero(proftime_T *tm)
{
# ifdef WIN3264
    tm->QuadPart = 0;
# else
    tm->tv_usec = 0;
    tm->tv_sec = 0;
# endif
}

# endif  /* FEAT_PROFILE || FEAT_RELTIME */

Bram Moolenaar's avatar
Bram Moolenaar committed
# if defined(FEAT_TIMERS) || defined(PROTO)
static timer_T	*first_timer = NULL;
Bram Moolenaar's avatar
Bram Moolenaar committed
static long	last_timer_id = 0;
Bram Moolenaar's avatar
Bram Moolenaar committed

proftime_time_left(proftime_T *due, proftime_T *now)
    if (now->QuadPart > due->QuadPart)
	return 0;
    QueryPerformanceFrequency(&fr);
    return (long)(((double)(due->QuadPart - now->QuadPart)
		   / (double)fr.QuadPart) * 1000);
#  else
    if (now->tv_sec > due->tv_sec)
    return (due->tv_sec - now->tv_sec) * 1000
	+ (due->tv_usec - now->tv_usec) / 1000;
Bram Moolenaar's avatar
Bram Moolenaar committed

Bram Moolenaar's avatar
Bram Moolenaar committed
/*
 * Insert a timer in the list of timers.
 */
    static void
insert_timer(timer_T *timer)
{
    timer->tr_next = first_timer;
    timer->tr_prev = NULL;
    if (first_timer != NULL)
	first_timer->tr_prev = timer;
    first_timer = timer;
Bram Moolenaar's avatar
Bram Moolenaar committed
    did_add_timer = TRUE;
Bram Moolenaar's avatar
Bram Moolenaar committed
}

/*
 * Take a timer out of the list of timers.
 */
    static void
remove_timer(timer_T *timer)
{
    if (timer->tr_prev == NULL)
	first_timer = timer->tr_next;
    else
	timer->tr_prev->tr_next = timer->tr_next;
    if (timer->tr_next != NULL)
	timer->tr_next->tr_prev = timer->tr_prev;
}

    static void
free_timer(timer_T *timer)
{
Bram Moolenaar's avatar
Bram Moolenaar committed
    free_callback(timer->tr_callback, timer->tr_partial);
    vim_free(timer);
Bram Moolenaar's avatar
Bram Moolenaar committed
}

/*
 * Create a timer and return it.  NULL if out of memory.
 * Caller should set the callback.
 */
    timer_T *
create_timer(long msec, int repeat)
{
    timer_T	*timer = (timer_T *)alloc_clear(sizeof(timer_T));
Bram Moolenaar's avatar
Bram Moolenaar committed
    long	prev_id = last_timer_id;
Bram Moolenaar's avatar
Bram Moolenaar committed

    if (timer == NULL)
	return NULL;
Bram Moolenaar's avatar
Bram Moolenaar committed
    if (++last_timer_id <= prev_id)
Bram Moolenaar's avatar
Bram Moolenaar committed
	/* Overflow!  Might cause duplicates... */
	last_timer_id = 0;
    timer->tr_id = last_timer_id;
Bram Moolenaar's avatar
Bram Moolenaar committed
    insert_timer(timer);
    if (repeat != 0)
	timer->tr_repeat = repeat - 1;
Bram Moolenaar's avatar
Bram Moolenaar committed
    timer->tr_interval = msec;
Bram Moolenaar's avatar
Bram Moolenaar committed

    profile_setlimit(msec, &timer->tr_due);
    return timer;
}

/*
 * Invoke the callback of "timer".
 */
    static void
timer_callback(timer_T *timer)
{
    typval_T	rettv;
    int		dummy;
    typval_T	argv[2];

    argv[0].v_type = VAR_NUMBER;
Bram Moolenaar's avatar
Bram Moolenaar committed
    argv[0].vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar's avatar
Bram Moolenaar committed
    argv[1].v_type = VAR_UNKNOWN;

    call_func(timer->tr_callback, (int)STRLEN(timer->tr_callback),
Bram Moolenaar's avatar
Bram Moolenaar committed
			&rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar's avatar
Bram Moolenaar committed
			timer->tr_partial, NULL);
    clear_tv(&rettv);
}

/*
 * Call timers that are due.
 * Return the time in msec until the next timer is due.
 * Returns -1 if there are no pending timers.
Bram Moolenaar's avatar
Bram Moolenaar committed
 */
    long
Bram Moolenaar's avatar
Bram Moolenaar committed
check_due_timer(void)
Bram Moolenaar's avatar
Bram Moolenaar committed
{
    timer_T	*timer;
Bram Moolenaar's avatar
Bram Moolenaar committed
    timer_T	*timer_next;
Bram Moolenaar's avatar
Bram Moolenaar committed
    long	this_due;
Bram Moolenaar's avatar
Bram Moolenaar committed
    long	next_due = -1;
Bram Moolenaar's avatar
Bram Moolenaar committed
    proftime_T	now;
    int		did_one = FALSE;
Bram Moolenaar's avatar
Bram Moolenaar committed
    long	current_id = last_timer_id;
Bram Moolenaar's avatar
Bram Moolenaar committed

    /* Don't run any timers while exiting or dealing with an error. */
    if (exiting || aborting())
Bram Moolenaar's avatar
Bram Moolenaar committed
    profile_start(&now);
    for (timer = first_timer; timer != NULL && !got_int; timer = timer_next)
Bram Moolenaar's avatar
Bram Moolenaar committed
    {
Bram Moolenaar's avatar
Bram Moolenaar committed
	timer_next = timer->tr_next;

	if (timer->tr_id == -1 || timer->tr_firing || timer->tr_paused)
	    continue;
	this_due = proftime_time_left(&timer->tr_due, &now);
Bram Moolenaar's avatar
Bram Moolenaar committed
	if (this_due <= 1)
Bram Moolenaar's avatar
Bram Moolenaar committed
	{
	    int save_timer_busy = timer_busy;
	    int save_vgetc_busy = vgetc_busy;
	    int save_did_emsg = did_emsg;
	    int save_called_emsg = called_emsg;
	    int	save_must_redraw = must_redraw;
	    int	save_trylevel = trylevel;
	    int save_did_throw = did_throw;
	    int save_ex_pressedreturn = get_pressedreturn();
	    except_T *save_current_exception = current_exception;
	    /* Create a scope for running the timer callback, ignoring most of
	     * the current scope, such as being inside a try/catch. */
	    timer_busy = timer_busy > 0 || vgetc_busy > 0;
	    vgetc_busy = 0;
	    did_emsg = FALSE;
	    did_uncaught_emsg = FALSE;
	    trylevel = 0;
	    did_throw = FALSE;
	    current_exception = NULL;

Bram Moolenaar's avatar
Bram Moolenaar committed
	    timer->tr_firing = TRUE;
	    timer_callback(timer);
	    timer->tr_firing = FALSE;
Bram Moolenaar's avatar
Bram Moolenaar committed
	    timer_next = timer->tr_next;
	    did_one = TRUE;
	    timer_busy = save_timer_busy;
	    vgetc_busy = save_vgetc_busy;
	    did_emsg = save_did_emsg;
	    called_emsg = save_called_emsg;
	    trylevel = save_trylevel;
	    did_throw = save_did_throw;
	    current_exception = save_current_exception;
	    if (must_redraw != 0)
		need_update_screen = TRUE;
	    must_redraw = must_redraw > save_must_redraw
					      ? must_redraw : save_must_redraw;
	    set_pressedreturn(save_ex_pressedreturn);
Bram Moolenaar's avatar
Bram Moolenaar committed

	    /* Only fire the timer again if it repeats and stop_timer() wasn't
	     * called while inside the callback (tr_id == -1). */
	    if (timer->tr_repeat != 0 && timer->tr_id != -1
		    && timer->tr_emsg_count < 3)
Bram Moolenaar's avatar
Bram Moolenaar committed
	    {
Bram Moolenaar's avatar
Bram Moolenaar committed
		profile_setlimit(timer->tr_interval, &timer->tr_due);
		this_due = proftime_time_left(&timer->tr_due, &now);
Bram Moolenaar's avatar
Bram Moolenaar committed
		if (this_due < 1)
		    this_due = 1;
		if (timer->tr_repeat > 0)
		    --timer->tr_repeat;
	    }
	    else
	    {
		this_due = -1;
		remove_timer(timer);
		free_timer(timer);
Bram Moolenaar's avatar
Bram Moolenaar committed
	    }
	}
Bram Moolenaar's avatar
Bram Moolenaar committed
	if (this_due > 0 && (next_due == -1 || next_due > this_due))
	    next_due = this_due;
Bram Moolenaar's avatar
Bram Moolenaar committed
    }

    if (did_one)
	redraw_after_callback(need_update_screen);
Bram Moolenaar's avatar
Bram Moolenaar committed

    if (bevalexpr_due_set)
    {
	this_due = proftime_time_left(&bevalexpr_due, &now);
	if (this_due <= 1)
	{
	    bevalexpr_due_set = FALSE;
	    if (balloonEval == NULL)
	    {
		balloonEval = (BalloonEval *)alloc(sizeof(BalloonEval));
		balloonEvalForTerm = TRUE;
	    }
	    if (balloonEval != NULL)
		general_beval_cb(balloonEval, 0);
	}
	else if (this_due > 0 && (next_due == -1 || next_due > this_due))
	    next_due = this_due;
    }
#endif

Bram Moolenaar's avatar
Bram Moolenaar committed
    return current_id != last_timer_id ? 1 : next_due;
Bram Moolenaar's avatar
Bram Moolenaar committed
}

/*
 * Find a timer by ID.  Returns NULL if not found;
 */
    timer_T *
Bram Moolenaar's avatar
Bram Moolenaar committed
find_timer(long id)
Bram Moolenaar's avatar
Bram Moolenaar committed
{
    timer_T *timer;

Bram Moolenaar's avatar
Bram Moolenaar committed
    if (id >= 0)
    {
	for (timer = first_timer; timer != NULL; timer = timer->tr_next)
	    if (timer->tr_id == id)
		return timer;
    }
    return NULL;
Bram Moolenaar's avatar
Bram Moolenaar committed
}


/*
 * Stop a timer and delete it.
 */
    void
stop_timer(timer_T *timer)
{
Bram Moolenaar's avatar
Bram Moolenaar committed
    if (timer->tr_firing)
	/* Free the timer after the callback returns. */
	timer->tr_id = -1;
    else
    {
	remove_timer(timer);
	free_timer(timer);
    }
Bram Moolenaar's avatar
Bram Moolenaar committed
}
Bram Moolenaar's avatar
Bram Moolenaar committed

Bram Moolenaar's avatar
Bram Moolenaar committed
    void
stop_all_timers(void)
{
Bram Moolenaar's avatar
Bram Moolenaar committed
    timer_T *timer;
    timer_T *timer_next;

    for (timer = first_timer; timer != NULL; timer = timer_next)
    {
	timer_next = timer->tr_next;
	stop_timer(timer);
    }
Bram Moolenaar's avatar
Bram Moolenaar committed
    void
add_timer_info(typval_T *rettv, timer_T *timer)
{
    list_T	*list = rettv->vval.v_list;
    dict_T	*dict = dict_alloc();
    dictitem_T	*di;
    long	remaining;
    proftime_T	now;

    if (dict == NULL)
	return;
    list_append_dict(list, dict);

Bram Moolenaar's avatar
Bram Moolenaar committed
    dict_add_nr_str(dict, "id", timer->tr_id, NULL);
Bram Moolenaar's avatar
Bram Moolenaar committed
    dict_add_nr_str(dict, "time", (long)timer->tr_interval, NULL);

    profile_start(&now);
    remaining = proftime_time_left(&timer->tr_due, &now);
Bram Moolenaar's avatar
Bram Moolenaar committed
    dict_add_nr_str(dict, "remaining", (long)remaining, NULL);

    dict_add_nr_str(dict, "repeat",
	       (long)(timer->tr_repeat < 0 ? -1 : timer->tr_repeat + 1), NULL);
Bram Moolenaar's avatar
Bram Moolenaar committed
    dict_add_nr_str(dict, "paused", (long)(timer->tr_paused), NULL);
Bram Moolenaar's avatar
Bram Moolenaar committed

    di = dictitem_alloc((char_u *)"callback");
    if (di != NULL)
    {
	if (dict_add(dict, di) == FAIL)
	    vim_free(di);
	else if (timer->tr_partial != NULL)
	{
	    di->di_tv.v_type = VAR_PARTIAL;
	    di->di_tv.vval.v_partial = timer->tr_partial;
	    ++timer->tr_partial->pt_refcount;
	}
	else
	{
	    di->di_tv.v_type = VAR_FUNC;
	    di->di_tv.vval.v_string = vim_strsave(timer->tr_callback);
	}
	di->di_tv.v_lock = 0;
    }
}

    void
add_timer_info_all(typval_T *rettv)
{
    timer_T *timer;

    for (timer = first_timer; timer != NULL; timer = timer->tr_next)
Bram Moolenaar's avatar
Bram Moolenaar committed
	if (timer->tr_id != -1)
	    add_timer_info(rettv, timer);
Bram Moolenaar's avatar
Bram Moolenaar committed
/*
 * Mark references in partials of timers.
 */
    int
set_ref_in_timer(int copyID)
{
    int		abort = FALSE;
    timer_T	*timer;
    typval_T	tv;

    for (timer = first_timer; timer != NULL; timer = timer->tr_next)
    {
Bram Moolenaar's avatar
Bram Moolenaar committed
	if (timer->tr_partial != NULL)
	{
	    tv.v_type = VAR_PARTIAL;
	    tv.vval.v_partial = timer->tr_partial;
	}
	else
	{
	    tv.v_type = VAR_FUNC;
	    tv.vval.v_string = timer->tr_callback;
	}
Bram Moolenaar's avatar
Bram Moolenaar committed
	abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
    }
    return abort;
}
Bram Moolenaar's avatar
Bram Moolenaar committed

#  if defined(EXITFREE) || defined(PROTO)
    void
timer_free_all()
{
    timer_T *timer;

    while (first_timer != NULL)
    {
	timer = first_timer;
	remove_timer(timer);
	free_timer(timer);
    }
}
#  endif
Bram Moolenaar's avatar
Bram Moolenaar committed
# endif

#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME) && defined(FEAT_FLOAT)
# if defined(HAVE_MATH_H)
#  include <math.h>
# endif

/*
 * Divide the time "tm" by "count" and store in "tm2".
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_divide(proftime_T *tm, int count, proftime_T *tm2)
{
    if (count == 0)
	profile_zero(tm2);
    else
    {
# ifdef WIN3264
	tm2->QuadPart = tm->QuadPart / count;
# else
	double usec = (tm->tv_sec * 1000000.0 + tm->tv_usec) / count;

	tm2->tv_sec = floor(usec / 1000000.0);
	tm2->tv_usec = vim_round(usec - (tm2->tv_sec * 1000000.0));
# if defined(FEAT_PROFILE) || defined(PROTO)
/*
 * Functions for profiling.
 */
Bram Moolenaar's avatar
Bram Moolenaar committed
static void script_do_profile(scriptitem_T *si);
static void script_dump_profile(FILE *fd);
static proftime_T prof_wait_time;

/*
 * Add the time "tm2" to "tm".
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_add(proftime_T *tm, proftime_T *tm2)
# ifdef WIN3264
    tm->QuadPart += tm2->QuadPart;
# else
    tm->tv_usec += tm2->tv_usec;
    tm->tv_sec += tm2->tv_sec;
    if (tm->tv_usec >= 1000000)
    {
	tm->tv_usec -= 1000000;
	++tm->tv_sec;
    }
# endif
/*
 * Add the "self" time from the total time and the children's time.
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_self(proftime_T *self, proftime_T *total, proftime_T *children)
{
    /* Check that the result won't be negative.  Can happen with recursive
     * calls. */
#ifdef WIN3264
    if (total->QuadPart <= children->QuadPart)
	return;
#else
    if (total->tv_sec < children->tv_sec
	    || (total->tv_sec == children->tv_sec
		&& total->tv_usec <= children->tv_usec))
	return;
#endif
    profile_add(self, total);
    profile_sub(self, children);
}

/*
 * Get the current waittime.
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_get_wait(proftime_T *tm)
{
    *tm = prof_wait_time;
}

/*
 * Subtract the passed waittime since "tm" from "tma".
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_sub_wait(proftime_T *tm, proftime_T *tma)
{
    proftime_T tm3 = prof_wait_time;

    profile_sub(&tm3, tm);
    profile_sub(tma, &tm3);
}

/*
 * Return TRUE if "tm1" and "tm2" are equal.
 */
    int
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_equal(proftime_T *tm1, proftime_T *tm2)
# ifdef WIN3264
    return (tm1->QuadPart == tm2->QuadPart);
# else
    return (tm1->tv_usec == tm2->tv_usec && tm1->tv_sec == tm2->tv_sec);
# endif
}

/*
 * Return <0, 0 or >0 if "tm1" < "tm2", "tm1" == "tm2" or "tm1" > "tm2"
 */
    int
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_cmp(const proftime_T *tm1, const proftime_T *tm2)
{
# ifdef WIN3264
    return (int)(tm2->QuadPart - tm1->QuadPart);
# else
    if (tm1->tv_sec == tm2->tv_sec)
	return tm2->tv_usec - tm1->tv_usec;
    return tm2->tv_sec - tm1->tv_sec;
# endif
}

static char_u	*profile_fname = NULL;
static proftime_T pause_time;

/*
 * ":profile cmd args"
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
ex_profile(exarg_T *eap)
{
    char_u	*e;
    int		len;

    e = skiptowhite(eap->arg);
    len = (int)(e - eap->arg);
    e = skipwhite(e);

    if (len == 5 && STRNCMP(eap->arg, "start", 5) == 0 && *e != NUL)
    {
	vim_free(profile_fname);
Bram Moolenaar's avatar
Bram Moolenaar committed
	profile_fname = expand_env_save_opt(e, TRUE);
	do_profiling = PROF_YES;
	profile_zero(&prof_wait_time);
	set_vim_var_nr(VV_PROFILING, 1L);
    }
    else if (do_profiling == PROF_NONE)
	EMSG(_("E750: First use \":profile start {fname}\""));
    else if (STRCMP(eap->arg, "pause") == 0)
    {
	if (do_profiling == PROF_YES)
	    profile_start(&pause_time);
	do_profiling = PROF_PAUSED;
    }
    else if (STRCMP(eap->arg, "continue") == 0)
    {
	if (do_profiling == PROF_PAUSED)
	{
	    profile_end(&pause_time);
	    profile_add(&prof_wait_time, &pause_time);
	}
	do_profiling = PROF_YES;
    }
    else
    {
	/* The rest is similar to ":breakadd". */
	ex_breakadd(eap);
    }
}

/* Command line expansion for :profile. */
static enum
{
    PEXP_SUBCMD,	/* expand :profile sub-commands */
    PEXP_FUNC		/* expand :profile func {funcname} */
} pexpand_what;

static char *pexpand_cmds[] = {
			"start",
#define PROFCMD_START	0
			"pause",
#define PROFCMD_PAUSE	1
			"continue",
#define PROFCMD_CONTINUE 2
			"func",
#define PROFCMD_FUNC	3
			"file",
#define PROFCMD_FILE	4
			NULL
#define PROFCMD_LAST	5
};

/*
 * Function given to ExpandGeneric() to obtain the profile command
 * specific expansion.
 */
    char_u *
Bram Moolenaar's avatar
Bram Moolenaar committed
get_profile_name(expand_T *xp UNUSED, int idx)
{
    switch (pexpand_what)
    {
    case PEXP_SUBCMD:
	return (char_u *)pexpand_cmds[idx];
    /* case PEXP_FUNC: TODO */
    default:
	return NULL;
    }
}

/*
 * Handle command line completion for :profile command.
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
set_context_in_profile_cmd(expand_T *xp, char_u *arg)
{
    char_u	*end_subcmd;

    /* Default: expand subcommands. */
    xp->xp_context = EXPAND_PROFILE;
    pexpand_what = PEXP_SUBCMD;
    xp->xp_pattern = arg;

    end_subcmd = skiptowhite(arg);
    if (*end_subcmd == NUL)
	return;

    if (end_subcmd - arg == 5 && STRNCMP(arg, "start", 5) == 0)
    {
	xp->xp_context = EXPAND_FILES;
	xp->xp_pattern = skipwhite(end_subcmd);
	return;
    }

    /* TODO: expand function names after "func" */
    xp->xp_context = EXPAND_NOTHING;
}

/*
 * Dump the profiling info.
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
profile_dump(void)
{
    FILE	*fd;

    if (profile_fname != NULL)
    {
	fd = mch_fopen((char *)profile_fname, "w");
	if (fd == NULL)
	    EMSG2(_(e_notopen), profile_fname);
	else
	{
	    script_dump_profile(fd);
	    func_dump_profile(fd);
	    fclose(fd);
	}
    }
}

/*
 * Start profiling script "fp".
 */
    static void
Bram Moolenaar's avatar
Bram Moolenaar committed
script_do_profile(scriptitem_T *si)
{
    si->sn_pr_count = 0;
    profile_zero(&si->sn_pr_total);
    profile_zero(&si->sn_pr_self);

    ga_init2(&si->sn_prl_ga, sizeof(sn_prl_T), 100);
    si->sn_prl_idx = -1;
    si->sn_prof_on = TRUE;
    si->sn_pr_nest = 0;
}

/*
 * Save time when starting to invoke another script or function.
Bram Moolenaar's avatar
Bram Moolenaar committed
script_prof_save(
    proftime_T	*tm)	    /* place to store wait time */
{
    scriptitem_T    *si;

    if (current_SID > 0 && current_SID <= script_items.ga_len)
    {
	si = &SCRIPT_ITEM(current_SID);
	if (si->sn_prof_on && si->sn_pr_nest++ == 0)
	    profile_start(&si->sn_pr_child);
    }
    profile_get_wait(tm);
}

/*
 * Count time spent in children after invoking another script or function.
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
script_prof_restore(proftime_T *tm)
{
    scriptitem_T    *si;

    if (current_SID > 0 && current_SID <= script_items.ga_len)
    {
	si = &SCRIPT_ITEM(current_SID);
	if (si->sn_prof_on && --si->sn_pr_nest == 0)
	{
	    profile_end(&si->sn_pr_child);
	    profile_sub_wait(tm, &si->sn_pr_child); /* don't count wait time */
	    profile_add(&si->sn_pr_children, &si->sn_pr_child);
	    profile_add(&si->sn_prl_children, &si->sn_pr_child);
	}
    }
}

static proftime_T inchar_time;

/*
 * Called when starting to wait for the user to type a character.
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
prof_inchar_enter(void)
{
    profile_start(&inchar_time);
}

/*
 * Called when finished waiting for the user to type a character.
 */
    void
Bram Moolenaar's avatar
Bram Moolenaar committed
prof_inchar_exit(void)
{
    profile_end(&inchar_time);
    profile_add(&prof_wait_time, &inchar_time);
}

/*
 * Dump the profiling results for all scripts in file "fd".
 */
    static void
Bram Moolenaar's avatar
Bram Moolenaar committed
script_dump_profile(FILE *fd)
{
    int		    id;
    scriptitem_T    *si;
    int		    i;
    FILE	    *sfd;
    sn_prl_T	    *pp;

    for (id = 1; id <= script_items.ga_len; ++id)
    {
	si = &SCRIPT_ITEM(id);
	if (si->sn_prof_on)
	{
	    fprintf(fd, "SCRIPT  %s\n", si->sn_name);
	    if (si->sn_pr_count == 1)
		fprintf(fd, "Sourced 1 time\n");
	    else
		fprintf(fd, "Sourced %d times\n", si->sn_pr_count);
	    fprintf(fd, "Total time: %s\n", profile_msg(&si->sn_pr_total));
	    fprintf(fd, " Self time: %s\n", profile_msg(&si->sn_pr_self));
	    fprintf(fd, "\n");
	    fprintf(fd, "count  total (s)   self (s)\n");

	    sfd = mch_fopen((char *)si->sn_name, "r");
	    if (sfd == NULL)
		fprintf(fd, "Cannot open file!\n");
	    else
	    {
		/* Keep going till the end of file, so that trailing
		 * continuation lines are listed. */
		for (i = 0; ; ++i)
		{
		    if (vim_fgets(IObuff, IOSIZE, sfd))
			break;
		    /* When a line has been truncated, append NL, taking care
		     * of multi-byte characters . */
		    if (IObuff[IOSIZE - 2] != NUL && IObuff[IOSIZE - 2] != NL)
		    {
			int n = IOSIZE - 2;
# ifdef FEAT_MBYTE
			if (enc_utf8)
			{
			    /* Move to the first byte of this char.
			     * utf_head_off() doesn't work, because it checks
			     * for a truncated character. */
			    while (n > 0 && (IObuff[n] & 0xc0) == 0x80)
				--n;
			}
			else if (has_mbyte)
			    n -= mb_head_off(IObuff, IObuff + n);
# endif
			IObuff[n] = NL;
			IObuff[n + 1] = NUL;
		    }
		    if (i < si->sn_prl_ga.ga_len
				     && (pp = &PRL_ITEM(si, i))->snp_count > 0)
		    {
			fprintf(fd, "%5d ", pp->snp_count);
			if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self))
			    fprintf(fd, "           ");
			else
			    fprintf(fd, "%s ", profile_msg(&pp->sn_prl_total));
			fprintf(fd, "%s ", profile_msg(&pp->sn_prl_self));
		    }
		    else
			fprintf(fd, "                            ");
		    fprintf(fd, "%s", IObuff);
		}
		fclose(sfd);
	    }
	    fprintf(fd, "\n");
	}
    }
}

/*
 * Return TRUE when a function defined in the current script should be
 * profiled.
 */
    int
Bram Moolenaar's avatar
Bram Moolenaar committed
prof_def_func(void)
    if (current_SID > 0)
	return SCRIPT_ITEM(current_SID).sn_pr_force;
    return FALSE;