citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit c92740bc4d01b80c9c7d9f6a370f33311f029367
parent c56821398340680c57e1b3b8bba236b21233631e
Author: Kyle Milz <kyle@0x30.net>
Date:   Wed, 20 Jul 2016 18:13:16 -0600

src: add citrun-term

- split out protocol parts from gl_runtime_conn into runtime_conn
- add new ncurses terminal viewer wip

Diffstat:
Msrc/Jamfile | 16++++++++++++++--
Msrc/runtime_conn.cc | 139+++++++++++++++++++++++++++----------------------------------------------------
Msrc/runtime_conn.h | 28++++++++++------------------
Asrc/term_main.cc | 41+++++++++++++++++++++++++++++++++++++++++
4 files changed, 113 insertions(+), 111 deletions(-)

diff --git a/src/Jamfile b/src/Jamfile @@ -7,12 +7,24 @@ MkWrap citrun-wrap : wrap.in ; InstallShell $(PREFIX)/bin : citrun-wrap ; # +# utils.a +# +ObjectC++Flags af_unix.cc runtime_conn.cc : -std=c++11 ; +Library utils : af_unix.cc runtime_conn.cc ; + +# +# citrun-term +# +LINKLIBS on citrun-term += -lcurses -L/usr/local/lib -lestdc++ ; + +LinkLibraries citrun-term : utils ; +Main citrun-term : term_main.cc ; + +# # citrun-gl # GL_SRCS = gl_main.cc - gl_runtime_conn.cc - af_unix.cc gl_view.cc demo-atlas.cc gl_buffer.cc diff --git a/src/runtime_conn.cc b/src/runtime_conn.cc @@ -1,121 +1,78 @@ -#include <err.h> - +// +// Copyright (c) 2016 Kyle Milz <kyle@0x30.net> +// +// Permission to use, copy, modify, and distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// #include <cassert> +#include <err.h> #include <iostream> #include <fstream> -#include <sstream> -#include <vector> -#include "gl_runtime_conn.h" +#include "runtime_conn.h" -RuntimeProcess::RuntimeProcess(af_unix *sock, demo_buffer_t *buf, demo_font_t *f) : - socket(sock), - buffer(buf), - font(f) +RuntimeProcess::RuntimeProcess(af_unix &sock) : + socket(sock) { uint64_t sz; - socket->read_all(sz); - program_name.resize(sz); - socket->read_all((uint8_t *)&program_name[0], sz); - uint64_t num_tus; - socket->read_all(num_tus); - translation_units.resize(num_tus); - - socket->read_all(lines_total); - + uint64_t file_name_sz; assert(sizeof(pid_t) == 4); - socket->read_all(process_id); - socket->read_all(parent_process_id); - socket->read_all(process_group); - - std::stringstream ss; - ss << "program name:\t" << program_name << std::endl; - ss << "code lines:\t" << lines_total << std::endl; - ss << "trnsltn units:\t" << num_tus << std::endl; - ss << "process id:\t" << process_id << std::endl; - ss << "parent pid:\t" << parent_process_id << std::endl; - ss << "process group:\t" << process_group << std::endl; - - glyphy_point_t cur_pos = { 0, 0 }; - demo_buffer_move_to(buffer, &cur_pos); - - demo_buffer_add_text(buffer, ss.str().c_str(), font, 2); - - demo_buffer_current_point(buffer, &cur_pos); - double y_margin = cur_pos.y; - - cur_pos.x = 0; - for (auto &current_unit : translation_units) { - cur_pos.y = y_margin; - - uint64_t file_name_sz; - socket->read_all(file_name_sz); - - current_unit.file_name.resize(file_name_sz); - socket->read_all((uint8_t *)&current_unit.file_name[0], file_name_sz); - - read_file(current_unit.file_name, cur_pos); - cur_pos.x += 50; - socket->read_all(current_unit.num_lines); - current_unit.execution_counts.resize(current_unit.num_lines, 0); + socket.read_all(sz); + program_name.resize(sz); + socket.read_all((uint8_t *)&program_name[0], sz); + socket.read_all(num_tus); + socket.read_all(lines_total); + socket.read_all(process_id); + socket.read_all(parent_process_id); + socket.read_all(process_group); - socket->read_all(current_unit.inst_sites); + translation_units.resize(num_tus); + for (auto &t : translation_units) { + socket.read_all(file_name_sz); + t.file_name.resize(file_name_sz); + socket.read_all((uint8_t *)&t.file_name[0], file_name_sz); + socket.read_all(t.num_lines); + socket.read_all(t.inst_sites); + + t.execution_counts.resize(t.num_lines, 0); + t.source.resize(t.num_lines); + read_source(t); } - - demo_font_print_stats(font); } void -RuntimeProcess::read_file(std::string file_name, glyphy_point_t top_left) +RuntimeProcess::read_source(struct TranslationUnit &t) { std::string line; - std::ifstream src_file(file_name, std::ios::binary); - - if (! src_file) - errx(1, "src_file.open()"); - - src_file.seekg(0, src_file.end); - int length = src_file.tellg(); - src_file.seekg(0, src_file.beg); - - char *src_buffer = new char [length + 1]; - - src_file.read(src_buffer, length); - src_buffer[length] = '\0'; + std::ifstream file_stream(t.file_name); - if (! src_file) - errx(1, "src_file.read()"); - src_file.close(); + if (file_stream.is_open() == 0) + errx(1, "ifstream.open()"); - demo_buffer_move_to(buffer, &top_left); - demo_buffer_add_text(buffer, src_buffer, font, 1); - - delete[] src_buffer; -} - -void -RuntimeProcess::draw() -{ + for (auto &l : t.source) + std::getline(file_stream, l); } void -RuntimeProcess::idle() +RuntimeProcess::read_executions() { for (auto &t : translation_units) { size_t bytes_total = t.num_lines * sizeof(uint64_t); - - socket->read_all((uint8_t *)&t.execution_counts[0], bytes_total); - - int execs = 0; - for (int i = 0; i < t.num_lines; i++) - execs += t.execution_counts[i]; - - // std::cout << t.file_name << ": " << execs << std::endl; + socket.read_all((uint8_t *)&t.execution_counts[0], bytes_total); } // Send response back uint8_t msg_type = 1; - assert(socket->write_all(&msg_type, 1) == 1); + assert(socket.write_all(&msg_type, 1) == 1); } diff --git a/src/runtime_conn.h b/src/runtime_conn.h @@ -1,42 +1,34 @@ #ifndef TEXT_H #define TEXT_H - #include <string> #include <vector> #include "af_unix.h" -#include "draw.h" - -#include "gl_buffer.h" -#include "demo-font.h" struct TranslationUnit { std::string file_name; uint32_t num_lines; - std::vector<uint64_t> execution_counts; uint32_t inst_sites; + std::vector<uint64_t> execution_counts; + std::vector<std::string> source; }; -class RuntimeProcess : public drawable { +class RuntimeProcess { public: - RuntimeProcess(af_unix *, demo_buffer_t *, demo_font_t *); - - void draw(); - void idle(); -private: - void read_file(std::string, glyphy_point_t); + RuntimeProcess(af_unix &); + void read_executions(); std::string program_name; uint64_t lines_total; pid_t process_id; pid_t parent_process_id; pid_t process_group; - - af_unix *socket; - demo_buffer_t *buffer; - demo_font_t *font; - std::vector<TranslationUnit> translation_units; +private: + void read_source(struct TranslationUnit &); + + double y_margin; + af_unix socket; }; #endif diff --git a/src/term_main.cc b/src/term_main.cc @@ -0,0 +1,41 @@ +#include <stdlib.h> // exit +#include <ncurses.h> + +#include "af_unix.h" +#include "runtime_conn.h" + +int +main(int argc, char *argv[]) +{ + int ch; + af_unix listen_sock; + + listen_sock.set_listen(); + + initscr(); + if (has_colors() == FALSE) { + endwin(); + printf("Your terminal does not support color\n"); + exit(1); + } + start_color(); + init_pair(1, COLOR_RED, COLOR_BLACK); + + printw("Waiting for connection on /tmp/citrun-gl.socket\n"); + refresh(); + + af_unix *client = listen_sock.accept(); + if (client == NULL) + errx(1, "client was NULL"); + + RuntimeProcess conn(*client); + printw("program name: %s\n", conn.program_name.c_str()); + printw("program name: %s\n", conn.program_name.c_str()); + + refresh(); + + getch(); + endwin(); + + return 0; +}