+2018-11-09 Tom Tromey <tom@tromey.com>
+
+ * common/scoped_fd.h (class scoped_fd): Add move constructor and
+ move assignment operator.
+ * psymtab.c (psymtab_to_fullname): Update.
+ * source.h (open_source_file): Return scoped_fd.
+ (find_and_open_source): Likewise.
+ * source.c (open_source_file): Return scoped_fd.
+ (get_filename_and_charpos): Update.
+ (print_source_lines_base): Update. Use scoped_fd::to_file.
+ (forward_search_command): Likewise.
+ (reverse_search_command): Likewise.
+ (find_and_open_source): Return scoped_fd.
+ * tui/tui-source.c (tui_set_source_content): Update. Use
+ gdb_file_up.
+
2018-11-09 John Baldwin <jhb@FreeBSD.org>
* minsyms.c (minimal_symbol_reader::install): Fix unsigned
{
public:
explicit scoped_fd (int fd = -1) noexcept : m_fd (fd) {}
+
+ scoped_fd (scoped_fd &&other)
+ : m_fd (other.m_fd)
+ {
+ other.m_fd = -1;
+ }
+
~scoped_fd ()
{
if (m_fd >= 0)
close (m_fd);
}
+ scoped_fd &operator= (scoped_fd &&other)
+ {
+ if (m_fd != other.m_fd)
+ {
+ if (m_fd >= 0)
+ close (m_fd);
+ m_fd = other.m_fd;
+ other.m_fd = -1;
+ }
+ return *this;
+ }
+
DISABLE_COPY_AND_ASSIGN (scoped_fd);
int release () noexcept
if (ps->fullname == NULL)
{
gdb::unique_xmalloc_ptr<char> fullname;
- int fd = find_and_open_source (ps->filename, ps->dirname, &fullname);
+ scoped_fd fd = find_and_open_source (ps->filename, ps->dirname,
+ &fullname);
ps->fullname = fullname.release ();
- if (fd >= 0)
- close (fd);
- else
+ if (fd.get () < 0)
{
/* rewrite_source_path would be applied by find_and_open_source, we
should report the pathname where GDB tried to find the file. */
return gdb::unique_xmalloc_ptr<char> (new_path);
}
-int
+/* See source.h. */
+
+scoped_fd
find_and_open_source (const char *filename,
const char *dirname,
gdb::unique_xmalloc_ptr<char> *fullname)
if (result >= 0)
{
*fullname = gdb_realpath (fullname->get ());
- return result;
+ return scoped_fd (result);
}
/* Didn't work -- free old one, try again. */
OPEN_MODE, fullname);
}
- return result;
+ return scoped_fd (result);
}
/* Open a source file given a symtab S. Returns a file descriptor or
This function is a convience function to find_and_open_source. */
-int
+scoped_fd
open_source_file (struct symtab *s)
{
if (!s)
- return -1;
+ return scoped_fd (-1);
- gdb::unique_xmalloc_ptr<char> fullname (s->fullname);
+ gdb::unique_xmalloc_ptr<char> fullname;
s->fullname = NULL;
- int fd = find_and_open_source (s->filename, SYMTAB_DIRNAME (s), &fullname);
+ scoped_fd fd = find_and_open_source (s->filename, SYMTAB_DIRNAME (s),
+ &fullname);
s->fullname = fullname.release ();
return fd;
}
to handle cases like the file being moved. */
if (s->fullname == NULL)
{
- int fd = open_source_file (s);
+ scoped_fd fd = open_source_file (s);
- if (fd >= 0)
- close (fd);
- else
+ if (fd.get () < 0)
{
gdb::unique_xmalloc_ptr<char> fullname;
{
int linenums_changed = 0;
- scoped_fd desc (open_source_file (s));
+ scoped_fd desc = open_source_file (s);
if (desc.get () < 0)
{
if (fullname)
print_source_lines_flags flags)
{
int c;
- int desc;
+ scoped_fd desc;
int noprint = 0;
int nlines = stopline - line;
struct ui_out *uiout = current_uiout;
{
last_source_visited = s;
desc = open_source_file (s);
+ if (desc.get () < 0)
+ {
+ last_source_error = desc.get ();
+ noprint = 1;
+ }
}
else
{
- desc = last_source_error;
flags |= PRINT_SOURCE_LINES_NOERROR;
+ noprint = 1;
}
}
else
{
- desc = last_source_error;
flags |= PRINT_SOURCE_LINES_NOERROR;
noprint = 1;
}
- if (desc < 0 || noprint)
+ if (noprint)
{
- last_source_error = desc;
-
if (!(flags & PRINT_SOURCE_LINES_NOERROR))
{
const char *filename = symtab_to_filename_for_display (s);
last_source_error = 0;
if (s->line_charpos == 0)
- find_source_lines (s, desc);
+ find_source_lines (s, desc.get ());
if (line < 1 || line > s->nlines)
- {
- close (desc);
- error (_("Line number %d out of range; %s has %d lines."),
- line, symtab_to_filename_for_display (s), s->nlines);
- }
+ error (_("Line number %d out of range; %s has %d lines."),
+ line, symtab_to_filename_for_display (s), s->nlines);
- if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
- {
- close (desc);
- perror_with_name (symtab_to_filename_for_display (s));
- }
+ if (lseek (desc.get (), s->line_charpos[line - 1], 0) < 0)
+ perror_with_name (symtab_to_filename_for_display (s));
- gdb_file_up stream (fdopen (desc, FDOPEN_MODE));
+ gdb_file_up stream = desc.to_file (FDOPEN_MODE);
clearerr (stream.get ());
while (nlines-- > 0)
if (current_source_symtab == 0)
select_source_symtab (0);
- scoped_fd desc (open_source_file (current_source_symtab));
+ scoped_fd desc = open_source_file (current_source_symtab);
if (desc.get () < 0)
perror_with_name (symtab_to_filename_for_display (current_source_symtab));
< 0)
perror_with_name (symtab_to_filename_for_display (current_source_symtab));
- gdb_file_up stream (fdopen (desc.release (), FDOPEN_MODE));
+ gdb_file_up stream = desc.to_file (FDOPEN_MODE);
clearerr (stream.get ());
while (1)
{
if (current_source_symtab == 0)
select_source_symtab (0);
- scoped_fd desc (open_source_file (current_source_symtab));
+ scoped_fd desc = open_source_file (current_source_symtab);
if (desc.get () < 0)
perror_with_name (symtab_to_filename_for_display (current_source_symtab));
< 0)
perror_with_name (symtab_to_filename_for_display (current_source_symtab));
- gdb_file_up stream (fdopen (desc.release (), FDOPEN_MODE));
+ gdb_file_up stream = desc.to_file (FDOPEN_MODE);
clearerr (stream.get ());
while (line > 1)
{
#ifndef SOURCE_H
#define SOURCE_H
+#include "common/scoped_fd.h"
+
struct symtab;
/* See openp function definition for their description. */
On Failure
An invalid file descriptor is returned (the return value is negative).
FULLNAME is set to NULL. */
-extern int find_and_open_source (const char *filename,
- const char *dirname,
- gdb::unique_xmalloc_ptr<char> *fullname);
+extern scoped_fd find_and_open_source (const char *filename,
+ const char *dirname,
+ gdb::unique_xmalloc_ptr<char> *fullname);
/* Open a source file given a symtab S. Returns a file descriptor or
negative number for error. */
-extern int open_source_file (struct symtab *s);
+extern scoped_fd open_source_file (struct symtab *s);
extern gdb::unique_xmalloc_ptr<char> rewrite_source_path (const char *path);
if (s != (struct symtab *) NULL)
{
- FILE *stream;
- int i, desc, c, line_width, nlines;
+ int i, c, line_width, nlines;
char *src_line = 0;
if ((ret = tui_alloc_source_buffer (TUI_SRC_WIN)) == TUI_SUCCESS)
/* Take hilite (window border) into account, when
calculating the number of lines. */
nlines = (line_no + (TUI_SRC_WIN->generic.height - 2)) - line_no;
- desc = open_source_file (s);
- if (desc < 0)
+ scoped_fd desc = open_source_file (s);
+ if (desc.get () < 0)
{
if (!noerror)
{
else
{
if (s->line_charpos == 0)
- find_source_lines (s, desc);
+ find_source_lines (s, desc.get ());
if (line_no < 1 || line_no > s->nlines)
- {
- close (desc);
- printf_unfiltered ("Line number %d out of range; "
- "%s has %d lines.\n",
- line_no,
- symtab_to_filename_for_display (s),
- s->nlines);
- }
- else if (lseek (desc, s->line_charpos[line_no - 1], 0) < 0)
- {
- close (desc);
- perror_with_name (symtab_to_filename_for_display (s));
- }
+ printf_unfiltered ("Line number %d out of range; "
+ "%s has %d lines.\n",
+ line_no,
+ symtab_to_filename_for_display (s),
+ s->nlines);
+ else if (lseek (desc.get (), s->line_charpos[line_no - 1], 0)
+ < 0)
+ perror_with_name (symtab_to_filename_for_display (s));
else
{
int offset, cur_line_no, cur_line, cur_len, threshold;
line and the offset to start the display. */
offset = src->horizontal_offset;
threshold = (line_width - 1) + offset;
- stream = fdopen (desc, FOPEN_RT);
- clearerr (stream);
+ gdb_file_up stream = desc.to_file (FOPEN_RT);
+ clearerr (stream.get ());
cur_line = 0;
src->gdbarch = get_objfile_arch (SYMTAB_OBJFILE (s));
src->start_line_or_addr.loa = LOA_LINE;
= TUI_SRC_WIN->generic.content[cur_line];
/* Get the first character in the line. */
- c = fgetc (stream);
+ c = fgetc (stream.get ());
if (offset == 0)
src_line = TUI_SRC_WIN->generic.content[cur_line]
{ /* If we have not reached EOL, then
eat chars until we do. */
while (c != EOF && c != '\n' && c != '\r')
- c = fgetc (stream);
+ c = fgetc (stream.get ());
/* Handle non-'\n' end-of-line. */
if (c == '\r'
- && (c = fgetc (stream)) != '\n'
+ && (c = fgetc (stream.get ())) != '\n'
&& c != EOF)
{
- ungetc (c, stream);
- c = '\r';
+ ungetc (c, stream.get ());
+ c = '\r';
}
}
}
while (c != EOF && c != '\n' && c != '\r'
&& i < threshold
- && (c = fgetc (stream)));
+ && (c = fgetc (stream.get ())));
}
/* Now copy the line taking the offset into
account. */
}
if (offset > 0)
xfree (src_line);
- fclose (stream);
TUI_SRC_WIN->generic.content_size = nlines;
ret = TUI_SUCCESS;
}