aboutsummaryrefslogtreecommitdiff
path: root/lib/std/debug.zig
AgeCommit message (Collapse)Author
2024-03-14coff: only store PDB basenameElaine Gibson
2024-03-11std.builtin: make atomic order fields lowercaseTristan Ross
2024-02-25fix crash when calling StackIterator.isValidMemory with emscriptenJae B
2024-02-23fix compilation issuesJae B
ie. C:\zig\current\lib\std\debug.zig:726:23: error: no field or member function named 'getDwarfInfoForAddress' in 'dwarf.DwarfInfo' if (try module.getDwarfInfoForAddress(unwind_state.debug_info.allocator, unwind_state.dwarf_context.pc)) |di| { ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~ C:\zig\current\lib\std\dwarf.zig:663:23: note: struct declared here pub const DwarfInfo = struct { ^~~~~~ referenced by: next_internal: C:\zig\current\lib\std\debug.zig:737:29 next: C:\zig\current\lib\std\debug.zig:654:31 remaining reference traces hidden; use '-freference-trace' to see all reference traces C:\zig\current\lib\std\debug.zig:970:31: error: no field or member function named 'getSymbolAtAddress' in 'dwarf.DwarfInfo' const symbol_info = module.getSymbolAtAddress(debug_info.allocator, address) catch |err| switch (err) { ~~~~~~^~~~~~~~~~~~~~~~~~~ C:\zig\current\lib\std\dwarf.zig:663:23: note: struct declared here pub const DwarfInfo = struct {
2024-02-14add deflate implemented from first principlesIgor Anić
Zig deflate compression/decompression implementation. It supports compression and decompression of gzip, zlib and raw deflate format. Fixes #18062. This PR replaces current compress/gzip and compress/zlib packages. Deflate package is renamed to flate. Flate is common name for deflate/inflate where deflate is compression and inflate decompression. There are breaking change. Methods signatures are changed because of removal of the allocator, and I also unified API for all three namespaces (flate, gzip, zlib). Currently I put old packages under v1 namespace they are still available as compress/v1/gzip, compress/v1/zlib, compress/v1/deflate. Idea is to give users of the current API little time to postpone analyzing what they had to change. Although that rises question when it is safe to remove that v1 namespace. Here is current API in the compress package: ```Zig // deflate fn compressor(allocator, writer, options) !Compressor(@TypeOf(writer)) fn Compressor(comptime WriterType) type fn decompressor(allocator, reader, null) !Decompressor(@TypeOf(reader)) fn Decompressor(comptime ReaderType: type) type // gzip fn compress(allocator, writer, options) !Compress(@TypeOf(writer)) fn Compress(comptime WriterType: type) type fn decompress(allocator, reader) !Decompress(@TypeOf(reader)) fn Decompress(comptime ReaderType: type) type // zlib fn compressStream(allocator, writer, options) !CompressStream(@TypeOf(writer)) fn CompressStream(comptime WriterType: type) type fn decompressStream(allocator, reader) !DecompressStream(@TypeOf(reader)) fn DecompressStream(comptime ReaderType: type) type // xz fn decompress(allocator: Allocator, reader: anytype) !Decompress(@TypeOf(reader)) fn Decompress(comptime ReaderType: type) type // lzma fn decompress(allocator, reader) !Decompress(@TypeOf(reader)) fn Decompress(comptime ReaderType: type) type // lzma2 fn decompress(allocator, reader, writer !void // zstandard: fn DecompressStream(ReaderType, options) type fn decompressStream(allocator, reader) DecompressStream(@TypeOf(reader), .{}) struct decompress ``` The proposed naming convention: - Compressor/Decompressor for functions which return type, like Reader/Writer/GeneralPurposeAllocator - compressor/compressor for functions which are initializers for that type, like reader/writer/allocator - compress/decompress for one shot operations, accepts reader/writer pair, like read/write/alloc ```Zig /// Compress from reader and write compressed data to the writer. fn compress(reader: anytype, writer: anytype, options: Options) !void /// Create Compressor which outputs the writer. fn compressor(writer: anytype, options: Options) !Compressor(@TypeOf(writer)) /// Compressor type fn Compressor(comptime WriterType: type) type /// Decompress from reader and write plain data to the writer. fn decompress(reader: anytype, writer: anytype) !void /// Create Decompressor which reads from reader. fn decompressor(reader: anytype) Decompressor(@TypeOf(reader) /// Decompressor type fn Decompressor(comptime ReaderType: type) type ``` Comparing this implementation with the one we currently have in Zig's standard library (std). Std is roughly 1.2-1.4 times slower in decompression, and 1.1-1.2 times slower in compression. Compressed sizes are pretty much same in both cases. More resutls in [this](https://github.com/ianic/flate) repo. This library uses static allocations for all structures, doesn't require allocator. That makes sense especially for deflate where all structures, internal buffers are allocated to the full size. Little less for inflate where we std version uses less memory by not preallocating to theoretical max size array which are usually not fully used. For deflate this library allocates 395K while std 779K. For inflate this library allocates 74.5K while std around 36K. Inflate difference is because we here use 64K history instead of 32K in std. If merged existing usage of compress gzip/zlib/deflate need some changes. Here is example with necessary changes in comments: ```Zig const std = @import("std"); // To get this file: // wget -nc -O war_and_peace.txt https://www.gutenberg.org/ebooks/2600.txt.utf-8 const data = @embedFile("war_and_peace.txt"); pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer std.debug.assert(gpa.deinit() == .ok); const allocator = gpa.allocator(); try oldDeflate(allocator); try new(std.compress.flate, allocator); try oldZlib(allocator); try new(std.compress.zlib, allocator); try oldGzip(allocator); try new(std.compress.gzip, allocator); } pub fn new(comptime pkg: type, allocator: std.mem.Allocator) !void { var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Compressor var cmp = try pkg.compressor(buf.writer(), .{}); _ = try cmp.write(data); try cmp.finish(); var fbs = std.io.fixedBufferStream(buf.items); // Decompressor var dcp = pkg.decompressor(fbs.reader()); const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } pub fn oldDeflate(allocator: std.mem.Allocator) !void { const deflate = std.compress.v1.deflate; // Compressor var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Remove allocator // Rename deflate -> flate var cmp = try deflate.compressor(allocator, buf.writer(), .{}); _ = try cmp.write(data); try cmp.close(); // Rename to finish cmp.deinit(); // Remove // Decompressor var fbs = std.io.fixedBufferStream(buf.items); // Remove allocator and last param // Rename deflate -> flate // Remove try var dcp = try deflate.decompressor(allocator, fbs.reader(), null); defer dcp.deinit(); // Remove const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } pub fn oldZlib(allocator: std.mem.Allocator) !void { const zlib = std.compress.v1.zlib; var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Compressor // Rename compressStream => compressor // Remove allocator var cmp = try zlib.compressStream(allocator, buf.writer(), .{}); _ = try cmp.write(data); try cmp.finish(); cmp.deinit(); // Remove var fbs = std.io.fixedBufferStream(buf.items); // Decompressor // decompressStream => decompressor // Remove allocator // Remove try var dcp = try zlib.decompressStream(allocator, fbs.reader()); defer dcp.deinit(); // Remove const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } pub fn oldGzip(allocator: std.mem.Allocator) !void { const gzip = std.compress.v1.gzip; var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Compressor // Rename compress => compressor // Remove allocator var cmp = try gzip.compress(allocator, buf.writer(), .{}); _ = try cmp.write(data); try cmp.close(); // Rename to finisho cmp.deinit(); // Remove var fbs = std.io.fixedBufferStream(buf.items); // Decompressor // Rename decompress => decompressor // Remove allocator // Remove try var dcp = try gzip.decompress(allocator, fbs.reader()); defer dcp.deinit(); // Remove const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } ```
2024-02-09Merge pull request #18712 from Vexu/std.optionsAndrew Kelley
std: make options a struct instance instead of a namespace
2024-02-06std.os.linux.MAP: use a packed structAndrew Kelley
Introduces type safety to this constant. Eliminates one use of `usingnamespace`.
2024-02-01remove std.io.ModeVeikka Tuominen
2024-01-16byos: Ease `GeneralPurposeAllocator` integrationJay Petacat
These changes enable me to use `GeneralPurposeAllocator` with my "Bring Your Own OS" package. The previous checks for a freestanding target have been expanded to `@hasDecl` checks. - `root.os.heap.page_allocator` is used if it exists. - `debug.isValidMemory` only calls `os.msync` if it's supported.
2024-01-08fixups from previous commitAndrew Kelley
* rename the functions * make the other function public and give it a better name * interact with stderr_mutex * std lib test coverage
2024-01-08general-use std.debug.hexdump for printing hexdumpsWooster
Recently, when I've been working with structures of data that is not directly in RAM but rather laid out in bytes somewhere else, it was always very useful to print out maybe the next 50 bytes or the previous 50 bytes or so to see what's ahead or before me. I would usually do this with a quick `std.debug.print("{any}\n", .{bytes});` or something but the output is not as nice obviously.
2024-01-03Remove some `@as` coercions from assertionsCarl Åstholm
These are some spurious fixes to help illustrate the improved ergonomics of the `expectEqual` change. It is by no means complete.
2023-12-01test: test with `-fstrip` and fix failuresJacob Young
Closes #17513
2023-11-29Remove all usages of `std.mem.copy` and remove `std.mem.set` (#18143)David Rubin
2023-11-29std.debug: optimized printLineFromFileAnyOs (#18142)tjog
* std.debug: optimized printLineFromFileAnyOs Uses mem.indexOfScalar to speed line iteration instead of byte for byte. Also prints the whole line in a single write (or up to a page size at a time) Closes #18099 * add test cases for printLineFromFileAnyOs
2023-11-26bsd: debitrot atomic and debugMichael Dusan
closes #18119
2023-11-23Merge pull request #18085 from ziglang/std-atomicsAndrew Kelley
rework std.atomic
2023-11-23Remove unnecessary branch on absolute pathRyan Liptak
Also remove the TODO--it was added in fd067fbe8b14c445ed764da15fff5163156126df, but the current code no longer used that workaround, so this TODO didn't seem relevant anymore.
2023-11-22rework std.atomicAndrew Kelley
* move std.atomic.Atomic to std.atomic.Value * fix incorrect argument order passed to testing.expectEqual * make the functions be a thin wrapper over the atomic builtins and stick to the naming conventions. * remove pointless functions loadUnchecked and storeUnchecked. Instead, name the field `raw` instead of `value` (which is redundant with the type name). * simplify the tests by not passing every possible combination. Many cases were iterating over every possible combinations but then not even using the for loop element value! * remove the redundant compile errors which are already implemented by the language itself. * remove dead x86 inline assembly. this should be implemented in the language if at all.
2023-11-21std.debug: detect general protection faults on x86_64-linuxWooster
```zig const std = @import("std"); pub fn main() !void { var addr: *u8 = @ptrFromInt(0xaaaaaaaaaaaaaaaa); addr.* = 1; } ``` On x86_64-linux: Before: ``` $ zig run x.zig Segmentation fault at address 0x0 /home/wooster/Desktop/zig/x.zig:5:5: 0x21d887 in main (x) addr.* = 1; ^ /home/wooster/Desktop/zig-linux-x86_64/lib/std/start.zig:583:37: 0x21d847 in posixCallMainAndExit (x) const result = root.main() catch |err| { ^ /home/wooster/Desktop/zig-linux-x86_64/lib/std/start.zig:251:5: 0x21d371 in _start (x) asm volatile (switch (native_arch) { ^ ???:?:?: 0x0 in ??? (???) Aborted (core dumped) ``` After: ``` $ zig run x.zig --zig-lib-dir lib General protection exception /home/wooster/Desktop/zig/x.zig:5:5: 0x21d907 in main (x) addr.* = 1; ^ /home/wooster/Desktop/zig/lib/std/start.zig:583:37: 0x21d8c7 in posixCallMainAndExit (x) const result = root.main() catch |err| { ^ /home/wooster/Desktop/zig/lib/std/start.zig:251:5: 0x21d3f1 in _start (x) asm volatile (switch (native_arch) { ^ ???:?:?: 0x0 in ??? (???) Aborted (core dumped) ``` As @IntegratedQuantum pointed out in <https://github.com/ziglang/zig/issues/17745#issuecomment-1783815386>, it seems that if `code` of the `siginfo_t` instance is a certain value (128), you are able to distinguish between a general protection exception and a segmentation fault. This does not seem to be documented on `man sigaction`: ``` The following values can be placed in si_code for a SIGSEGV signal: SEGV_MAPERR Address not mapped to object. SEGV_ACCERR Invalid permissions for mapped object. SEGV_BNDERR (since Linux 3.19) Failed address bound checks. SEGV_PKUERR (since Linux 4.6) Access was denied by memory protection keys. See pkeys(7). The protection key which applied to this access is available via si_pkey. ``` (those constants are 1, 2, 3, and 4; none of them are the 128) I can't find a lot of documentation about this but it seems to work consistently for me on x86_64-linux. Here is a gist which provides additional evidence that this is a reliable way of checking for a general protection fault: https://gist.github.com/ytoshima/5682393 (read comment in first line) See also: https://stackoverflow.com/questions/64309366/why-is-the-segfault-address-null-when-accessing-memory-that-has-any-of-the-16-mo This only seems to affect x86_64 and on 32-bit x86 this does not seem to be a problem. Helps with #17745 but doesn't close it because the issue still exists on Windows and other POSIX OSs. I also limited this to x86_64-linux for now because that's the only platform where I tested it. Might work on more POSIX OSs.
2023-11-19lib: correct unnecessary uses of 'var'mlugg
2023-10-31std.builtin.Endian: make the tags lower caseAndrew Kelley
Let's take this breaking change opportunity to fix the style of this enum.
2023-10-31mem: fix ub in writeIntJacob Young
Use inline to vastly simplify the exposed API. This allows a comptime-known endian parameter to be propogated, making extra functions for a specific endianness completely unnecessary.
2023-10-27x86_64: fix c abi test failuresJacob Young
2023-10-25x86_64: pass more testsJacob Young
* 128-bit integer multiplication with overflow * more instruction encodings used by std inline asm * implement the `try_ptr` air instruction * follow correct stack frame abi * enable full panic handler * enable stack traces
2023-10-22Revert "Revert "Merge pull request #17637 from jacobly0/x86_64-test-std""Jacob Young
This reverts commit 6f0198cadbe29294f2bf3153a27beebd64377566.
2023-10-22Revert "Merge pull request #17637 from jacobly0/x86_64-test-std"Andrew Kelley
This reverts commit 0c99ba1eab63865592bb084feb271cd4e4b0357e, reversing changes made to 5f92b070bf284f1493b1b5d433dd3adde2f46727. This caused a CI failure when it landed in master branch due to a 128-bit `@byteSwap` in std.mem.
2023-10-22Merge pull request #17637 from jacobly0/x86_64-test-stdJacob Young
x86_64: start to enable `test-std` and `test-compiler-rt` testing
2023-10-21Fix simple doc mistakes. (#17624)JustinWayland
* Add missing period in Stack's description This looks fine in the source, but looks bad when seen on the documentation website. * Correct documentation for attachSegfaultHandler() The description for attachSegfaultHandler() looks pretty bad without indicating that the stuff at the end is code * Added missing 'the's in Queue.put's documentation * Fixed several errors in Stack's documentation `push()` and `pop()` were not styled as code There was no period after `pop()`, which looks bad on the documentation. * Fix multiple problems in base64.zig Both "invalid"s in Base64.decoder were not capitalized. Missing period in documentation of Base64DecoderWithIgnore.calcSizeUpperBound. * Fix capitalization typos in bit_set.zig In DynamicBitSetUnmanaged.deinit's and DynamicBitSet.deinit's documentation, "deinitializes" was uncapitalized. * Fix typos in fifo.zig's documentation Added a previously missing period to the end of the first line of LinearFifo.writableSlice's documentation. Added missing periods to both lines of LinearFifo.pump's documentation. * Fix typos in fmt.bufPrint's documentation The starts of both lines were not capitalized. * Fix minor documentation problems in fs/file.zig Missing periods in documentation for Permissions.setReadOnly, PermissionsWindows.setReadOnly, MetadataUnix.created, MetadataLinux.created, and MetadataWindows.created. * Fix a glaring typo in enums.zig * Correct errors in fs.zig * Fixed documentation problems in hash_map.zig The added empty line in verify_context's documentation is needed, otherwise autodoc for some reason assumes that the list hasn't been terminated and continues reading off the rest of the documentation as if it were part of the second list item. * Added lines between consecutive URLs in http.zig Makes the documentation conform closer to what was intended. * Fix wrongfully ended sentence in Uri.zig * Handle wrongly entered comma in valgrind.zig. * Add missing periods in wasm.zig's documentation * Fix odd spacing in event/loop.zig * Add missing period in http/Headers.zig * Added missing period in io/limited_reader.zig This isn't in the documentation due to what I guess is a limitation of autodoc, but it's clearly supposed to be. If it was, it would look pretty bad. * Correct documentation in math/big/int.zig * Correct formatting in math/big/rational.zig * Create an actual link to ZIGNOR's paper. * Fixed grammatical issues in sort/block.zig This will not show up in the documentation currently. * Fix typo in hash_map.zig
2023-10-21x86_64: disable failing tests, enable test-std testingJacob Young
2023-10-02Add illumos OS tagStephen Gregoratto
- Adds `illumos` to the `Target.Os.Tag` enum. A new function, `isSolarish` has been added that returns true if the tag is either Solaris or Illumos. This matches the naming convention found in Rust's `libc` crate[1]. - Add the tag wherever `.solaris` is being checked against. - Check for the C pre-processor macro `__illumos__` in CMake to set the proper target tuple. Illumos distros patch their compilers to have this in the "built-in" set (verified with `echo | cc -dM -E -`). Alternatively you could check the output of `uname -o`. Right now, both Solaris and Illumos import from `c/solaris.zig`. In the future it may be worth putting the shared ABI bits in a base file, and mixing that in with specific `c/solaris.zig`/`c/illumos.zig` files. [1]: https://github.com/rust-lang/libc/tree/6e02a329a2a27f6887ea86952f389ca11e06448c/src/unix/solarish
2023-09-23compiler: preserve result type information through address-of operatormlugg
This commit introduces the new `ref_coerced_ty` result type into AstGen. This represents a expression which we want to treat as an lvalue, and the pointer will be coerced to a given type. This change gives known result types to many expressions, in particular struct and array initializations. This allows certain casts to work which previously required explicitly specifying types via `@as`. It also eliminates our dependence on anonymous struct types for expressions of the form `&.{ ... }` - this paves the way for #16865, and also results in less Sema magic happening for such initializations, also leading to potentially better runtime code. As part of these changes, this commit also implements #17194 by disallowing RLS on explicitly-typed struct and array initializations. Apologies for linking these changes - it seemed rather pointless to try and separate them, since they both make big changes to struct and array initializations in AstGen. The rationale for this change can be found in the proposal - in essence, performing RLS whilst maintaining the semantics of the intermediary type is a very difficult problem to solve. This allowed the problematic `coerce_result_ptr` ZIR instruction to be completely eliminated, which in turn also simplified the logic for inferred allocations in Sema - thanks to this, we almost break even on line count! In doing this, the ZIR instructions surrounding these initializations have been restructured - some have been added and removed, and others renamed for clarity (and their semantics changed slightly). In order to optimize ZIR tag count, the `struct_init_anon_ref` and `array_init_anon_ref` instructions have been removed in favour of using `ref` on a standard anonymous value initialization, since these instructions are now virtually never used. Lastly, it's worth noting that this commit introduces a slightly strange source of generic poison types: in the expression `@as(*anyopaque, &x)`, the sub-expression `x` has a generic poison result type, despite no generic code being involved. This turns out to be a logical choice, because we don't know the result type for `x`, and the generic poison type represents precisely this case, providing the semantics we need. Resolves: #16512 Resolves: #17194
2023-08-17Merge pull request #16824 from mikdusan/bsdAndrew Kelley
de-bitrot the BSDs
2023-08-17std.debug: openbsd does not have getcontextMichael Dusan
2023-08-15std.debug: avoid os.getFdPathMichael Dusan
Commit ea9917d9bd921f9fc14028a8dbf9e3f0f2579120 introduced usage of fs.Dir.realpath which eventually calls os.getFdpath which is forbidden to be used by the compiler. It causes building zig to fail on OpenBsd, NetBSD and older versions of FreeBSD and DragonFly. This patch substitutes with os.realpath on libc targets and eventually calls c.realpath and allows zig to build. Any use of realpath is not desired but this is the lesser evil.
2023-08-15coff: handle the case of there being no PDB pathkcbanner
2023-08-15debug: support looking up debug symbols in both PDB and DWARF debug info, ↵kcbanner
instead of only using DWARF if `.debug_info` is present coff: support reading from memory loaded by the loader, or a mapped file
2023-08-06std: remove some unused imports (#16710)Zachary Raineri
2023-08-04debug: expose module debug info deinitializationDavid Gonzalez Martin
Before this commit, you could use readElfDebugInfo independently with one catch: the data is not freed since the deinitialization functions for ModuleDebugInfo are private. This change makes them public so the users of such function and similar can free the memory after the debug symbols have been used.
2023-07-26debug: skip unwind error printing on platforms that don't have_ucontextkcbanner
2023-07-26debug: print unwind errors if they occur on the first iteration, and ↵kcbanner
differentiate between missing info and an actual unwind error in the message
2023-07-20dwarf: move macho unwind code from macho -> dwarfkcbanner
dwarf: fixup unchecked .eh_frame CIE offset subtraction
2023-07-20test: disable omit_frame_pointer unwinding tests on aarch64-macoskcbanner
dwarf: handle signal frame CIE flag
2023-07-20debug: disable the new unwinder on aarch64-macoskcbanner
2023-07-20macho: don't scan all eh_frame entries, instead follow the offset from the ↵kcbanner
__unwind_info directly
2023-07-20dwarf: add explicit_fde_offset to support more optimal __unwind_info dwarf ↵kcbanner
lookups
2023-07-20debug: load the macho unwind sections from the already-mapped imagekcbanner
2023-07-20dwarf: fixup integer overflow in readEhPointerkcbanner
debug: handle the possibility of eh_frame / debug_frame being mapped in memory or loaded from disk
2023-07-20use eh_frame from the mapped binary if availablekcbanner
2023-07-20debug: fixup an inconsistency in the getcontext implementation on aarch64-macoskcbanner