xref: /netbsd-src/external/gpl3/binutils/dist/gprofng/src/UserLabel.cc (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1*cb63e24eSchristos /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
24f645668Schristos    Contributed by Oracle.
34f645668Schristos 
44f645668Schristos    This file is part of GNU Binutils.
54f645668Schristos 
64f645668Schristos    This program is free software; you can redistribute it and/or modify
74f645668Schristos    it under the terms of the GNU General Public License as published by
84f645668Schristos    the Free Software Foundation; either version 3, or (at your option)
94f645668Schristos    any later version.
104f645668Schristos 
114f645668Schristos    This program is distributed in the hope that it will be useful,
124f645668Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
134f645668Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
144f645668Schristos    GNU General Public License for more details.
154f645668Schristos 
164f645668Schristos    You should have received a copy of the GNU General Public License
174f645668Schristos    along with this program; if not, write to the Free Software
184f645668Schristos    Foundation, 51 Franklin Street - Fifth Floor, Boston,
194f645668Schristos    MA 02110-1301, USA.  */
204f645668Schristos 
214f645668Schristos #include "config.h"
224f645668Schristos #include <time.h>
234f645668Schristos 
244f645668Schristos #include "DbeSession.h"
254f645668Schristos #include "Expression.h"
264f645668Schristos #include "StringBuilder.h"
274f645668Schristos #include "util.h"
284f645668Schristos #include "UserLabel.h"
294f645668Schristos #include "debug.h"
304f645668Schristos 
314f645668Schristos int UserLabel::last_id = 0;
324f645668Schristos 
UserLabel(char * _name)334f645668Schristos UserLabel::UserLabel (char *_name)
344f645668Schristos {
354f645668Schristos   name = dbe_strdup (_name);
364f645668Schristos   comment = str_expr = all_times = hostname = NULL;
374f645668Schristos   start_f = stop_f = false;
384f645668Schristos   expr = NULL;
394f645668Schristos   start_tv.tv_sec = 0;
404f645668Schristos   start_tv.tv_usec = 0;
414f645668Schristos   atime = timeStart = timeStop = start_sec = start_hrtime = 0;
424f645668Schristos   relative = REL_TIME;
434f645668Schristos   id = ++last_id;
444f645668Schristos }
454f645668Schristos 
~UserLabel()464f645668Schristos UserLabel::~UserLabel ()
474f645668Schristos {
484f645668Schristos   free (name);
494f645668Schristos   free (comment);
504f645668Schristos   free (all_times);
514f645668Schristos   free (hostname);
524f645668Schristos   free (str_expr);
534f645668Schristos   delete expr;
544f645668Schristos }
554f645668Schristos 
564f645668Schristos void
gen_expr()574f645668Schristos UserLabel::gen_expr ()
584f645668Schristos {
594f645668Schristos   if (!start_f && !stop_f)
604f645668Schristos     return;
614f645668Schristos   StringBuilder sb;
624f645668Schristos   sb.append ('(');
634f645668Schristos   if (str_expr)
644f645668Schristos     {
654f645668Schristos       sb.append (str_expr);
664f645668Schristos       sb.append (NTXT (" || ("));
674f645668Schristos     }
684f645668Schristos   if (start_f)
694f645668Schristos     {
704f645668Schristos       sb.append (NTXT ("TSTAMP"));
714f645668Schristos       sb.append (NTXT (">="));
724f645668Schristos       sb.append (timeStart);
734f645668Schristos       if (stop_f)
744f645668Schristos 	{
754f645668Schristos 	  sb.append (NTXT (" && "));
764f645668Schristos 	}
774f645668Schristos     }
784f645668Schristos   if (stop_f)
794f645668Schristos     {
804f645668Schristos       sb.append (NTXT ("TSTAMP"));
814f645668Schristos       sb.append ('<');
824f645668Schristos       sb.append (timeStop);
834f645668Schristos     }
844f645668Schristos   sb.append (')');
854f645668Schristos   if (str_expr)
864f645668Schristos     {
874f645668Schristos       sb.append (')');
884f645668Schristos       delete str_expr;
894f645668Schristos     }
904f645668Schristos   str_expr = sb.toString ();
914f645668Schristos   start_f = stop_f = false;
924f645668Schristos }
934f645668Schristos 
944f645668Schristos void
register_user_label(int groupId)954f645668Schristos UserLabel::register_user_label (int groupId)
964f645668Schristos {
974f645668Schristos   gen_expr ();
984f645668Schristos   if (str_expr)
994f645668Schristos     {
1004f645668Schristos       char *old_str = str_expr;
1014f645668Schristos       str_expr = dbe_sprintf (NTXT ("(EXPGRID==%d && %s)"), groupId, old_str);
1024f645668Schristos       delete old_str;
1034f645668Schristos       UserLabel *ulbl = dbeSession->findUserLabel (name);
1044f645668Schristos       if (ulbl)
1054f645668Schristos 	{
1064f645668Schristos 	  old_str = ulbl->str_expr;
1074f645668Schristos 	  ulbl->str_expr = dbe_sprintf (NTXT ("(%s || %s)"), old_str, str_expr);
1084f645668Schristos 	  delete old_str;
1094f645668Schristos 	  if (comment)
1104f645668Schristos 	    {
1114f645668Schristos 	      if (ulbl->comment)
1124f645668Schristos 		{
1134f645668Schristos 		  old_str = ulbl->comment;
1144f645668Schristos 		  ulbl->comment = dbe_sprintf (NTXT ("%s; %s"), old_str, comment);
1154f645668Schristos 		  delete old_str;
1164f645668Schristos 		}
1174f645668Schristos 	      else
1184f645668Schristos 		ulbl->comment = dbe_strdup (comment);
1194f645668Schristos 	    }
1204f645668Schristos 	  delete ulbl->expr;
1214f645668Schristos 	  ulbl->expr = dbeSession->ql_parse (ulbl->str_expr);
1224f645668Schristos 	}
1234f645668Schristos       else
1244f645668Schristos 	{
1254f645668Schristos 	  expr = dbeSession->ql_parse (str_expr);
1264f645668Schristos 	  dbeSession->append (this);
1274f645668Schristos 	}
1284f645668Schristos     }
1294f645668Schristos }
1304f645668Schristos 
1314f645668Schristos char *
dump()1324f645668Schristos UserLabel::dump ()
1334f645668Schristos {
1344f645668Schristos   StringBuilder sb;
1354f645668Schristos   sb.append (name);
1364f645668Schristos   if (str_expr)
1374f645668Schristos     {
1384f645668Schristos       sb.append (NTXT ("  str_expr='"));
1394f645668Schristos       sb.append (str_expr);
1404f645668Schristos       sb.append ('\'');
1414f645668Schristos     }
1424f645668Schristos   if (all_times)
1434f645668Schristos     {
1444f645668Schristos       sb.append (NTXT (" atime="));
1454f645668Schristos       sb.append ((unsigned int) (atime / NANOSEC));
1464f645668Schristos       sb.append ('.');
1474f645668Schristos       char buf[128];
1484f645668Schristos       snprintf (buf, sizeof (buf), NTXT ("%09llu"), (unsigned long long) (atime % NANOSEC));
1494f645668Schristos       sb.append (buf);
1504f645668Schristos       sb.append (NTXT ("  all_times='"));
1514f645668Schristos       sb.append (all_times);
1524f645668Schristos       sb.append ('\'');
1534f645668Schristos     }
1544f645668Schristos   if (comment)
1554f645668Schristos     {
1564f645668Schristos       sb.append (NTXT ("  comment='"));
1574f645668Schristos       sb.append (comment);
1584f645668Schristos       sb.append ('\'');
1594f645668Schristos     }
1604f645668Schristos   return sb.toString ();
1614f645668Schristos }
1624f645668Schristos 
1634f645668Schristos void
dump(const char * msg,Vector<UserLabel * > * labels)1644f645668Schristos UserLabel::dump (const char *msg, Vector<UserLabel*> *labels)
1654f645668Schristos {
1664f645668Schristos   if (!DUMP_USER_LABELS)
1674f645668Schristos     return;
1684f645668Schristos   if (msg)
1694f645668Schristos     fprintf (stderr, NTXT ("%s\n"), msg);
1704f645668Schristos   for (int i = 0, sz = labels ? labels->size () : 0; i < sz; i++)
1714f645668Schristos     {
1724f645668Schristos       UserLabel *lbl = labels->fetch (i);
1734f645668Schristos       char *s = lbl->dump ();
1744f645668Schristos       fprintf (stderr, NTXT ("%2d %s\n"), i, s);
1754f645668Schristos       delete s;
1764f645668Schristos     }
1774f645668Schristos }
178