Newer
Older
v = eval_expr(bp->dbg_name, NULL);
debug_newval = typval_tostring(v);
free_tv(bp->dbg_val);
bp->dbg_val = v;
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
}
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
{
/* 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
# ifdef WIN3264
QueryPerformanceCounter(tm);
# else
}
/*
* Compute the elapsed time from "tm" till now and store in "tm".
*/
void
# 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;
}
}
/*
* Subtract the time "tm2" from "tm".
*/
void
# 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;
}
/*
* Return a string that represents the time in "tm".
* Uses a static buffer!
*/
char *
{
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);
# 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
{
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".
{
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
{
# ifdef WIN3264
tm->QuadPart = 0;
# else
tm->tv_usec = 0;
tm->tv_sec = 0;
# endif
}
# endif /* FEAT_PROFILE || FEAT_RELTIME */
# if defined(FEAT_TIMERS) || defined(PROTO)
static timer_T *first_timer = NULL;
static long
proftime_time_left(proftime_T *due, proftime_T *now)
{
# ifdef WIN3264
LARGE_INTEGER fr;
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 0;
return (due->tv_sec - now->tv_sec) * 1000
+ (due->tv_usec - now->tv_usec) / 1000;
# endif
}
/*
* 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;
}
/*
* 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)
{
free_callback(timer->tr_callback, timer->tr_partial);
vim_free(timer);
}
/*
* 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));
/* Overflow! Might cause duplicates... */
last_timer_id = 0;
timer->tr_id = last_timer_id;
insert_timer(timer);
if (repeat != 0)
timer->tr_repeat = repeat - 1;
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;
argv[1].v_type = VAR_UNKNOWN;
call_func(timer->tr_callback, (int)STRLEN(timer->tr_callback),
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.
int need_update_screen = FALSE;
/* Don't run any timers while exiting or dealing with an error. */
if (exiting || aborting())
return next_due;
profile_start(&now);
for (timer = first_timer; timer != NULL && !got_int; timer = timer_next)
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);
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;
called_emsg = FALSE;
did_emsg = FALSE;
did_uncaught_emsg = FALSE;
must_redraw = 0;
trylevel = 0;
did_throw = FALSE;
current_exception = NULL;
timer->tr_firing = TRUE;
timer_callback(timer);
timer->tr_firing = FALSE;
timer_busy = save_timer_busy;
vgetc_busy = save_vgetc_busy;
if (did_uncaught_emsg)
++timer->tr_emsg_count;
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);
/* 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)
this_due = proftime_time_left(&timer->tr_due, &now);
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);
if (this_due > 0 && (next_due == -1 || next_due > this_due))
next_due = this_due;
redraw_after_callback(need_update_screen);
Bram Moolenaar
committed
#ifdef FEAT_BEVAL_TERM
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
}
/*
* Find a timer by ID. Returns NULL if not found;
*/
timer_T *
if (id >= 0)
{
for (timer = first_timer; timer != NULL; timer = timer->tr_next)
if (timer->tr_id == id)
return timer;
}
return NULL;
}
/*
* Stop a timer and delete it.
*/
void
stop_timer(timer_T *timer)
{
if (timer->tr_firing)
/* Free the timer after the callback returns. */
timer->tr_id = -1;
else
{
remove_timer(timer);
free_timer(timer);
}
timer_T *timer;
timer_T *timer_next;
for (timer = first_timer; timer != NULL; timer = timer_next)
{
timer_next = timer->tr_next;
stop_timer(timer);
}
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);
dict_add_nr_str(dict, "time", (long)timer->tr_interval, NULL);
profile_start(&now);
remaining = proftime_time_left(&timer->tr_due, &now);
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);
dict_add_nr_str(dict, "paused", (long)(timer->tr_paused), NULL);
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
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)
if (timer->tr_id != -1)
add_timer_info(rettv, timer);
/*
* 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)
{
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;
}
abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
}
return abort;
}
# 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
#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
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.
*/
static void script_do_profile(scriptitem_T *si);
static void script_dump_profile(FILE *fd);
/*
* Add the time "tm2" to "tm".
*/
void
# 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;
}
/*
* Add the "self" time from the total time and the children's time.
*/
void
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
{
*tm = prof_wait_time;
}
/*
* Subtract the passed waittime since "tm" from "tma".
*/
void
{
proftime_T tm3 = prof_wait_time;
profile_sub(&tm3, tm);
profile_sub(tma, &tm3);
}
/*
* Return TRUE if "tm1" and "tm2" are equal.
*/
int
# 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
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;
/*
* ":profile cmd args"
*/
void
{
char_u *e;
int len;
e = skiptowhite(eap->arg);
e = skipwhite(e);
if (len == 5 && STRNCMP(eap->arg, "start", 5) == 0 && *e != NUL)
{
vim_free(profile_fname);
profile_zero(&prof_wait_time);
set_vim_var_nr(VV_PROFILING, 1L);
}
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} */
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
} 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 *
{
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
{
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
{
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);
fclose(fd);
}
}
}
/*
* Start profiling script "fp".
*/
static void
{
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.
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
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
{
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
{
profile_start(&inchar_time);
}
/*
* Called when finished waiting for the user to type a character.
*/
void
{
profile_end(&inchar_time);
profile_add(&prof_wait_time, &inchar_time);
}
/*
* Dump the profiling results for all scripts in file "fd".
*/
static void
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
{
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");
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)
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
{
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
if (current_SID > 0)
return SCRIPT_ITEM(current_SID).sn_pr_force;
return FALSE;